Factory Pattern

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 42

Creational pattern

In software engineering, creational design


patterns are design patterns that deal with
object creation mechanisms, trying to create
objects in a manner suitable to the situation. The
basic form of object creation could result in
design problems or added complexity to the
design. Creational design patterns solve this
problem by somehow controlling this
object creation.

Factory method pattern


Abstract factory pattern
Factory method pattern

You can make out from the name factory its meant
to construct and create something.

Intent
Define an interface for creating an object, but let subclasses
decide which class to instantiate.

Factory Method lets a class defer instantiation to subclasses.

Also Known As (Virtual Constructor)


Motivation

Sometimes, an Application (or framework) at runtime, cannot


anticipate the class of object that it must create.

a class may want it's subclasses to specify the objects to be


created.

a class may delegate responsibility to one of several helper


subclasses so that knowledge can be localized to specific
helper subclasses.
Common usage
• Factory methods are common in Toolkits and Frameworks where
library code needs to create objects of types which may be subclassed by
applications using the framework.

The Problem
• One of the goals of object-oriented design is to delegate responsibility
among different objects. This kind of partitioning is good since it
encourages Encapsulation and Delegation.
•Sometimes, an Application (or framework) at runtime,
cannot anticipate the class of object that it must create.

•The Application (or framework) may know that it has


to instantiate classes, but it may only know about abstract
classes (or interfaces), which it cannot instantiate.

•Thus the Application class may only know when it has to


instantiate a new Object of a class, not what kind of subclass
to create.
• A class may want it's subclasses to specify the objects to
be created.

• A class may delegate responsibility to one of several


helper subclasses so that knowledge can be localized to
specific helper subclasses.
The Problem

The Problem with the code

1- Lots of scattered new Key word


2- Client is aware of all invoice types
The
Solution
The Solution
This pattern helps to model an interface for creating an
object which at creation time can let its subclasses decide
which class to instantiate.

a Factory Pattern since it is responsible for "Manufacturing"


an Object. It helps instantiate the appropriate Subclass
. by creating the right Object from a group of related classes

The Factory Pattern promotes loose coupling by eliminating


the need to bind application-specific classes into the code.
More about Factory
Method
The factory method pattern is an Object_Oriented Pattern Like other creational
patterns, it deals with the problem of creating object (products) without specifying
the exact class of object that will be created.

The factory method design pattern handles this problem by defining a separate
method for creating the objects, whose subclasses can then override to specify
the derived type of product that will be created.

More generally, the term factory method is often used to refer to any method
whose main purpose is creation of objects.
Consequences
Factory methods eliminate the need to bind application-specific classes into
your code. The code only deals with the Product interface; therefore it can
work with any user-defined Concrete Product classes.

Factory Method pattern is a simplified version of Abstract Factory pattern.


Factory Method pattern is responsible of creating products that belong to one
family, while Abstract Factory pattern deals with multiple families of products.

Related Patterns:

Abstract Factory
Singleton
Flyweight
Abstract Factory pattern
Intent

Provide an interface for creating families of related or

dependent Object without specifying their concrete classes.


Applicability
Use the Abstract Factory pattern when
· 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.
Consequences
The Abstract Factory pattern has the following benefits and liabilities:

1. 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.

2. 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
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.
Consequences
3. 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.

4. Supporting new kinds of products is difficult. Extending abstract


factories to produce new kinds of Products isn't easy. That's because
the AbstractFactory interface fixes the set of products that can be
created Supporting new kinds of products requires extending the factory
interface, which involves changing the AbstractFactory class and all of
its subclasses. We discuss one solution to this problem in the
Implementation section
Implementation
Here are some useful techniques for implementing the Abstract Factory
pattern.

1. Factories as singletons. An application typically needs only one


instance of a ConcreteFactory per product family. So it's usually best
implementedas a Singleton.

2. Creating the products. AbstractFactory only declares an interface for


creating products. It's up to ConcreteProduct subclasses to actually
create them. The most common way to do this is to define a factory
method.

Related Patterns:

Factory Method
Singleton

You might also like