Lecture 3 Creational Patterns Complete
Lecture 3 Creational Patterns Complete
• Note that there can be variation in the content of the children’s meal,
but the construction process is the same. Whether a customer orders
a hamburger, cheeseburger, or chicken, the process is the same. The
employee at the counter directs the crew to assemble a main item,
side item, and toy. These items are then placed in a bag. The drink is
placed in a cup and remains outside of the bag. This same process is
used at competing restaurants.
Checklist
• Decide if a common input and many possible
representations (or outputs) is the problem at hand.
• Encapsulate the parsing of the common input in a
Reader class.
• Design a standard protocol for creating all possible
output representations. Capture the steps of this
protocol in a Builder interface.
• Define a Builder derived class for each
target representation.
• The client creates a Reader object and a Builder object,
and registers the latter with the former.
• The client asks the Reader to “construct”.
• The client asks the Builder to return the result.
Factory Pattern
• Factory patterns are examples of creational patterns
▫ Creational patterns abstract the object instantiation
process.
▫ They hide how objects are created and help make the
overall system independent of how its objects are
created and composed.
▫ Class creational patterns focus on the use of
inheritance to decide the object to be instantiated
⚫ Factory Method
▫ Object creational patterns focus on the delegation of
the instantiation to another object
⚫ Abstract Factory
Difference
• The Abstract Factory pattern is very similar to
the Factory Method pattern, one difference
between the two is that with the Abstract
Factory pattern, a class delegates the
responsibility of object instantiation to another
object
• via composition whereas the Factory Method
pattern uses inheritance and relies on a subclass
to handle the desired object instantiation.
Contd..
• If there is only one creator, it specifies an
abstract factory method, allowing its subclasses
to choose the concrete product to instantiate.
▫ The factory( ) takes an argument that allows it to determine what type of Shape to create;
it happens to be a String in this case but it could be any set of data. The factory( ) is now
the only other code in the system that needs to be changed when a new type of Shape is
added (the initialization data for the objects will presumably come from somewhere outside
the system, and not be a hard-coded array as in the above example).
▫ The static factory( ) method in the previous example forces all the creation operations to
be focused in one spot, so that’s the only place you need to change the code. This is
certainly a reasonable solution, as it throws a box around the process of creating objects.
• Polymorphic factories
▫ However, the reason for the Factory Method pattern is so that different types of factories
can be sub classed from the basic factory (the above design is mentioned as a special case).
Example
• Now the factory method appears in its own class, ShapeFactory, as
the create( ) method. This is a protected method which means it
cannot be called directly, but it can be overridden.
• The subclasses of Shape must each create their own subclasses of
ShapeFactory and override the create( ) method to create an
object of their own type. The actual creation of shapes is performed
by calling ShapeFactory.createShape( ), which is a static method
that uses the Map in ShapeFactory to find the appropriate factory
object based on an identifier that you pass it.
• The factory is immediately used to create the shape object, but you
could imagine a more complex problem where the appropriate
factory object is returned and then used by the caller to create an
object in a more sophisticated way. However, it seems that much of
the time you don’t need the intricacies of the polymorphic factory
method, and a single static method in the base class will work fine.
Abstract Factory
• The Abstract Factory pattern looks like the
factory objects we’ve seen previously, with not
one but several factory methods. Each of the
factory methods creates a different kind of
object.
Prototype Pattern
• The prototype means making a clone. This implies
cloning of an object to avoid creation.
• If the cost of creating a new object is large and
creation is resource intensive, we clone the object.
• We use the interface Cloneable and call its method
clone() to clone the object.
• In Case of a plant cell.
▫ cloning involves making a copy of the original one.
▫ Here, we break the cell in two and make two copies
and the original one does not exist.
▫ Let’s take a class Plant Cell having a method split().
The plant cell will implement the interface Cloneable.
Discussion