Skip to content

Commit a9b7111

Browse files
committed
work on Abstract Factory readme
1 parent b3bfd43 commit a9b7111

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

abstract-factory/README.md

+34-19
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ tags:
99
---
1010

1111
## Also known as
12+
1213
Kit
1314

1415
## Intent
16+
1517
Provide an interface for creating families of related or dependent
1618
objects without specifying their concrete classes.
1719

1820
## Explanation
21+
1922
Real world example
2023

2124
> 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
3639
public interface Castle {
3740
String getDescription();
3841
}
42+
3943
public interface King {
4044
String getDescription();
4145
}
46+
4247
public interface Army {
4348
String getDescription();
4449
}
@@ -66,7 +71,7 @@ public class ElfArmy implements Army {
6671
}
6772
}
6873

69-
// Orcish implementations similarly...
74+
// Orcish implementations similarly -> ...
7075

7176
```
7277

@@ -112,9 +117,17 @@ var castle = factory.createCastle();
112117
var king = factory.createKing();
113118
var army = factory.createArmy();
114119

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 Elven Army!
118131
```
119132

120133
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) {
156169
```
157170

158171
## Class diagram
172+
159173
![alt text](./etc/abstract-factory.urm.png "Abstract Factory class diagram")
160174

161175

162176
## Applicability
177+
163178
Use the Abstract Factory pattern when
164179

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

176191
## Use Cases:
177192

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
180195
* UI tools for different OS
181196

182197
## Consequences:
183198

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.
185200
* 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.
188202

189203
## Tutorial
204+
190205
* [Abstract Factory Pattern Tutorial](https://www.journaldev.com/1418/abstract-factory-design-pattern-in-java)
191206

192207

0 commit comments

Comments
 (0)