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
+59-82Lines changed: 59 additions & 82 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,21 +6,25 @@ tag:
6
6
- Abstraction
7
7
- Decoupling
8
8
- Gang of Four
9
+
- Instantiation
10
+
- Polymorphism
9
11
---
10
12
11
13
## Also known as
12
14
13
-
Kit
15
+
*Kit
14
16
15
17
## Intent
16
18
17
-
The Abstract Factory design pattern provides a way to create families of related objects without specifying their concrete classes. This allows for code that is independent of the specific classes of objects it uses, promoting flexibility and maintainability.
19
+
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
18
20
19
21
## Explanation
20
22
21
23
Real-world example
22
24
23
-
> 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.
25
+
> Imagine a furniture company that produces various styles of furniture: modern, Victorian, and rustic. Each style includes products like chairs, tables, and sofas. To ensure consistency within each style, the company uses an Abstract Factory pattern.
26
+
>
27
+
> In this scenario, the Abstract Factory is an interface for creating families of related furniture objects (chairs, tables, sofas). Each concrete factory (ModernFurnitureFactory, VictorianFurnitureFactory, RusticFurnitureFactory) implements the Abstract Factory interface and creates a set of products that match the specific style. This way, clients can create a whole set of modern or Victorian furniture without worrying about the details of their instantiation. This maintains a consistent style and allows easy swapping of one style of furniture for another.
24
28
25
29
In plain words
26
30
@@ -32,6 +36,8 @@ Wikipedia says
32
36
33
37
**Programmatic Example**
34
38
39
+
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.
40
+
35
41
Translating the kingdom example above. First of all, we have some interfaces and implementation for the objects in the kingdom.
36
42
37
43
```java
@@ -76,7 +82,6 @@ public class ElfArmy implements Army {
76
82
}
77
83
78
84
// Orcish implementations similarly -> ...
79
-
80
85
```
81
86
82
87
Then we have the abstraction and implementations for the kingdom factory.
@@ -108,44 +113,7 @@ public class ElfKingdomFactory implements KingdomFactory {
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.
131
-
132
-
```java
133
-
var factory=newElfKingdomFactory();
134
-
var castle=factory.createCastle();
135
-
var king=factory.createKing();
136
-
var army=factory.createArmy();
137
-
138
-
castle.getDescription();
139
-
king.getDescription();
140
-
army.getDescription();
141
-
```
142
-
143
-
Program output:
144
-
145
-
```java
146
-
This is the elven castle!
147
-
This is the elven king!
148
-
This is the elven Army!
116
+
// Orcish implementations similarly -> ...
149
117
```
150
118
151
119
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`. The client can use `FactoryMaker` to create the desired concrete factory which, in turn, will produce different concrete objects (derived from `Army`, `King`, `Castle`). In this example, we also used an enum to parameterize which type of kingdom factory the client will ask for.
@@ -164,50 +132,62 @@ public static class FactoryMaker {
164
132
};
165
133
}
166
134
}
135
+
```
167
136
168
-
publicstaticvoid main(String[] args) {
169
-
var app =newApp();
137
+
Here is the main function of our example application:
*[Factory Method](https://java-design-patterns.com/patterns/factory-method/): Abstract Factory uses Factory Methods to create products.
217
+
*[Singleton](https://java-design-patterns.com/patterns/singleton/): Abstract Factory classes are often implemented as Singletons.
218
+
*[Factory Kit](https://java-design-patterns.com/patterns/factory-kit/): Similar to Abstract Factory but focuses on configuring and managing a set of related objects in a flexible way.
241
219
242
220
## Credits
243
221
244
-
*[Design Patterns: Elements of Reusable Object-Oriented Software](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59)
245
-
*[Head First Design Patterns: A Brain-Friendly Guide](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b)
246
-
*[Java Design Patterns: A Hands-On Experience with Real-World Examples](https://amzn.to/3HWNf4U)
222
+
*[Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
247
223
*[Design Patterns in Java](https://amzn.to/3Syw0vC)
248
-
*
224
+
*[Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software](https://amzn.to/49NGldq)
225
+
*[Java Design Patterns: A Hands-On Experience with Real-World Examples](https://amzn.to/3HWNf4U)
0 commit comments