Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit588b106

Browse files
committed
Update factory example
1 parent0d41af9 commit588b106

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 orderto hide the
20-
implementationlogic andmakes client code focus on usage ratherthen initialization new objects.
19+
Providing a static method encapsulated in a class calledthefactory, to hide the implementation
20+
logic andmake client code focus on usage ratherthan initializing new objects.
2121

2222
##Explanation
2323

24-
Realworld 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-
publicinterfaceCar {
41+
publicinterfaceCoin {
4142
StringgetDescription();
4243
}
4344

44-
publicclassFordimplementsCar {
45+
publicclassGoldCoinimplementsCoin {
4546

46-
staticfinalStringDESCRIPTION="This isFord.";
47+
staticfinalStringDESCRIPTION="This isa gold coin.";
4748

4849
@Override
4950
publicStringgetDescription() {
5051
returnDESCRIPTION;
5152
}
5253
}
5354

54-
publicclassFerrariimplementsCar {
55+
publicclassCopperCoinimplementsCoin {
5556

56-
staticfinalStringDESCRIPTION="This isFerrari.";
57+
staticfinalStringDESCRIPTION="This isa copper coin.";
5758

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

65-
Enumeration above represents types ofcars that we support (`Ford` and`Ferrari`).
66+
Enumeration above represents types ofcoins that we support (`GoldCoin` and`CopperCoin`).
6667

6768
```java
68-
publicenumCarType {
69-
70-
FORD(Ford::new),
71-
FERRARI(Ferrari::new);
72-
73-
privatefinalSupplier<Car> constructor;
74-
75-
CarType(Supplier<Car> constructor) {
76-
this.constructor= constructor;
77-
}
78-
79-
publicSupplier<Car> getConstructor() {
80-
returnthis.constructor;
81-
}
69+
@RequiredArgsConstructor
70+
@Getter
71+
publicenumCoinType {
72+
73+
COPPER(CopperCoin::new),
74+
GOLD(GoldCoin::new);
75+
76+
privatefinalSupplier<Coin> constructor;
8277
}
8378
```
84-
Then we have thestatic method `getCar` to create car objects encapsulated in the factory class
85-
`CarsFactory`.
79+
80+
Then we have thestatic method `getCoin` to create coin objects encapsulated in the factory class
81+
`CoinFactory`.
8682

8783
```java
88-
publicclassCarsFactory {
89-
90-
publicstaticCargetCar(CarTypetype) {
84+
publicclassCoinFactory {
85+
86+
publicstaticCoingetCoin(CoinTypetype) {
9187
return type.getConstructor().get();
9288
}
9389
}
9490
```
9591

96-
Now on the client code we can create different types ofcars using the factory class.
92+
Now on the client code we can create different types ofcoins 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 isFord.
109-
This isFerrari.
105+
The alchemist begins his work.
106+
This is a copper coin.
107+
This is a gold coin.
110108
```
111109

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

116114
##Applicability
117115

118-
Use theSimpleFactory pattern when you only care about the creation of a object, not how to create
116+
Use thefactory 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-
interfaceCar {
8+
interfaceCoin {
99
+getDescription() :String {abstract}
1010
}
11-
classCarsFactory {
12-
+CarsFactory()
13-
+getCar(type :CarType) :Car {static}
11+
classCoinFactory {
12+
+CoinFactory()
13+
+getCoin(type :CoinType) :Coin {static}
1414
}
15-
~enumCarType {
16-
+FERRARI {static}
17-
+FORD {static}
18-
+valueOf(name :String) :CarType {static}
19-
+values() :CarType[] {static}
15+
enumCoinType {
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-
classFerrari {
23+
classCopperCoin {
2224
~DESCRIPTION : String {static}
23-
+Ferrari()
25+
+CopperCoin()
2426
+getDescription() :String
2527
}
26-
classFord {
28+
classGoldCoin {
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 examplethe CarFactoryis the factory class and it provides a static method to
34-
* create differentcars.
33+
* <p>In this examplean alchemist manufactures coins. CoinFactoryis the factory class and it
34+
*provides a static method tocreate differenttypes of coins.
3535
*/
3636

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

‎factory/src/main/java/com/iluwatar/factory/Car.javarenamed 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
packagecom.iluwatar.factory;
2525

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

3131
StringgetDescription();
3232

‎factory/src/main/java/com/iluwatar/factory/CarsFactory.javarenamed 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
packagecom.iluwatar.factory;
2525

2626
/**
27-
* Factory ofcars.
27+
* Factory ofcoins.
2828
*/
29-
publicclassCarsFactory {
29+
publicclassCoinFactory {
3030

3131
/**
32-
* Factory method takes as parametera car type andinitiate the appropriate class.
32+
* Factory method takes asaparameterthe coin type andcalls the appropriate class.
3333
*/
34-
publicstaticCargetCar(CarTypetype) {
34+
publicstaticCoingetCoin(CoinTypetype) {
3535
returntype.getConstructor().get();
3636
}
3737
}

‎factory/src/main/java/com/iluwatar/factory/CarType.javarenamed 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
importlombok.RequiredArgsConstructor;
2929

3030
/**
31-
* Enumeration for different types ofcars.
31+
* Enumeration for different types ofcoins.
3232
*/
3333
@RequiredArgsConstructor
3434
@Getter
35-
publicenumCarType {
35+
publicenumCoinType {
3636

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

40+
privatefinalSupplier<Coin>constructor;
4241
}

‎factory/src/main/java/com/iluwatar/factory/Ferrari.javarenamed 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
packagecom.iluwatar.factory;
2525

2626
/**
27-
*Ferrari implementation.
27+
*CopperCoin implementation.
2828
*/
29-
publicclassFerrariimplementsCar {
29+
publicclassCopperCoinimplementsCoin {
3030

31-
staticfinalStringDESCRIPTION ="This isFerrari.";
31+
staticfinalStringDESCRIPTION ="This isa copper coin.";
3232

3333
@Override
3434
publicStringgetDescription() {

‎factory/src/main/java/com/iluwatar/factory/Ford.javarenamed 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
packagecom.iluwatar.factory;
2525

2626
/**
27-
*Ford implementation.
27+
*GoldCoin implementation.
2828
*/
29-
publicclassFordimplementsCar {
29+
publicclassGoldCoinimplementsCoin {
3030

31-
staticfinalStringDESCRIPTION ="This isFord.";
31+
staticfinalStringDESCRIPTION ="This isa gold coin.";
3232

3333
@Override
3434
publicStringgetDescription() {

‎factory/src/test/java/com/iluwatar/factory/CarsFactoryTest.javarenamed 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
importorg.junit.jupiter.api.Test;
2929

30-
classCarsFactoryTest {
30+
classCoinFactoryTest {
3131

3232
@Test
33-
voidshouldReturnFerrariInstance() {
34-
finalvarferrari =CarsFactory.getCar(CarType.FERRARI);
35-
assertTrue(ferrariinstanceofFerrari);
33+
voidshouldReturnGoldCoinInstance() {
34+
finalvargoldCoin =CoinFactory.getCoin(CoinType.GOLD);
35+
assertTrue(goldCoininstanceofGoldCoin);
3636
}
37-
3837
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp