From 2bb252e08f3dbf74a0a11cd443fc9ac8241fa53a Mon Sep 17 00:00:00 2001 From: Samil Ayoub Date: Thu, 3 Sep 2020 22:41:55 +0100 Subject: [PATCH] Clean the code --- simple-factory/README.md | 128 ------------------ simple-factory/etc/simple-factory.urm.puml | 35 ----- simple-factory/pom.xml | 41 ------ .../java/com/iluwatar/simplefactory/App.java | 42 ------ .../java/com/iluwatar/simplefactory/Car.java | 10 -- .../simplefactory/CarSimpleFactory.java | 25 ---- .../com/iluwatar/simplefactory/Ferrari.java | 14 -- .../java/com/iluwatar/simplefactory/Ford.java | 14 -- .../com/iluwatar/simplefactory/AppTest.java | 14 -- .../simplefactory/CarSimpleFactoryTest.java | 15 -- 10 files changed, 338 deletions(-) delete mode 100644 simple-factory/README.md delete mode 100644 simple-factory/etc/simple-factory.urm.puml delete mode 100644 simple-factory/pom.xml delete mode 100644 simple-factory/src/main/java/com/iluwatar/simplefactory/App.java delete mode 100644 simple-factory/src/main/java/com/iluwatar/simplefactory/Car.java delete mode 100644 simple-factory/src/main/java/com/iluwatar/simplefactory/CarSimpleFactory.java delete mode 100644 simple-factory/src/main/java/com/iluwatar/simplefactory/Ferrari.java delete mode 100644 simple-factory/src/main/java/com/iluwatar/simplefactory/Ford.java delete mode 100644 simple-factory/src/test/java/com/iluwatar/simplefactory/AppTest.java delete mode 100644 simple-factory/src/test/java/com/iluwatar/simplefactory/CarSimpleFactoryTest.java diff --git a/simple-factory/README.md b/simple-factory/README.md deleted file mode 100644 index 1d069a6dd..000000000 --- a/simple-factory/README.md +++ /dev/null @@ -1,128 +0,0 @@ ---- -layout: pattern -title: Simple Factory -folder: simple-factory -permalink: /patterns/simple-factory/ -categories: Creational -tags: - - Gang of Four ---- - -## Also known as - -* Factory -* Static Factory Method - -## Intent - -Providing a static method encapsulated in a class called factory, in order to hide the implementation logic and makes client code focus on usage rather then initialization new objects. - -## Explanation - -Real world example - -> Lets say we have a web application connected to SQLServer, but now we want to switch to Oracle. To do so without modifying existing source code, we need to implements Simple Factory pattern, in which a static method can be invoked to create connection to a given database. - -Wikipedia says - -> Factory is an object for creating other objects – formally a factory is a function or method that returns objects of a varying prototype or class. - -**Programmatic Example** - -We have an interface "Car" and tow implementations "Ford" and "Ferrari". - -```java -/** - * Car interface. - */ -public interface Car { - - public String getDescription(); - -} - -/** - * Ford implementation. - */ -public class Ford implements Car { - - static final String DESCRIPTION = "This is Ford."; - - @Override - public String getDescription() { - return DESCRIPTION; - } -} - -/** - * Ferrari implementation. - */ -public class Ferrari implements Car { - - static final String DESCRIPTION = "This is Ferrari."; - - @Override - public String getDescription() { - return DESCRIPTION; - } -} -``` - -Then we have the static method "getCar" to create car objects encapsulated in the factory class "CarSimpleFactory". - -```java -/** - * Factory of cars. - */ -public class CarSimpleFactory { - - /** - * Enumeration for different types of cars. - */ - static enum CarType { - FORD, FERRARI - } - - /** - * Factory method takes as parameter a car type and initiate the appropriate class. - */ - public static Car getCar(CarType type) { - switch (type) { - case FORD: return new Ford(); - case FERRARI: return new Ferrari(); - default: throw new IllegalArgumentException("Model not supported."); - } - } -} -``` - -Now on the client code we can create differentes types of cars(Ford or Ferrari) using the factory class. - -```java -Car car1 = CarSimpleFactory.getCar(CarSimpleFactory.CarType.FORD); -Car car2 = CarSimpleFactory.getCar(CarSimpleFactory.CarType.FERRARI); -LOGGER.info(car1.getDescription()); -LOGGER.info(car2.getDescription()); -``` - -Program output: - -```java -This is Ford. -This Ferrari. -``` -## Applicability - -Use the Simple Factory pattern when you only care about the creation of a object, not how to create and manage it. - -## Disadvantages: - -The code becomes more complicated than it should be. - -## Related patterns - -[Factory Method](https://java-design-patterns.com/patterns/factory-method/) -[Factory Kit](https://java-design-patterns.com/patterns/factory-kit/) -[Abstract Factory](https://java-design-patterns.com/patterns/abstract-factory/) - - diff --git a/simple-factory/etc/simple-factory.urm.puml b/simple-factory/etc/simple-factory.urm.puml deleted file mode 100644 index 77fee4b5b..000000000 --- a/simple-factory/etc/simple-factory.urm.puml +++ /dev/null @@ -1,35 +0,0 @@ -@startuml -package com.iluwatar.simplefactory { - class App { - - LOGGER : Logger {static} - + App() - + main(args : String[]) {static} - } - interface Car { - + getDescription() : String {abstract} - } - class CarSimpleFactory { - + CarSimpleFactory() - + getCar(type : CarType) : Car {static} - } - ~enum CarType { - + FERRARI {static} - + FORD {static} - + valueOf(name : String) : CarType {static} - + values() : CarType[] {static} - } - class Ferrari { - ~ DESCRIPTION : String {static} - + Ferrari() - + getDescription() : String - } - class Ford { - ~ DESCRIPTION : String {static} - + Ford() - + getDescription() : String - } -} -CarType ..+ CarSimpleFactory -Ferrari ..|> Car -Ford ..|> Car -@enduml \ No newline at end of file diff --git a/simple-factory/pom.xml b/simple-factory/pom.xml deleted file mode 100644 index ca7c0547d..000000000 --- a/simple-factory/pom.xml +++ /dev/null @@ -1,41 +0,0 @@ - - 4.0.0 - - com.iluwatar - java-design-patterns - 1.24.0-SNAPSHOT - - simple-factory - - - org.junit.jupiter - junit-jupiter-engine - test - - - junit - junit - - - - - - - org.apache.maven.plugins - maven-assembly-plugin - - - - - - com.iluwatar.simplefactory.App - - - - - - - - - \ No newline at end of file diff --git a/simple-factory/src/main/java/com/iluwatar/simplefactory/App.java b/simple-factory/src/main/java/com/iluwatar/simplefactory/App.java deleted file mode 100644 index f13566ba6..000000000 --- a/simple-factory/src/main/java/com/iluwatar/simplefactory/App.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * The MIT License - * Copyright © 2014-2019 Ilkka Seppälä - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -package com.iluwatar.simplefactory; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class App { - - private static final Logger LOGGER = LoggerFactory.getLogger(App.class); - - /** - * Program main entry point. - */ - public static void main(String[] args) { - Car car1 = CarSimpleFactory.getCar(CarSimpleFactory.CarType.FORD); - Car car2 = CarSimpleFactory.getCar(CarSimpleFactory.CarType.FERRARI); - LOGGER.info(car1.getDescription()); - LOGGER.info(car2.getDescription()); - } -} diff --git a/simple-factory/src/main/java/com/iluwatar/simplefactory/Car.java b/simple-factory/src/main/java/com/iluwatar/simplefactory/Car.java deleted file mode 100644 index b87070040..000000000 --- a/simple-factory/src/main/java/com/iluwatar/simplefactory/Car.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.iluwatar.simplefactory; - -/** - * Car interface. - */ -public interface Car { - - public String getDescription(); - -} diff --git a/simple-factory/src/main/java/com/iluwatar/simplefactory/CarSimpleFactory.java b/simple-factory/src/main/java/com/iluwatar/simplefactory/CarSimpleFactory.java deleted file mode 100644 index 911fe610f..000000000 --- a/simple-factory/src/main/java/com/iluwatar/simplefactory/CarSimpleFactory.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.iluwatar.simplefactory; - -/** - * Factory of cars. - */ -public class CarSimpleFactory { - - /** - * Enumeration for different types of cars. - */ - static enum CarType { - FORD, FERRARI - } - - /** - * Factory method takes as parameter a car type and initiate the appropriate class. - */ - public static Car getCar(CarType type) { - switch (type) { - case FORD: return new Ford(); - case FERRARI: return new Ferrari(); - default: throw new IllegalArgumentException("Model not supported."); - } - } -} diff --git a/simple-factory/src/main/java/com/iluwatar/simplefactory/Ferrari.java b/simple-factory/src/main/java/com/iluwatar/simplefactory/Ferrari.java deleted file mode 100644 index d6e865182..000000000 --- a/simple-factory/src/main/java/com/iluwatar/simplefactory/Ferrari.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.iluwatar.simplefactory; - -/** - * Ferrari implementation. - */ -public class Ferrari implements Car { - - static final String DESCRIPTION = "This is Ferrari."; - - @Override - public String getDescription() { - return DESCRIPTION; - } -} diff --git a/simple-factory/src/main/java/com/iluwatar/simplefactory/Ford.java b/simple-factory/src/main/java/com/iluwatar/simplefactory/Ford.java deleted file mode 100644 index 19cbea2ae..000000000 --- a/simple-factory/src/main/java/com/iluwatar/simplefactory/Ford.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.iluwatar.simplefactory; - -/** - * Ford implementation. - */ -public class Ford implements Car { - - static final String DESCRIPTION = "This is Ford."; - - @Override - public String getDescription() { - return DESCRIPTION; - } -} diff --git a/simple-factory/src/test/java/com/iluwatar/simplefactory/AppTest.java b/simple-factory/src/test/java/com/iluwatar/simplefactory/AppTest.java deleted file mode 100644 index 4465b3b8c..000000000 --- a/simple-factory/src/test/java/com/iluwatar/simplefactory/AppTest.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.iluwatar.simplefactory; - -import static org.junit.jupiter.api.Assertions.*; - -import org.junit.jupiter.api.Test; - -class AppTest { - - @Test - void shouldExecuteWithoutExceptions() { - assertDoesNotThrow(() -> App.main(new String[]{})); - } - -} diff --git a/simple-factory/src/test/java/com/iluwatar/simplefactory/CarSimpleFactoryTest.java b/simple-factory/src/test/java/com/iluwatar/simplefactory/CarSimpleFactoryTest.java deleted file mode 100644 index 394b652b6..000000000 --- a/simple-factory/src/test/java/com/iluwatar/simplefactory/CarSimpleFactoryTest.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.iluwatar.simplefactory; - -import static org.junit.jupiter.api.Assertions.*; - -import org.junit.jupiter.api.Test; - -class CarSimpleFactoryTest { - - @Test - void shouldReturnFerrariInstance() { - final var ferrari = CarSimpleFactory.getCar(CarSimpleFactory.CarType.FERRARI); - assertTrue(ferrari instanceof Ferrari); - } - -} -- GitLab