Chapter 3 – Page 2 Creational Pattern: If an application needs to be portable, it Abstract needs to encapsulateFactory platform dependencies. These platforms might include the operating system, the database management system, the windowing system, the web browser, etc. Frequently, this encapsulation isn’t engineered in advance, resulting in a massive proliferation of #ifdef case statements with options for all currently supported platforms.
AbstractFactory Client create platform objects directly, they create abstract objects and utilize an abstract factory as an interface for createProductA() createProductB() AbstractProductA ConcreteFactory1 createProductA() createProductB() ProductA2 ProductA1 AbstractProductB ProductB2 ProductB1 ConcreteFactory2 createProductA() createProductB() Chapter 3 – Page 3 The Abstract Rather than Factory Pattern having clients
The AnimalWorld client uses the abstract ContinentFactory to produce abstract Herbivore and Carnivore products. When the concrete platform is an AfricaFactory, concrete Wildebeest and Lion objects are created, while when the concrete platform is an AmericaFactory, concrete Bison and Wolf objects are created. ContinentFactory AnimalWorld createHerbivore() createCarnivore() Herbivore AfricaFactory createWildebeest() createLion() Bison Wildebeest Carnivore Wolf Lion AmericaFactory createBison() createWolf() Chapter 3 – Page 4 Non-Software Example
The client uses the abstract ShapeFactory to produce abstract curved and straight shapes. If the platform has been established as “simple”, concrete Circle and Square objects are created, while if the platform has been established as “robust”, concrete Ellipse and Rectangle objects are created. ShapeFactory Client createCurvedInstance() createStraightInstance() CurvedShape SimpleShapeFactory createCircle() createSquare() Ellipse Circle StraightShape Rectangle Square RobustShapeFactory createEllipse() createRectangle() Chapter 3 – Page 5 Primitive Software Example
• Since the factory only provides an abstract pointer, the client doesn’t need to include any header files or class declarations relating to the concrete type. Objects of a concrete type are created by the factory, but the client accesses such objects only through their abstract interface. • Adding new concrete types is done by modifying the client code to use a different factory (usually a one-line change in the code). This factory creates objects of a different concrete type, but still returns a pointer of the same abstract type as before, insulating the client from change, eliminating the need to change every location in the code where a new object is created. Chapter 3 – Page 9 Abstract Factory Design Advantages