|
| 1 | +--- |
| 2 | +layout: pattern |
| 3 | +title: Simple Factory |
| 4 | +folder: simple-factory |
| 5 | +permalink: /patterns/simple-factory/ |
| 6 | +categories: Creational |
| 7 | +tags: |
| 8 | + - Gang of Four |
| 9 | +--- |
| 10 | + |
| 11 | +## Also known as |
| 12 | + |
| 13 | +* 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 | +Then we have the static method "getCar" to create car objects encapsulated in the factory class "CarSimpleFactory". |
| 72 | + |
| 73 | +```java |
| 74 | +/** |
| 75 | + * Factory of cars. |
| 76 | + */ |
| 77 | +public class CarSimpleFactory { |
| 78 | + |
| 79 | + /** |
| 80 | + * Enumeration for different types of cars. |
| 81 | + */ |
| 82 | + static enum CarType { |
| 83 | + FORD, FERRARI |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Factory method takes as parameter a car type and initiate the appropriate class. |
| 88 | + */ |
| 89 | + public static Car getCar(CarType type) { |
| 90 | + switch (type) { |
| 91 | + case FORD: return new Ford(); |
| 92 | + case FERRARI: return new Ferrari(); |
| 93 | + default: throw new IllegalArgumentException("Model not supported."); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +Now on the client code we can create differentes types of cars(Ford or Ferrari) using the factory class. |
| 100 | + |
| 101 | +```java |
| 102 | +Car car1 = CarSimpleFactory.getCar(CarSimpleFactory.CarType.FORD); |
| 103 | +Car car2 = CarSimpleFactory.getCar(CarSimpleFactory.CarType.FERRARI); |
| 104 | +LOGGER.info(car1.getDescription()); |
| 105 | +LOGGER.info(car2.getDescription()); |
| 106 | +``` |
| 107 | + |
| 108 | +Program output: |
| 109 | + |
| 110 | +```java |
| 111 | +This is Ford. |
| 112 | +This Ferrari. |
| 113 | +``` |
| 114 | +## Applicability |
| 115 | + |
| 116 | +Use the Simple Factory pattern when you only care about the creation of a object, not how to create and manage it. |
| 117 | + |
| 118 | +## Disadvantages: |
| 119 | + |
| 120 | +The code becomes more complicated than it should be. |
| 121 | + |
| 122 | +## Related patterns |
| 123 | + |
| 124 | +[Factory Method](https://java-design-patterns.com/patterns/factory-method/) |
| 125 | +[Factory Kit](https://java-design-patterns.com/patterns/factory-kit/) |
| 126 | +[Abstract Factory](https://java-design-patterns.com/patterns/abstract-factory/) |
| 127 | + |
| 128 | + |
0 commit comments