Professional Documents
Culture Documents
Patterns
From Definition to Implementation
Usage
1. When the system needs to be independent of how its object are created, composed, and
represented.
2. When the family of related objects has to be used together, then this constraint needs to
be enforced.
3. When you want to provide a library of objects that does not show implementations and
only reveals interfaces.
4. When the system needs to be configured with one of a multiple family of objects.
Abstract Factory Pattern Demo, our demo class uses Factory Producer
to get an AbstractFactory object. It will pass information (CIRCLE /
RECTANGLE / SQUARE for Shape) to AbstractFactory to get the type of
object it needs. It also passes information (RED / GREEN/BLUE for Color)
to Abstract Factory to get the type of object it needs.
05
Class
Diagram
08
Step 1:
Interfaces
08
Step 2: Concrete Classes
08
Step 3: Create Abstract Factory
08
Step 4: Factory Classes
08
Step 5: Factory Producer
08
Step 6: Test
08
Step 7: Result
08
Factory method vs Abstract Factory
The methods of an Abstract Factory are implemented as Factory Methods. Both the
Abstract Factory Pattern and the Factory Method Pattern decouple the client system from
the actual implementation classes through the abstract types and factories. The Factory
Method creates objects through inheritance whereas the Abstract Factory creates objects
through composition.
With the Factory pattern, you produce implementations (Apple, Banana, Cherry, etc.) of a
particular interface -- say, IFruit. With the Abstract Factory pattern, you produce
implementations of a particular Factory interface -- say, IFruitFactory. Each of those knows
how to create different kinds of fruit.
Factory method: You have a factory that creates objects that derive from a particular base
class. Abstract factory: You have a factory that creates other factories, and these factories
in turn create objects derived from base classes. You do this because you often don't just
want to create a single object (as with the Factory method) - rather, you want to create a
collection of related objects.
07
Example
• Imagine you are constructing a house and you approach a
carpenter for a door. You give the measurement for the door and
your requirements, and he will construct a door for you. In this
case, the carpenter is a factory of doors. Your specifications are
inputs for the factory, and the door is the output or product from
the Factory.
14