8000 Merge branch 'master' into java9 · sithuhlaing/java-design-patterns@e1d8266 · GitHub
[go: up one dir, main page]

Skip to content

Commit e1d8266

Browse files
committed
Merge branch 'master' into java9
2 parents 54d8ec9 + a745c5d commit e1d8266

File tree

118 files changed

+2910
-2448
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+2910
-2448
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ datanucleus.log
1717
/bin/
1818
*.log
1919
data-mapper/src/main/resources/log4j.xml
20+
event-sourcing/Journal.json

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ notifications:
3636
on_failure: always # options: [always|never|change] default: always
3737
on_start: never # options: [always|never|change] default: always
3838

39-
sudo: false # route the build to the container-based infrastructure for a faster build
39+
sudo: required

_scripts/postPumlsToServer.firstrun.output

Lines changed: 0 additions & 190 deletions
This file was deleted.

abstract-factory/README.md

Lines changed: 101 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,107 @@ Kit
1818
Provide an interface for creating families of related or dependent
1919
objects without specifying their concrete classes.
2020

21-
![alt text](./etc/abstract-factory_1.png "Abstract Factory")
21+
## Explanation
22+
Real world example
23+
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.
25+
26+
In plain words
27+
28+
> A factory of factories; a factory that groups the individual but related/dependent factories together without specifying their concrete classes.
29+
30+
Wikipedia says
31+
32+
> The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes
33+
34+
**Programmatic Example**
35+
36+
Translating the kingdom example above. First of all we have some interfaces and implementation for the objects in the kingdom
37+
38+
```
39+
public interface Castle {
40+
String getDescription();
41+
}
42+
public interface King {
43+
String getDescription();
44+
}
45+
public interface Army {
46+
String getDescription();
47+
}
48+
49+
// Elven implementations ->
50+
public class ElfCastle implements Castle {
51+
static final String DESCRIPTION = "This is the Elven castle!";
52+
@Override
53+
public String getDescription() {
54+
return DESCRIPTION;
55+
}
56+
}
57+
public class ElfKing implements King {
58+
static final String DESCRIPTION = "This is the Elven king!";
59+
@Override
60+
public String getDescription() {
61+
return DESCRIPTION;
62+
}
63+
}
64+
public class ElfArmy implements Army {
65+
static final String DESCRIPTION = "This is the Elven Army!";
66+
@Override
67+
public String getDescription() {
68+
return DESCRIPTION;
69+
}
70+
}
71+
72+
// Orcish implementations similarly...
73+
74+
```
75+
76+
Then we have the abstraction and implementations for the kingdom factory
77+
78+
```
79+
public interface KingdomFactory {
80+
Castle createCastle();
81+
King createKing();
82+
Army createArmy();
83+
}
84+
85+
public class ElfKingdomFactory implements KingdomFactory {
86+
public Castle createCastle() {
87+
return new ElfCastle();
88+
}
89+
public King createKing() {
90+
return new ElfKing();
91+
}
92+
public Army createArmy() {
93+
return new ElfArmy();
94+
}
95+
}
96+
97+
public class OrcKingdomFactory implements KingdomFactory {
98+
public Castle createCastle() {
99+
return new OrcCastle();
100+
}
101+
public King createKing() {
102+
return new OrcKing();
103+
}
104+
public Army createArmy() {
105+
return new OrcArmy();
106+
}
107+
}
108+
```
109+
110+
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.
111+
112+
```
113+
KingdomFactory factory = new ElfKingdomFactory();
114+
Castle castle = factory.createCastle();
115+
King king = factory.createKing();
116+
Army army = factory.createArmy();
117+
118+
castle.getDescription(); // Output: This is the Elven castle!
119+
king.getDescription(); // Output: This is the Elven king!
120+
army.getDescription(); // Output: This is the Elven Army!
121+
```
22122

23123
## Applicability
24124
Use the Abstract Factory pattern when
@@ -41,9 +141,6 @@ Use the Abstract Factory pattern when
41141

42142
* Dependency injection in java hides the service class dependencies that can lead to runtime errors that would have been caught at compile time.
43143

44-
45-
46-
47144
## Real world examples
48145

49146
* [javax.xml.parsers.DocumentBuilderFactory](http://docs.oracle.com/javase/8/docs/api/javax/xml/parsers/DocumentBuilderFactory.html)
-21.1 KB
Binary file not shown.

abstract-factory/etc/abstract-factory.ucls

Lines changed: 0 additions & 159 deletions
This file was deleted.

abstract-factory/etc/abstract-factory.urm.puml

Lines changed: 0 additions & 89 deletions
This file was deleted.
-57 KB
Binary file not shown.

0 commit comments

Comments
 (0)
0