Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

CSE 425: Software

Design and Pattern


Section 2
Type of Creation pattern
1. Singleton pattern
2. Abstract Factory
3. Factory Method
4. Builder
5. Prototype

Singleton:

• Ensure a class only has one instance, and provide a global


point of access to it.

Abstract Factory:

• Provide an interface for creating families of related or


dependent objects without specifying their concrete
classes.
• Clients never create platform objects directly, they ask the
factory to do that for them.
Factory Method:
• Define an interface for creating an object, but let
subclasses decide which class to instantiate.
• The advantage of a Factory Method is that it can return
the same instance multiple times, or can return a subclass
rather than an object of that exact type.

Builder
• Separates object construction from its representation.
• Separate the construction of a complex object from its
representation.
• Provides control over steps of construction process.
Structure of Builder Pattern:

1. Builder:
Specifies an abstract interface for creating parts
of a Product object.
2. Concrete Builder:
Constructs and assembles parts of the product
by implementing the Builder interface.
3. Director:
Constructs an object using the Builder interface 
4. Product:
Represents the complex object under
construction.
Abstract Factory vs Builder:
• Builder focuses on constructing a complex object step by
step. Abstract Factory focuses on constructing a family of
product objects.
• Builder focus on building one Complex product. Abstract
factory focus on building many different types of families
to build many products.
Prototype
• Allows an object to create customized objects without
knowing their class or any details of how to create them.
• The concept is to copy an existing object rather than
creating a new instance from scratch, something that may
include costly operations.
• The newly copied object may change same properties only
if required.

Type of Clone:
1. Shallow Clone:
Shallow copy copies an object’s value type fields into
the target object and the object’s reference types are
copied as references into the target object.
2.Deep Clone:
A deep copy copies an object’s value and reference
types into a complete new copy of the target objects.
Question 1:

Builds a class based on requirements where Director asks the builder to


build each of the parts. Mainly the builder pattern is not used
independently but other patterns may have builder pattern as a part
where they create the complicated objects using the builder.
Typically the class diagram looks like

Apply this pattern for Pizza Restaurant as we have a waiter as a


director and pizza as a product with different sizes and topics.
Answer:
Waiter is Director
PizzaBuilder is Builder
CheesePizzaBuilder and MixedPizzaBuilder are Concretebuilder
Pizza is product
When Waiter (Director) is asked to serve it creates the Pizza
(Product) using the PizzaBuilder.
Question 2:
Client uses the factory to create products and it’s the factory
which decides when the actual product is needed for
instantiation. This way client decouples the instance and can be
saved from some of the crucial operations of object copy if the
type of object may change after creation.

Apply this pattern considering the creator is a computer


factory and the product is the processor.
Answer:
ComputerFactory (Creator)
ConcreteComputerFactory (ConcreteCreator)
Processor (Product)
ConcreteProcessor (ConcreteProduct)
When the GetProcessor of ComputerFactory is called its the
ConcreteComputerFactory creates the ConcreteProcessor
and the creation of ConcreteProcessor is delayed till we call the
GetProcessor() function.

You might also like