@@ -16,16 +16,17 @@ tags:
16
16
17
17
## Intent
18
18
19
- Providing a static method encapsulated in a class called factory, in order to hide the
20
- implementation logic and makes client code focus on usage rather then initialization new objects.
19
+ Providing a static method encapsulated in a class called the factory, to hide the implementation
20
+ logic and make client code focus on usage rather than initializing new objects.
21
21
22
22
## Explanation
23
23
24
- Real world example
24
+ Real- world example
25
25
26
- > Lets say we have a web application connected to SQLServer, but now we want to switch to Oracle. To
27
- > do so without modifying existing source code, we need to implements Simple Factory pattern, in
28
- > which a static method can be invoked to create connection to a given database.
26
+ > Imagine an alchemist who is about to manufacture coins. The alchemist must be able to create both
27
+ > gold and copper coins and switching between them must be possible without modifying the existing
28
+ > source code. The factory pattern makes it possible by providing a static construction method which
29
+ > can be called with relevant parameters.
29
30
30
31
Wikipedia says
31
32
@@ -34,26 +35,26 @@ Wikipedia says
34
35
35
36
** Programmatic Example**
36
37
37
- We have an interface ` Car ` and two implementations ` Ford ` and ` Ferrari ` .
38
+ We have an interface ` Coin ` and two implementations ` GoldCoin ` and ` CopperCoin ` .
38
39
39
40
``` java
40
- public interface Car {
41
+ public interface Coin {
41
42
String getDescription ();
42
43
}
43
44
44
- public class Ford implements Car {
45
+ public class GoldCoin implements Coin {
45
46
46
- static final String DESCRIPTION = " This is Ford ." ;
47
+ static final String DESCRIPTION = " This is a gold coin ." ;
47
48
48
49
@Override
49
50
public String getDescription () {
50
51
return DESCRIPTION ;
51
52
}
52
53
}
53
54
54
- public class Ferrari implements Car {
55
+ public class CopperCoin implements Coin {
55
56
56
- static final String DESCRIPTION = " This is Ferrari ." ;
57
+ static final String DESCRIPTION = " This is a copper coin ." ;
57
58
58
59
@Override
59
60
public String getDescription () {
@@ -62,51 +63,48 @@ public class Ferrari implements Car {
62
63
}
63
64
```
64
65
65
- Enumeration above represents types of cars that we support (` Ford ` and ` Ferrari ` ).
66
+ Enumeration above represents types of coins that we support (` GoldCoin ` and ` CopperCoin ` ).
66
67
67
68
``` java
68
- public enum CarType {
69
-
70
- FORD (Ford :: new ),
71
- FERRARI (Ferrari :: new );
72
-
73
- private final Supplier<Car > constructor;
74
-
75
- CarType(Supplier<Car > constructor) {
76
- this . constructor = constructor;
77
- }
78
-
79
- public Supplier<Car > getConstructor() {
80
- return this . constructor;
81
- }
69
+ @RequiredArgsConstructor
70
+ @Getter
71
+ public enum CoinType {
72
+
73
+ COPPER (CopperCoin :: new ),
74
+ GOLD (GoldCoin :: new );
75
+
76
+ private final Supplier<Coin > constructor;
82
77
}
83
78
```
84
- Then we have the static method `getCar` to create car objects encapsulated in the factory class
85
- `CarsFactory `.
79
+
80
+ Then we have the static method `getCoin` to create coin objects encapsulated in the factory class
81
+ `CoinFactory `.
86
82
87
83
```java
88
- public class CarsFactory {
89
-
90
- public static Car getCar ( CarType type ) {
84
+ public class CoinFactory {
85
+
86
+ public static Coin getCoin ( CoinType type ) {
91
87
return type. getConstructor(). get();
92
88
}
93
89
}
94
90
```
95
91
96
- Now on the client code we can create different types of cars using the factory class.
92
+ Now on the client code we can create different types of coins using the factory class.
97
93
98
94
```java
99
- var car1 = CarsFactory . getCar(CarType . FORD );
100
- var car2 = CarsFactory . getCar(CarType . FERRARI );
101
- LOGGER . info(car1. getDescription());
102
- LOGGER . info(car2. getDescription());
95
+ LOGGER . info(" The alchemist begins his work." );
96
+ var coin1 = CoinFactory . getCoin(CoinType . COPPER );
97
+ var coin2 = CoinFactory . getCoin(CoinType . GOLD );
98
+ LOGGER . info(coin1. getDescription());
99
+ LOGGER . info(coin2. getDescription());
103
100
```
104
101
105
102
Program output:
106
103
107
104
```java
108
- This is Ford .
109
- This is Ferrari .
105
+ The alchemist begins his work.
106
+ This is a copper coin.
107
+ This is a gold coin.
110
108
```
111
109
112
110
## Class Diagram
@@ -115,7 +113,7 @@ This is Ferrari.
115
113
116
114
## Applicability
117
115
118
- Use the Simple Factory pattern when you only care about the creation of a object, not how to create
116
+ Use the factory pattern when you only care about the creation of a object, not how to create
119
117
and manage it.
120
118
121
119
Pros
@@ -127,13 +125,13 @@ Cons
127
125
128
126
* The code becomes more complicated than it should be.
129
127
130
- ## Real world examples
128
+ ## Known uses
131
129
132
130
* [java.util. Calendar #getInstance()](https: // docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#getInstance--)
133
131
* [java.util. ResourceBundle #getBundle()](https: // docs.oracle.com/javase/8/docs/api/java/util/ResourceBundle.html#getBundle-java.lang.String-)
134
132
* [java.text. NumberFormat #getInstance()](https: // docs.oracle.com/javase/8/docs/api/java/text/NumberFormat.html#getInstance--)
135
133
* [java.nio.charset. Charset #forName()](https: // docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html#forName-java.lang.String-)
136
- * [java.net. URLStreamHandlerFactory #createURLStreamHandler(String )](https: // docs.oracle.com/javase/8/docs/api/java/net/URLStreamHandlerFactory.html) (Returns different singleton objects, depending on a protocol)
134
+ * [java.net. URLStreamHandlerFactory #createURLStreamHandler(String )](https: // docs.oracle.com/javase/8/docs/api/java/net/URLStreamHandlerFactory.html) (returns different singleton objects, depending on a protocol)
137
135
* [java.util. EnumSet #of()](https: // docs.oracle.com/javase/8/docs/api/java/util/EnumSet.html#of(E))
138
136
* [javax.xml.bind. JAXBContext #createMarshaller()](https: // docs.oracle.com/javase/8/docs/api/javax/xml/bind/JAXBContext.html#createMarshaller--) and other similar methods.
139
137
0 commit comments