Definition:
- Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
- A hierarchy that encapsulates: many possible “platforms”, and the construction of a suite of “products”.
- The new operator considered harmful.
Where to use & benefits
- Creates families of related or dependent objects like Kit.
- Provides a class library of products, exposing interface not implementation.
- Needs to isolate concrete classes from their super classes.
- A system needs independent of how its products are created, composed, and represented.
- Try to enforce a constraint.
- An alternative to Facade to hide platform-specific classe.
- Easily extensible to a system or a family.
- Related patterns include.
- Factory method, which is often implemented with an abstract factory.
- Singleton, which is often implemented with an abstract factory.
- Prototype, which is often implemented with an abstract factory.
- Facade, which is often used with an abstract factory by providing an interface for creating implementing class.
This pattern is one level of abstraction higher than factory pattern. This means that the abstract factory returns the factory of classes. Like Factory pattern returned one of the several sub-classes, this returns such factory which later will return one of the sub-classes.
Working with Abstract Factory Design Pattern
The classes that participate to the Abstract Factory pattern are:
AbstractFactory - declares a interface for operations that create abstract products.
ConcreteFactory - implements operations to create concrete products.
AbstractProduct - declares an interface for a type of product objects.
Product - defines a product to be created by the corresponding ConcreteFactory; it implements the AbstractProduct interface.
Client - uses the interfaces declared by the AbstractFactory and AbstractProduct classes.
Working with Abstract Factory Design Pattern
The classes that participate to the Abstract Factory pattern are:
AbstractFactory - declares a interface for operations that create abstract products.
ConcreteFactory - implements operations to create concrete products.
AbstractProduct - declares an interface for a type of product objects.
Product - defines a product to be created by the corresponding ConcreteFactory; it implements the AbstractProduct interface.
Client - uses the interfaces declared by the AbstractFactory and AbstractProduct classes.
The classic implementation for the Abstract Factory pattern is the following:
1 | package com.kota.java; |
No comments:
Post a Comment