Factory Method: Object Diagram For Factory Injection Mold Example

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

Creational Factory Method

Factory Method
Object Diagram
for Factory
Method using
Injection Mold
Example

The Factory Method defines an interface for creating objects, but lets subclasses
decide which classes to instantiate. Injection molding presses demonstrate this
pattern. Manufacturers of plastic toys process plastic molding powder, and inject the
plastic into molds of the desired shapes .The class of toy (car, etc.) is determined by
the mold
Creational Factory Method
Factory Method
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
Creational Factory Method
Problem :
“A framework needs to standardize the architectural model
for a range of applications, but allow for individual
applications to define their own domain objects and
provide for their instantiation”

<<create>>
Creates
Creational Factory Method

Illustration: Example Application that can present


multiple documents to the user
Frameworks use abstract classes to define and maintain
relationships between objects.
A framework is often responsible for creating these objects
as well.
As shown in the figure :
Application and Document which
form a relationship need to be *
extended by the client applications

<<create>>
Creational Factory Method
Illustration :
Because the particular document subclass to instantiate is
application specific, the application class cant predict the
subclass of the document to instantiate.
The problem here is : The framework must instantiate
classes , but it only knows about abstract classes, which it
can not instantiate.

<<create>>
Creational Factory Method

Illustration : Solution
• Factory Method Pattern offer a solution. It encapsulates
the knowledge of which Document subclass to create
and moves this knowledge out of the framework.
• Application subclasses redefine an abstract
CreateDocument( ) on Application to return an appropriate
Document subclass.
• Once an application subclass is instantiated , It can then
instantiate application – specific Documents without
knowing their Class , We call CreateDocument( ) a
Factory Method because it is responsible for
manufacturing an object.
Creational Factory Method

Solution :
• A superclass specifies all standard and generic
behavior (using pure virtual "placeholders" for
creation steps), and then delegates the creation
details to subclasses that are supplied by the client.
• Factory Method makes a design more
customizable and only a little more complicated.
Other design patterns require new classes, whereas
Factory Method only requires a new operation.
Creational Factory Method
Applicability
Use the Factory Method pattern when
 
• A class can’t anticipate the class objects it must create.
 
• A class wants its subclasses to specify the objects it
creates.
 
• Classes delegate responsibility to one of several helper
subclasses, and you want to localize the knowledge of
which helper subclass is the delegate.
Creational Factory Method
Structure

<<creates>>
Creational Factory Method

Collaborations

• Creator relies on its subclasses to define the


factory method so that it returns an instance of the
appropriate ConcreteProduct.

Consequences
• Connects parallel class hierarchies.
– Isolates concrete classes
– Makes exchanging product families easy
– Promotes consistency among products
Creational Factory Method

Model
Room,Door,Wall
classes inherited
form MapSite Class

<<Create>> <<Create>>
<<Create>>
Creational Factory Method

Related Patterns

• Abstract factory is often implemented with factory


methods.
• Factory Method is to creating objects as Template
Method is to implementing an algorithm
• Factory Methods are usually called within
Template Methods.
• Factory Method: creation through inheritance.
Prototype: creation through delegation

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy