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
+16-16Lines changed: 16 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -20,9 +20,9 @@ objects without specifying their concrete classes.
20
20
21
21
## Explanation
22
22
23
-
Realworld example
23
+
Real-world example
24
24
25
-
> To create a kingdom we need objects with a 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.
25
+
> To create a kingdom we need objects with a common theme. The elven kingdom needs an elven king, elven castle, and elven army whereas the orcish kingdom needs an orcish king, orcish castle, and orcish army. There is a dependency between the objects in the kingdom.
26
26
27
27
In plain words
28
28
@@ -34,7 +34,7 @@ Wikipedia says
34
34
35
35
**Programmatic Example**
36
36
37
-
Translating the kingdom example above. First of all we have some interfaces and implementation for the objects in the
37
+
Translating the kingdom example above. First of all, we have some interfaces and implementation for the objects in the
38
38
kingdom.
39
39
40
40
```java
@@ -52,21 +52,21 @@ public interface Army {
52
52
53
53
// Elven implementations ->
54
54
publicclassElfCastleimplementsCastle {
55
-
staticfinalStringDESCRIPTION="This is the Elven castle!";
55
+
staticfinalStringDESCRIPTION="This is the elven castle!";
56
56
@Override
57
57
publicStringgetDescription() {
58
58
returnDESCRIPTION;
59
59
}
60
60
}
61
61
publicclassElfKingimplementsKing {
62
-
staticfinalStringDESCRIPTION="This is the Elven king!";
62
+
staticfinalStringDESCRIPTION="This is the elven king!";
63
63
@Override
64
64
publicStringgetDescription() {
65
65
returnDESCRIPTION;
66
66
}
67
67
}
68
68
publicclassElfArmyimplementsArmy {
69
-
staticfinalStringDESCRIPTION="This is the Elven Army!";
69
+
staticfinalStringDESCRIPTION="This is the elven Army!";
70
70
@Override
71
71
publicStringgetDescription() {
72
72
returnDESCRIPTION;
@@ -77,7 +77,7 @@ public class ElfArmy implements Army {
77
77
78
78
```
79
79
80
-
Then we have the abstraction and implementations for the kingdom factory
80
+
Then we have the abstraction and implementations for the kingdom factory.
81
81
82
82
```java
83
83
publicinterfaceKingdomFactory {
@@ -111,7 +111,7 @@ public class OrcKingdomFactory implements KingdomFactory {
111
111
}
112
112
```
113
113
114
-
Now we have our abstract factory that lets us make family of related objects i.e. Elven kingdom factory creates Elven castle, king and army etc.
114
+
Now we have the abstract factory that lets us make a family of related objects i.e. elven kingdom factory creates elven castle, king and army, etc.
115
115
116
116
```java
117
117
var factory =newElfKingdomFactory();
@@ -127,13 +127,13 @@ army.getDescription();
127
127
Program output:
128
128
129
129
```java
130
-
This is the Elven castle!
131
-
This is the Elven king!
132
-
This is the ElvenArmy!
130
+
This is the elven castle!
131
+
This is the elven king!
132
+
This is the elvenArmy!
133
133
```
134
134
135
-
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.
136
-
The client can use FactoryMaker to create the desired concrete factory which, in turn, will produce different concrete objects (Army, King, Castle).
135
+
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`.
136
+
The client can use `FactoryMaker` to create the desired concrete factory which, in turn, will produce different concrete objects (derived from `Army`, `King`, `Castle`).
137
137
In this example, we also used an enum to parameterize which type of kingdom factory the client will ask for.
138
138
139
139
```java
@@ -179,8 +179,8 @@ public static void main(String[] args) {
179
179
180
180
Use the Abstract Factory pattern when
181
181
182
-
* The system should be independent of how its products are created, composed and represented
183
-
* The system should be configured with one of multiple families of products
182
+
* The system should be independent of how its products are created, composed, and represented
183
+
* The system should be configured with one of the multiple families of products
184
184
* The family of related product objects is designed to be used together, and you need to enforce this constraint
185
185
* You want to provide a class library of products, and you want to reveal just their interfaces, not their implementations
186
186
* The lifetime of the dependency is conceptually shorter than the lifetime of the consumer.
@@ -200,7 +200,7 @@ Example use cases
200
200
201
201
* Dependency injection in java hides the service class dependencies that can lead to runtime errors that would have been caught at compile time.
202
202
* While the pattern is great when creating predefined objects, adding the new ones might be challenging.
203
-
* The code becomes more complicated than it should be, since a lot of new interfaces and classes are introduced along with the pattern.
203
+
* 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