Abstract Factory pattern
1.Brief:
It defines an interface Factory
for a family of products which can be considered as an abstract factory. There are concrete classes like xxxFactory
implement Factory
. Each xxxFactory
provides operations of a specific type of family of products.
2.What participates in this design pattern?
-
AbstractFactory
-
- declares an interface for operations that create abstract product objects.
-
ConcreteFactory
-
- implements the operations to create concrete product objects.
-
AbstractProduct
-
- declares an interface for a type of product object.
-
ConcreteProduct
-
- defines a product object to be created by the corresponding concrete factory.
- implements the AbstractProduct interface.
-
Client
-
- uses only interfaces declared by AbstractFactory and AbstractProduct classes.
3.Example:
The following is composed of one interface GUI
and two concrete classes WinGUI
and MacGUI
. GUI
defines two method createButton
and createCheckbox
. Then,two concrete factories implement them in their onw way and return their own type of products.
The following pic denotes the family of products and their method
Then, create a Client
class Application{
private Factory factory;
private Button button;
private Checkbox checkbox;
public Application(Factory inputFactory){
this.factory = inputFactory;
this.button = inputFactory.createButton();
this.checkbox = inputFactory.createCheckbox();
}
}
Now if we want to create an Application
public static void main{
Factory winfac = new WinFactory();
Factory macfac = new MacFactory();
// if we want to create a Win app
Application winApp = new Application(winfac);
// if we want to create a Mac app
Application macApp = new Application(macfac);
}
4.Where can we apply this design pattern?
I pick the description from GoF as my experience can not help me summarize them well.
Summary:
- a system should be independent of how its products are created, composed, and represented.
- a system should be configured with one of multiple families of products.
- a family of related product objects is designed to be used together, and you need to enforce this constraint.
- you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.
5.What are the advantages and disadvantages of this design pattern?
Advantages
- It isolates the concrete classes as the factory class encapsulates the responsibility and process of creation of products which will not appear in the client code
- It makes exchanging products families easily as it can use different product configurations simply by changing the concrete factory
- It promotes consistency among products as it makes sure than products all come from the same family
Disadvantages
- Supporting new kinds of products is difficult which involves changing the AbstractFactory class and all of its subclasses
- There are too many classes and interfaces
bstractFactory class and all of its subclasses
- There are too many classes and interfaces