Sei sulla pagina 1di 16

Design Patterns

Workshop
Creational Design Pattern – Builder, Prototype, Object Pool

Session 04
Objectives
Ø Builder Pattern
ü Overview
ü Class Diagram
ü Implementation Classes
ü Builder Design Pattern Examples in Java SE
Ø Prototype Pattern
ü Overview
ü Class Diagram and Implementation Classes
Ø Object Pool
ü Overview
ü Class Diagram and Implementation Classes
Ø Case Study
Builder Pattern
Ø Builder design pattern is a creational design pattern like Factory Pattern and Abstract
Factory Pattern. This pattern was introduced to solve some of the problems with
Factory and Abstract Factory design patterns when the Object contains a lot of
attributes.
Ø There are three major issues with Factory and Abstract Factory design patterns when
the Object contains a lot of attributes.
Factory and Abstract Factory
Ø Too Many arguments to pass from client program to the Factory class that can be error
prone because most of the time, the type of arguments are same and from client side
it’s hard to maintain the order of the argument.
Ø Some of the parameters might be optional but in Factory pattern, we are forced to
send all the parameters and optional parameters need to send as NULL.
Ø If the object is heavy and its creation is complex, then all that complexity will be part
of Factory classes that is confusing.
Builder Design Pattern
Ø To solve the issues with large number of parameters by providing a constructor with
required parameters and then different setter methods to set the optional parameters
but the problem with this is that the Object state will be inconsistent until unless all
the attributes are set explicitly.
Ø Builder pattern solves the issue with large number of optional parameters and
inconsistent state by providing a way to build the object step-by-step and provide a
method that will actually return the final Object.
Builder Design Pattern
Class Diagram
Builder Design Pattern
Ø First of all you need to create a static nested class and then copy all the arguments
from the outer class to the Builder class. We should follow the naming convention and
if the class name is Event then builder class should be named as EventBuilder.
Ø The Builder class should have a public constructor with all the required attributes as
parameters.
Ø Builder class should have methods to set the optional parameters and it should return
the same Builder object after setting the optional attribute.
Ø The final step is to provide a build() method in the builder class that will return the
Object needed by client program. For this we need to have a private constructor in the
Class with Builder class as argument.
Builder Design Pattern
Ø Builder Design Pattern Examples in Java SE
ü java.lang.StringBuilder#append() (unsynchronized)

ü java.lang.StringBuffer#append() (synchronized)
Prototype Design Pattern
Ø When a client needs to create a set of objects that are alike or differ from each other
only in terms of their state and it is expensive to create such objects in terms of the
time and the processing involved.
Ø As an alternative to building numerous factories that mirror the classes to be
instantiated (as in the Factory Method).
Ø Prototype pattern suggests to:
ü Create one object upfront and designate it as a prototype object.
ü Create other objects by simply making a copy of the prototype object and making
required modifications.
Ø Provides a simpler way of creating an object by cloning it from an existing (prototype)
object.
SHALLOW COPY VERSUS DEEP COPY
Ø The built-in clone() method creates a clone of the original object as a shallow copy.
Ø When an object is cloned as a shallow copy:
ü The original top-level object and all of its primitive members are duplicated.
ü Any lower-level objects that the top-level object contains are not duplicated. Only
references to these objects are copied. This results in both the original and the
cloned object referring to the same copy of the lower-level object.
Ø When an object is cloned as a deep copy:
ü The original top-level object and all of its primitive members are duplicated. Any
lower-level objects that the top-level object contains are not duplicated. Only
references to these objects are also duplicated. In this case, both the original and
the cloned object refer to two different lower-level objects.
SHALLOW COPY VERSUS DEEP COPY

Shallow Clone Deep Clone

Original Object Cloned Object Original Object Cloned Object

Reference Object Reference Object Reference Object


Prototype Design Pattern
Object Pool Design Pattern
Ø Mostly, performance is the key issue during the application development and the
object creation, which may be a cost us.
Ø Object Pool Pattern allow us " to reuse the object that are expensive to create".
Ø Object pool is a container which contains a specified number of objects. If an object is
taken from the pool, it is not available in the pool until it is put back.
Ø Life Cycle of Objects in Pool: Creation, Validation and Destroy.
Advantage of Object Pool Design Pattern
Ø It boosts the performance of the application significantly.
Ø It is most effective in a situation where the rate of initializing a class instance is high.
Ø It manages the connections and provides a way to reuse and share them.
Ø It can also provide the limit for the maximum number of objects that can be created.
Ø An application requires objects, which are expensive to create. For example there is a
need of opening too many connections for the database then it takes too longer to
create a new one and the database server will be overloaded.
Ø There are several clients who need the same resource at different times.
Object Pool Design Pattern
Class Diagram
Case Study
Ø Revise all Creational Design Patterns.

Potrebbero piacerti anche