Skip to content

Commit 588b106

Browse files
committed
Update factory example
1 parent 0d41af9 commit 588b106

File tree

10 files changed

+85
-87
lines changed

10 files changed

+85
-87
lines changed

factory/README.md

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@ tags:
1616

1717
## Intent
1818

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.
2121

2222
## Explanation
2323

24-
Real world example
24+
Real-world example
2525

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.
2930
3031
Wikipedia says
3132

@@ -34,26 +35,26 @@ Wikipedia says
3435
3536
**Programmatic Example**
3637

37-
We have an interface `Car` and two implementations `Ford` and `Ferrari`.
38+
We have an interface `Coin` and two implementations `GoldCoin` and `CopperCoin`.
3839

3940
```java
40-
public interface Car {
41+
public interface Coin {
4142
String getDescription();
4243
}
4344

44-
public class Ford implements Car {
45+
public class GoldCoin implements Coin {
4546

46-
static final String DESCRIPTION = "This is Ford.";
47+
static final String DESCRIPTION = "This is a gold coin.";
4748

4849
@Override
4950
public String getDescription() {
5051
return DESCRIPTION;
5152
}
5253
}
5354

54-
public class Ferrari implements Car {
55+
public class CopperCoin implements Coin {
5556

56-
static final String DESCRIPTION = "This is Ferrari.";
57+
static final String DESCRIPTION = "This is a copper coin.";
5758

5859
@Override
5960
public String getDescription() {
@@ -62,51 +63,48 @@ public class Ferrari implements Car {
6263
}
6364
```
6465

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`).
6667

6768
```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;
8277
}
8378
```
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`.
8682

8783
```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) {
9187
return type.getConstructor().get();
9288
}
9389
}
9490
```
9591

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.
9793

9894
```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());
103100
```
104101

105102
Program output:
106103

107104
```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.
110108
```
111109

112110
## Class Diagram
@@ -115,7 +113,7 @@ This is Ferrari.
115113

116114
## Applicability
117115

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
119117
and manage it.
120118

121119
Pros
@@ -127,13 +125,13 @@ Cons
127125

128126
* The code becomes more complicated than it should be.
129127

130-
## Real world examples
128+
## Known uses
131129

