Implementation of Abstract Factory (Creational) Design Pattern
Implementation of Abstract Factory (Creational) Design Pattern
1
Types of Design Pattern
1. Creational design patterns
This design patterns is help to make a system independent of how its
objects are created, composed, and represented.
Abstract Factory
Builder
Factory Method
Object Pool
Prototype
Singleton
2
Abstract Factory Design Pattern
Intent
Problem
Motivation
This mechanism makes exchanging product families easy because the specific
class of the factory object appears only once in the application - where it is
instantiated. The application can wholesale replace the entire family of products
simply by instantiating a different concrete instance of the abstract factory.
Applicability
It isolates concrete classes. The Abstract Factory pattern helps you control
the classes of objects that an application creates. Because a factory
encapsulates the responsibility and the process of creating product objects,
it isolates clients from implementation classes. Clients manipulate
instances through their abstract interfaces. Product class names are
isolated in the implementation of the concrete factory; they do not appear
in client code.
It makes exchanging product families easy. The class of a concrete factory
appears only once in an application---that is, where it's instantiated. This
makes it easy to change the concrete factory an application uses. It can
use different product configurations simply by changing the concrete
3
factory. Because an abstract factory creates a complete family of products,
the whole product family changes at once. In our user interface example,
we can switch from Motif widgets to Presentation Manager widgets simply
by switching the corresponding factory objects and recreating the
interface.
It promotes consistency among products. When product objects in a family
are designed to work together, it's important that an application use
objects from only one family at a time. AbstractFactory makes this easy to
enforce.
Factory methods eliminate the need to bind application-specific classes
into your code.
The code only deals with the product interfaces; therefore, it can work
with any user-defined concrete product classes.
Structure
4
Class Diagram
In Abstract Factory pattern an interface is responsible for creating a factory of
related objects without explicitly specifying their classes. Each generated factory
can give the objects as per the Factory pattern. We are going to create a Shape
and Color interfaces and concrete classes implementing these interfaces. We
create an abstract factory class AbstractFactory as next step. Factory classes
ShapeFactory and ColorFactory are defined where each factory extends
AbstractFactory. A factory creator/generator class FactoryProducer is created.