CreationalPatterns
COMP2511
Creational Patterns:
Factory Method
Abstract Factory Pattern
Builder Pattern
Singleton Pattern
Prepared by
Dr. Ashesh Mahidadia
Design Patterns
Creational Patterns
v Factory Method
v Abstract Factory
v Builder
v Singleton
Structural Patterns
v Adapter
v Composite
v Decorator
COMP2511: Creational Design Patterns 2
Behavioral Patterns
v Iterator
v Observer
v State
v Strategy
v Template
v Visitor
discussed
discussed
discussed
discussed
discussed
discussed
discussed
discussed
Creational Patterns
Some of the material from https://refactoring.guru/ and Wikipedia.
3COMP2511: Creational Design Patterns
Creational Patterns
Creational patterns provide various object creation mechanisms, which increase
flexibility and reuse of existing code.
v Factory Method
o provides an interface for creating objects in a superclass,
but allows subclasses to alter the type of objects that will be created.
v Abstract Factory
o let users produce families of related objects
without specifying their concrete classes.
v Builder
o let users construct complex objects step by step. The pattern allows users to
produce different types and representations of an object using the same
construction code.
v Singleton
o Let users ensure that a class has only one instance,
while providing a global access point to this instance.
COMP2511: Creational Design Patterns 4
Factory Method
5COMP2511: Creational Design Patterns
Factory Method
v Factory Method is a creational design pattern that uses factory methods to deal with
the problem of creating objects without having to specify the exact class of the object
that will be created.
v Problem:
o creating an object directly within the class that requires (uses) the object is inflexible
o it commits the class to a particular object and
omakes it impossible to change the instantiation independently from
(without having to change) the class.
v Possible Solution:
o Define a separate operation (factory method) for creating an object.
o Create an object by calling a factory method.
o This enables writing of subclasses to change the way an object is created
(to redefine which class to instantiate).
COMP2511: Creational Design Patterns 6
Factory Method : Structure
1. The Product declares the interface, which is common to all objects that can be produced by the
creator and its subclasses.
2. Concrete Products are different implementations of the product interface.
3. The Creator class declares the factory method that returns new product objects.
4. Concrete Creators override the base factory method so it returns a different type of product.
COMP2511: Creational Design Patterns 7
Factory Method : Example
COMP2511: Creational Design Patterns 8
Factory Method
COMP2511: Creational Design Patterns 9
Demonstration :
Factory Method Pattern
Factory Method
COMP2511: Creational Design Patterns 10
• For more, read the following:
https://refactoring.guru/design-patterns/factory-method
Abstract Factory Pattern
11COMP2511: Creational Design Patterns
Abstract Factory Pattern
Intent: Abstract Factory is a creational design pattern that lets you produce families of
related objects without specifying their concrete classes.
Problem:
Imagine that you’re creating a furniture shop simulator. Your code consists of classes that
represent:
v A family of related products, say: Chair + Sofa + CoffeeTable.
v Several variants of this family.
v For example, products Chair + Sofa + CoffeeTable are available in these variants:
COMP2511: Creational Design Patterns 12
Abstract Factory Pattern:
COMP2511: Creational Design Patterns 13
Possible Solution:
Abstract Factory Pattern: Structure
COMP2511: Creational Design Patterns 14
1. Abstract Products declare interfaces for a set of distinct but related products which make up a product family.
2. Concrete Products are various implementations of abstract products, grouped by variants. Each abstract product
(chair/sofa) must be implemented in all given variants (Victorian/Modern).
3. The Abstract Factory interface declares a set of methods for creating each of the abstract products.
4. Concrete Factories implement creation methods of the abstract factory. Each concrete factory corresponds to a specific
variant of products and creates only those product variants.
5. The Client can work with any concrete factory/product variant, as long as it communicates with their objects via abstract
interfaces.
Abstract Factory Pattern: Example
COMP2511: Creational Design Patterns 15
Abstract Factory Pattern
Demonstration :
Abstract Factory Pattern
COMP2511: Creational Design Patterns 16
Abstract Factory Pattern
• For more, read the following:
https://refactoring.guru/design-patterns/abstract-factory
COMP2511: Creational Design Patterns 17
Builder Pattern
18COMP2511: Creational Design Patterns
Builder Pattern
Intent: Builder is a creational design pattern that lets you construct complex objects step
by step. The pattern allows you to produce different types and representations of an object
using the same construction code.
Problem:
v Imagine a complex object that requires laborious, step-by-step
initialization/construction of many fields and nested objects.
v Such initialization/construction code is usually buried inside a monstrous constructor
with lots of parameters.
v Or even worse: scattered all over the client code.
COMP2511: Creational Design Patterns 19
Builder Pattern
v The Builder pattern suggests that you extract the object construction code out of its
own class and move it to separate objects called builders.
v The Builder pattern lets you construct complex objects step by step.
v The Builder doesn’t allow other objects to access the product while it’s being built.
v Director: The director class defines the order in which to execute the building steps,
while the builder provides the implementation for those steps.
COMP2511: Creational Design Patterns 20
Builder Pattern: Structure
v The Builder interface declares product
construction steps that are common to all types
of builders.
v Concrete Builders provide different
implementations of the construction steps.
Concrete builders may produce products that
don’t follow the common interface.
v Products are resulting objects. Products
constructed by different builders don’t have to
belong to the same class hierarchy or interface.
v The Director class defines the order in which to
call construction steps, so you can create and
reuse specific configurations of products.
v The Clientmust associate one of the builder
objects with the director.
COMP2511: Creational Design Patterns
21
Builder Pattern: Example
This example illustrates how you can reuse the
same object construction code when,
v building different types of cars, and
v creating the corresponding manuals for
them.
Example in Java (MUST read):
https://refactoring.guru/design-patterns/builder/java/example
COMP2511: Creational Design Patterns
22
Relations with Other Patterns
v Many designs start by using Factory Method (less complicated and more customizable
via subclasses) and evolve toward Abstract Factory, or Builder (more flexible, but more
complicated).
v Builder focuses on constructing complex objects step by step.
v Abstract Factory specializes in creating families of related objects.
v Abstract Factory returns the product immediately, whereas Builder lets you run some
additional construction steps before fetching the product.
COMP2511: Creational Design Patterns 23
Builder Pattern
Demonstration:
Builder Pattern
COMP2511: Creational Design Patterns 24
Builder Pattern
• For more information, read:
https://refactoring.guru/design-patterns/builder
COMP2511: Creational Design Patterns 25
Singleton Pattern
26COMP2511: Creational Design Patterns
Singleton Pattern
Intent: Singleton is a creational design pattern that lets you ensure that a class has
only one instance, while providing a global access point to this instance.
Problem: A client wants to,
v ensure that a class has just a single instance, and
v provide a global access point to that instance
Solution:
All implementations of the Singleton have these two steps in common:
v Make the default constructor private, to prevent other objects from using the new operator
with the Singleton class.
v Create a static creation method that acts as a constructor. Under the hood, this method calls the
private constructor to create an object and saves it in a static field. All following calls to this
method return the cached object.
v If your code has access to the Singleton class, then it’s able to call the Singleton’s static method.
v Whenever Singleton’s static method is called, the same object is always returned.
COMP2511: Creational Design Patterns 27
Singleton: Structure
v The Singleton class declares the static
method getInstance (1) that returns the
same instance of its own class.
v The Singleton’s constructor should be
hidden from the client code.
v Calling the getInstance (1) method
should be the only way of getting the
Singleton object.
COMP2511: Creational Design Patterns 28
Singleton: How to Implement
v Add a private static field to the class for storing the singleton instance.
v Declare a public static creation method for getting the singleton instance.
v Implement “lazy initialization” inside the static method.
o It should create a new object on its first call and put it into the static field.
o The method should always return that instance on all subsequent calls.
v Make the constructor of the class private.
o The static method of the class will still be able to call the constructor, but not the
other objects.
v In a client, call singleton’s static creation method to access the object.
COMP2511: Creational Design Patterns 29
Singleton Pattern
Demonstration:
Singleton Pattern
COMP2511: Creational Design Patterns 30
Singleton Pattern
• For more information, read:
https://refactoring.guru/design-patterns/singleton
COMP2511: Creational Design Patterns 31
End
COMP2511: Creational Design Patterns 32