Factory Pattern
Factory Pattern
Factory 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.
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 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.
Related Patterns:
Abstract Factory
Singleton
Flyweight
Abstract Factory pattern
Intent
· 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.
Related Patterns:
Factory Method
Singleton