You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: abstract-factory/README.md
+34-19
Original file line number
Diff line number
Diff line change
@@ -9,13 +9,16 @@ tags:
9
9
---
10
10
11
11
## Also known as
12
+
12
13
Kit
13
14
14
15
## Intent
16
+
15
17
Provide an interface for creating families of related or dependent
16
18
objects without specifying their concrete classes.
17
19
18
20
## Explanation
21
+
19
22
Real world example
20
23
21
24
> To create a kingdom we need objects with common theme. Elven kingdom needs an Elven king, Elven castle and Elven army whereas Orcish kingdom needs an Orcish king, Orcish castle and Orcish army. There is a dependency between the objects in the kingdom.
@@ -36,9 +39,11 @@ Translating the kingdom example above. First of all we have some interfaces and
36
39
publicinterfaceCastle {
37
40
StringgetDescription();
38
41
}
42
+
39
43
publicinterfaceKing {
40
44
StringgetDescription();
41
45
}
46
+
42
47
publicinterfaceArmy {
43
48
StringgetDescription();
44
49
}
@@ -66,7 +71,7 @@ public class ElfArmy implements Army {
66
71
}
67
72
}
68
73
69
-
// Orcish implementations similarly...
74
+
// Orcish implementations similarly -> ...
70
75
71
76
```
72
77
@@ -112,9 +117,17 @@ var castle = factory.createCastle();
112
117
var king = factory.createKing();
113
118
var army = factory.createArmy();
114
119
115
-
castle.getDescription(); // Output: This is the Elven castle!
116
-
king.getDescription(); // Output: This is the Elven king!
117
-
army.getDescription(); // Output: This is the Elven Army!
120
+
castle.getDescription();
121
+
king.getDescription();
122
+
army.getDescription();
123
+
```
124
+
125
+
Program output:
126
+
127
+
```java
128
+
This is the Elven castle!
129
+
This is the Elven king!
130
+
This is the ElvenArmy!
118
131
```
119
132
120
133
Now, we can design a factory for our different kingdom factories. In this example, we created FactoryMaker, responsible for returning an instance of either ElfKingdomFactory or OrcKingdomFactory.
@@ -156,37 +169,39 @@ public static void main(String[] args) {
156
169
```
157
170
158
171
## Class diagram
172
+
159
173

160
174
161
175
162
176
## Applicability
177
+
163
178
Use the Abstract Factory pattern when
164
179
165
-
*a system should be independent of how its products are created, composed and represented
166
-
*a system should be configured with one of multiple families of products
167
-
*a family of related product objects is designed to be used together, and you need to enforce this constraint
168
-
*you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations
169
-
*the lifetime of the dependency is conceptually shorter than the lifetime of the consumer.
170
-
* you need a run-time value to construct a particular dependency
171
-
* you want to decide which product to call from a family at runtime.
172
-
* you need to supply one or more parameters only known at run-time before you can resolve a dependency.
173
-
*when you need consistency among products
174
-
*you don’t want to change existing code when adding new products or families of products to the program.
180
+
*The system should be independent of how its products are created, composed and represented
181
+
*The system should be configured with one of multiple families of products
182
+
*The family of related product objects is designed to be used together, and you need to enforce this constraint
183
+
*You want to provide a class library of products, and you want to reveal just their interfaces, not their implementations
184
+
*The lifetime of the dependency is conceptually shorter than the lifetime of the consumer.
185
+
* You need a run-time value to construct a particular dependency
186
+
* You want to decide which product to call from a family at runtime.
187
+
* You need to supply one or more parameters only known at run-time before you can resolve a dependency.
188
+
*When you need consistency among products
189
+
*You don’t want to change existing code when adding new products or families of products to the program.
175
190
176
191
## Use Cases:
177
192
178
-
*Selecting to call the appropriate implementation of FileSystemAcmeService or DatabaseAcmeService or NetworkAcmeService at runtime.
179
-
*Unit test case writing becomes much easier
193
+
*Selecting to call the appropriate implementation of FileSystemAcmeService or DatabaseAcmeService or NetworkAcmeService at runtime.
194
+
*Unit test case writing becomes much easier
180
195
* UI tools for different OS
181
196
182
197
## Consequences:
183
198
184
-
*Dependency injection in java hides the service class dependencies that can lead to runtime errors that would have been caught at compile time.
199
+
*Dependency injection in java hides the service class dependencies that can lead to runtime errors that would have been caught at compile time.
185
200
* While the pattern is great when creating predefined objects, adding the new ones might be challenging.
186
-
* The code may become more complicated than it should be, since a lot of new interfaces and classes are introduced along with the pattern.
187
-
201
+
* The code becomes more complicated than it should be, since a lot of new interfaces and classes are introduced along with the pattern.
0 commit comments