2-DP
2-DP
UNIT-II
Abstract Factory(creational pattern)
• Abstract Factory patterns work around a
super-factory which creates other factories.
This factory is also called as factory of
factories. This type of design pattern comes
under creational pattern as this pattern
provides one of the best ways to create an
object.
What is Abstract Factory Pattern?
• Abstract factory pattern falls into “Creational
design pattern” category. Abstract factory
design pattern is used to mange different
object types of same family.
• All the object should belong to same family
but they are of different categories.
• Pattern name &classification: Abstract factory
& Creational Pattern
• Intent: Provide an interface for creating
families of related or dependent objects
without specifying their concrete classes.
• Also known as: Kit
• Motivation:
Extends key Generalization
impliments realization
Applicability
We should use Abstract Factory design pattern when:
• The system needs to be independent of the products it
works with are created.
• The system should be configured to work with
multiple families of products.
• A family of products is designed to be used together
and this constraint is needed to be enforced.
• A library of products is to be provided and only their
interfaces are to be revealed but not their
implementations.
Structure
• AbstractFactory - This class declares an
interface that is used to create abstract
product objects. We interact with this
interface to allow the client to access
instances of the our ConcreteFactory concrete
subclasses.
• ConcreteFactory - A subclass of the Abstract
factory, these concrete classes implement the
operations which are used to create concrete
product objects. Each Factory has it’s own
individual family of products which it creates.
• AbstractProduct - The AbstractProduct class is used to
declare an interface for a specific type of product
object.
• ConcreteProduct - The concrete product is provided by
it’s ConcreteFactory and defines the individual
implementation details for the specified product
through the implementation of its AbstractProduct
interface.
• Client - Only uses and has access to the interfaces that
are defined by the AbstractFactory and
AbstractProduct classes, making it completely unaware
of the implementation details of the system.
• Collaborations
➢ The ConcreteFactory class creates products
objects having a particular implementation. To
create different product, the client should use
a different concrete factory.
➢AbstractFactory defers creation of product
objects to its ConcreteFactory subclass
Implementation
1. Declare abstract product interfaces for all product
types. Then make all concrete product classes
implement these interfaces.
2. Declare the abstract factory interface with a set of
creation methods for all abstract products.
3. Implement a set of concrete factory classes, one for
each product variant.
4. Create factory initialization code somewhere in the
app. It should instantiate one of the concrete factory
classes, depending on the application configuration or
the current environment. Pass this factory object to all
classes that construct products.
5. Scan through the code and find all direct calls to
product constructors. Replace them with calls to the
appropriate creation method on the factory object.
Consequences
• It isolates concrete classes.
• It makes exchanging product families easy.
• It creates consistency among products
Sample Code
AbstractFactory.java
public abstract class AbstractFactory
{
abstract Shape getShape(String shapeType) ;
}
• Shape.java
public interface Shape
{
void draw();
}
• RoundedRectangle.java
public class RoundedRectangle implements Shape
{
@Override
public void draw()
{
System.out.println("Inside RoundedRectangle::draw() method.");
}
}
Implementation
ShapeFactory.java
public class ShapeFactory extends AbstractFactory
{
@Override
public Shape getShape(String shapeType)
{
if(shapeType.equalsIgnoreCase("RECTANGLE"))
{
return new Rectangle();
}
else if(shapeType.equalsIgnoreCase("SQUARE"))
{
return new Square();
}
return null;
}
}
public class RoundedShapeFactory extends AbstractFactory
{
@Override
public Shape getShape(String shapeType)
{
if(shapeType.equalsIgnoreCase("RECTANGLE"))
{
return new RoundedRectangle();
}
else if(shapeType.equalsIgnoreCase("SQUARE"))
{
return new RoundedSquare();
}
return null;
}
}
• Known uses: ET++ [WGM88] uses the Abstract
Factory pattern to achieve portability across
different window systems (X Windows and
SunView, for example).
• Related patterns
AbstractFactory classes are often
implemented with factory methods but they
can also be implemented using Prototype.
A concrete factory is often a Singleton.
2.Builder
• Pattern name and classification: Builder and Creational
• Intent: Separate the construction of a complex object from its
representation so that the same construction process can
create different representations.
• Builder pattern builds a complex object using simple objects
and using a step by step approach. This type of design pattern
comes under creational pattern as this pattern provides one
of the best ways to create an object.
• A Builder class builds the final object step by step. This builder
is independent of other objects