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
> "The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes"
78
+
79
+
<br/>
80
+
81
+
* In plain words:
82
+
* A factory that groups individual but related/dependent factories together without specifying their concrete classes;
83
+
* A factory of factories;
84
+
85
+
---
86
+
87
+
# Example
88
+
89
+
* In a factory that creates kingdoms, we need objects with common theme:
90
+
91
+
* Elven kingdom needs an Elven king, Elven castle and Elven army;
92
+
93
+
* Orcish kingdom needs an Orcish king, Orcish castle and Orcish army;
94
+
95
+
<br/>
96
+
97
+
* There is a dependency between the objects in the kingdom;
98
+
99
+
---
100
+
101
+
# Diagram
102
+
103
+
* Based on the kingdom example, the diagram below showcases the different concrete factories and their concrete products:
104
+
105
+
.center[]
106
+
107
+
108
+
---
109
+
110
+
# Diagram
111
+
112
+
* The class diagram below showcases the factory of factories;
113
+
114
+
* At runtime, we can define which Kingdom type is needed and pass it as a parameter to define which concrete KingdomFactory to instantiate;
115
+
116
+
* The concrete factory returned will then be able to produce the related objects of the specified type;
117
+
118
+
.center[]
119
+
120
+
---
121
+
122
+
# Applicability
123
+
124
+
Use the Abstract Factory pattern when:
125
+
126
+
* A system should be independent of how its products are created, composed and represented;
127
+
128
+
* A system should be configured with one of multiple families of products;
129
+
130
+
* A family of related product objects is designed to be used together, and you need to enforce this constraint;
131
+
132
+
* You want to provide a class library of products, and you want to reveal just their interfaces, not their implementations;
133
+
134
+
---
135
+
136
+
# Applicability
137
+
138
+
Use the Abstract Factory pattern when:
139
+
140
+
* The lifetime of the dependency is conceptually shorter than the lifetime of the consumer;
141
+
142
+
* You need a run-time value to construct a particular dependency;
143
+
144
+
* You want to decide which product to call from a family at runtime;
145
+
146
+
* You need to supply one or more parameters only known at run-time before you can resolve a dependency;
147
+
148
+
---
149
+
150
+
#Use Cases
151
+
152
+
* Selecting to call the appropriate implementation of FileSystemAcmeService or DatabaseAcmeService or NetworkAcmeService at runtime;
153
+
* Unit test case writing becomes much easier;
154
+
155
+
---
156
+
157
+
# Consequences
158
+
159
+
* Dependency injection in java hides the service class dependencies that can lead to runtime errors that would have been caught at compile time
* [Design Patterns: Elements of Reusable Object-Oriented Software](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612)
0 commit comments