132130
* [java.util.Calendar#getInstance()](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#getInstance--)
133131
* [java.util.ResourceBundle#getBundle()](https://docs.oracle.com/javase/8/docs/api/java/util/ResourceBundle.html#getBundle-java.lang.String-)
134132
* [java.text.NumberFormat#getInstance()](https://docs.oracle.com/javase/8/docs/api/java/text/NumberFormat.html#getInstance--)
135133
* [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)
137135
* [java.util.EnumSet#of()](https://docs.oracle.com/javase/8/docs/api/java/util/EnumSet.html#of(E))
138136
* [javax.xml.bind.JAXBContext#createMarshaller()](https://docs.oracle.com/javase/8/docs/api/javax/xml/bind/JAXBContext.html#createMarshaller--) and other similar methods.
139137

factory/etc/factory.urm.png

3.82 KB
Loading

factory/etc/factory.urm.puml

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,32 @@ package com.iluwatar.factory {
55
+ App()
66
+ main(args : String[]) {static}
77
}
8-
interface Car {
8+
interface Coin {
99
+ getDescription() : String {abstract}
1010
}
11-
class CarsFactory {
12-
+ CarsFactory()
13-
+ getCar(type : CarType) : Car {static}
11+
class CoinFactory {
12+
+ CoinFactory()
13+
+ getCoin(type : CoinType) : Coin {static}
1414
}
15-
~enum CarType {
16-
+ FERRARI {static}
17-
+ FORD {static}
18-
+ valueOf(name : String) : CarType {static}
19-
+ values() : CarType[] {static}
15+
enum CoinType {
16+
+ COPPER {static}
17+
+ GOLD {static}
18+
- constructor : Supplier<Coin>
19+
+ getConstructor() : Supplier<Coin>
20+
+ valueOf(name : String) : CoinType {static}
21+
+ values() : CoinType[] {static}
2022
}
21-
class Ferrari {
23+
class CopperCoin {
2224
~ DESCRIPTION : String {static}
23-
+ Ferrari()
25+
+ CopperCoin()
2426
+ getDescription() : String
2527
}
26-
class Ford {
28+
class GoldCoin {
2729
~ DESCRIPTION : String {static}
28-
+ Ford()
30+
+ GoldCoin()
2931
+ getDescription() : String
3032
}
3133
}
32-
CarType ..+ CarsFactory
33-
Ferrari ..|> Car
34-
Ford ..|> Car
34+
CopperCoin ..|> Coin
35+
GoldCoin ..|> Coin
3536
@enduml

factory/src/main/java/com/iluwatar/factory/App.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
* create and return objects of varying classes, in order to hide the implementation logic
3131
* and makes client code focus on usage rather then objects initialization and management.
3232
*
33-
* <p>In this example the CarFactory is the factory class and it provides a static method to
34-
* create different cars.
33+
* <p>In this example an alchemist manufactures coins. CoinFactory is the factory class and it
34+
* provides a static method to create different types of coins.
3535
*/
3636

3737
@Slf4j
@@ -41,9 +41,10 @@ public class App {
4141
* Program main entry point.
4242
*/
4343
public static void main(String[] args) {
44-
var car1 = CarsFactory.getCar(CarType.FORD);
45-
var car2 = CarsFactory.getCar(CarType.FERRARI);
46-
LOGGER.info(car1.getDescription());
47-
LOGGER.info(car2.getDescription());
44+
LOGGER.info("The alchemist begins his work.");
45+
var coin1 = CoinFactory.getCoin(CoinType.COPPER);
46+
var coin2 = CoinFactory.getCoin(CoinType.GOLD);
47+
LOGGER.info(coin1.getDescription());
48+
LOGGER.info(coin2.getDescription());
4849
}
4950
}

factory/src/main/java/com/iluwatar/factory/Car.java renamed to factory/src/main/java/com/iluwatar/factory/Coin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
package com.iluwatar.factory;
2525

2626
/**
27-
* Car interface.
27+
* Coin interface.
2828
*/
29-
public interface Car {
29+
public interface Coin {
3030

3131
String getDescription();
3232

factory/src/main/java/com/iluwatar/factory/CarsFactory.java renamed to factory/src/main/java/com/iluwatar/factory/CoinFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424
package com.iluwatar.factory;
2525

2626
/**
27-
* Factory of cars.
27+
* Factory of coins.
2828
*/
29-
public class CarsFactory {
29+
public class CoinFactory {
3030

3131
/**
32-
* Factory method takes as parameter a car type and initiate the appropriate class.
32+
* Factory method takes as a parameter the coin type and calls the appropriate class.
3333
*/
34-
public static Car getCar(CarType type) {
34+
public static Coin getCoin(CoinType type) {
3535
return type.getConstructor().get();
3636
}
3737
}

factory/src/main/java/com/iluwatar/factory/CarType.java renamed to factory/src/main/java/com/iluwatar/factory/CoinType.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,14 @@
2828
import lombok.RequiredArgsConstructor;
2929

3030
/**
31-
* Enumeration for different types of cars.
31+
* Enumeration for different types of coins.
3232
*/
3333
@RequiredArgsConstructor
3434
@Getter
35-
public enum CarType {
35+
public enum CoinType {
3636

37-
FORD(Ford::new),
38-
FERRARI(Ferrari::new);
39-
40-
private final Supplier<Car> constructor;
37+
COPPER(CopperCoin::new),
38+
GOLD(GoldCoin::new);
4139

40+
private final Supplier<Coin> constructor;
4241
}

factory/src/main/java/com/iluwatar/factory/Ferrari.java renamed to factory/src/main/java/com/iluwatar/factory/CopperCoin.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
package com.iluwatar.factory;
2525

2626
/**
27-
* Ferrari implementation.
27+
* CopperCoin implementation.
2828
*/
29-
public class Ferrari implements Car {
29+
public class CopperCoin implements Coin {
3030

31-
static final String DESCRIPTION = "This is Ferrari.";
31+
static final String DESCRIPTION = "This is a copper coin.";
3232

3333
@Override
3434
public String getDescription() {

factory/src/main/java/com/iluwatar/factory/Ford.java renamed to factory/src/main/java/com/iluwatar/factory/GoldCoin.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
package com.iluwatar.factory;
2525

2626
/**
27-
* Ford implementation.
27+
* GoldCoin implementation.
2828
*/
29-
public class Ford implements Car {
29+
public class GoldCoin implements Coin {
3030

31-
static final String DESCRIPTION = "This is Ford.";
31+
static final String DESCRIPTION = "This is a gold coin.";
3232

3333
@Override
3434
public String getDescription() {

factory/src/test/java/com/iluwatar/factory/CarsFactoryTest.java renamed to factory/src/test/java/com/iluwatar/factory/CoinFactoryTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,11 @@
2727

2828
import org.junit.jupiter.api.Test;
2929

30-
class CarsFactoryTest {
30+
class CoinFactoryTest {
3131

3232
@Test
33-
void shouldReturnFerrariInstance() {
34-
final var ferrari = CarsFactory.getCar(CarType.FERRARI);
35-
assertTrue(ferrari instanceof Ferrari);
33+
void shouldReturnGoldCoinInstance() {
34+
final var goldCoin = CoinFactory.getCoin(CoinType.GOLD);
35+
assertTrue(goldCoin instanceof GoldCoin);
3636
}
37-
3837
}

0 commit comments

Comments
 (0)