|
| 1 | +--- |
| 2 | +layout: pattern |
| 3 | +title: Factory |
| 4 | +folder: factory |
| 5 | +permalink: /patterns/factory/ |
| 6 | +categories: Creational |
| 7 | +tags: |
| 8 | + - Gang of Four |
| 9 | +--- |
| 10 | + |
| 11 | +## Also known as |
| 12 | + |
| 13 | +* Simple Factory |
| 14 | +* Static Factory Method |
| 15 | + |
| 16 | +## Intent |
| 17 | + |
| 18 | +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. |
| 19 | + |
| 20 | +## Explanation |
| 21 | + |
| 22 | +Real world example |
| 23 | + |
| 24 | +> 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. |
| 25 | +
|
| 26 | +Wikipedia says |
| 27 | + |
| 28 | +> 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. |
| 29 | +
|
| 30 | +**Programmatic Example** |
| 31 | + |
| 32 | +We have an interface 'Car' and tow implementations 'Ford' and 'Ferrari'. |
| 33 | + |
| 34 | +```java |
| 35 | +/** |
| 36 | + * Car interface. |
| 37 | + */ |
| 38 | +public interface Car { |
| 39 | + |
| 40 | + public String getDescription(); |
| 41 | + |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Ford implementation. |
| 46 | + */ |
| 47 | +public class Ford implements Car { |
| 48 | + |
| 49 | + static final String DESCRIPTION = "This is Ford."; |
| 50 | + |
| 51 | + @Override |
| 52 | + public String getDescription() { |
| 53 | + return DESCRIPTION; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Ferrari implementation. |
| 59 | + */ |
| 60 | +public class Ferrari implements Car { |
| 61 | + |
| 62 | + static final String DESCRIPTION = "This is Ferrari."; |
| 63 | + |
| 64 | + @Override |
| 65 | + public String getDescription() { |
| 66 | + return DESCRIPTION; |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +Enumeration above represents types of cars that we support (Ford and Ferrari). |
| 72 | + |
| 73 | +```java |
| 74 | +public enum CarType { |
| 75 | + |
| 76 | + /** |
| 77 | + * Enumeration for different types of cars. |
| 78 | + */ |
| 79 | + FORD(Ford::new), |
| 80 | + FERRARI(Ferrari::new); |
| 81 | + |
| 82 | + private final Supplier<Car> constructor; |
| 83 | + |
| 84 | + CarType(Supplier<Car> constructor) { |
| 85 | + this.constructor = constructor; |
| 86 | + } |
| 87 | + |
| 88 | + public Supplier<Car> getConstructor() { |
| 89 | + return this.constructor; |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
| 93 | +Then we have the static method 'getCar' to create car objects encapsulated in the factory class 'CarSimpleFactory'. |
| 94 | + |
| 95 | +```java |
| 96 | +/** |
| 97 | + * Factory of cars. |
| 98 | + */ |
| 99 | +public class CarsFactory { |
| 100 | + |
| 101 | + /** |
| 102 | + * Factory method takes as parameter a car type and initiate the appropriate class. |
| 103 | + */ |
| 104 | + public static Car getCar(CarType type) { |
| 105 | + return type.getConstructor().get(); |
| 106 | + } |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +Now on the client code we can create different types of cars using the factory class. |
| 111 | + |
| 112 | +```java |
| 113 | +var car1 = CarsFactory.getCar(CarType.FORD); |
| 114 | +var car2 = CarsFactory.getCar(CarType.FERRARI); |
| 115 | +LOGGER.info(car1.getDescription()); |
| 116 | +LOGGER.info(car2.getDescription());; |
| 117 | +``` |
| 118 | + |
| 119 | +Program output: |
| 120 | + |
| 121 | +```java |
| 122 | +This is Ford. |
| 123 | +This Ferrari. |
| 124 | +``` |
| 125 | +## Class Diagram |
| 126 | + |
| 127 | + |
| 128 | +## Applicability |
| 129 | +Use the Simple Factory pattern when you only care about the creation of a object, not how to create and manage it. |
| 130 | + |
| 131 | +## Pros |
| 132 | +* Allows keeping all objects creation in one place and avoid of spreading 'new' key value across codebase. |
| 133 | +* Allows to writs loosely coupled code. Some of its main advantages include better testability, easy-to-understand code, swappable components, scalability and isolated features. |
| 134 | + |
| 135 | +## Cons |
| 136 | +* The code becomes more complicated than it should be. |
| 137 | + |
| 138 | +## Related patterns |
| 139 | + |
| 140 | +[Factory Method](https://java-design-patterns.com/patterns/factory-method/) |
| 141 | +[Factory Kit](https://java-design-patterns.com/patterns/factory-kit/) |
| 142 | +[Abstract Factory](https://java-design-patterns.com/patterns/abstract-factory/) |
| 143 | + |
| 144 | + |
0 commit comments