0% found this document useful (0 votes)
5 views26 pages

010 Design Pattern Creational 20250310

The document discusses creational design patterns in software engineering, emphasizing their importance for creating flexible and reusable software. It outlines various design patterns such as Singleton, Prototype, and Factory, detailing their intents, problems, and implementations. The document also highlights the significance of design patterns in improving maintainability, extensibility, and scalability in software design.

Uploaded by

MANI 96 FF
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views26 pages

010 Design Pattern Creational 20250310

The document discusses creational design patterns in software engineering, emphasizing their importance for creating flexible and reusable software. It outlines various design patterns such as Singleton, Prototype, and Factory, detailing their intents, problems, and implementations. The document also highlights the significance of design patterns in improving maintainability, extensibility, and scalability in software design.

Uploaded by

MANI 96 FF
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Design Pattern – Creational

Pattern

Malik Nabeel Ahmed Awan, PhD


Design challenges
2

• Designing software for reuse is hard. One must find:


• a good problem decomposition, and the right software
• a design with flexibility, modularity and elegance
• Designs often emerge from trial and error
• Successful designs do exist
• two designs they are almost never identical
• they exhibit some recurring characteristics
• Can designs be described, codified or standardized?
• this would short circuit the trial-and-error phase
• produce "better" software faster
Why use Design Patterns?
3

Good Design
Maintainability

The
Extensibility
Scalability

ro a
Testability

d to
Reusablilty

goo
Design Patterns

dd
Singleton

e si
Abstract Factory

gn
DAO
Strategy
Decorator

Design Principles
Program to an interface, not an implementation
High cohesion
Low coupling
Open-Closed
Separation of concerns

University of Kansas
Design pattern
4

• design pattern:
a solution to a common software problem in a context
• describes a recurring software structure
• is abstract from programming language
• identifies classes and their roles in the solution to a problem
• patterns are not code or designs; must be instantiated/applied
• example: Iterator pattern
• The Iterator pattern defines an interface that declares methods for
sequentially accessing the objects in a collection.
What is a design pattern?
5

“Each pattern describes a problem which occurs over and over again in our
environment, and then describes the core of the solution to that problem, in such
a way that you can use this solution a million times over, without ever doing it the
same way twice.” [Christopher Alexander]

Design patterns capture the best practices of experienced object-oriented software


developers. Design patterns are solutions to general software development
problems.
Patterns in engineering
6

• How do other engineers find and use patterns?


• Mature engineering disciplines have handbooks describing successful solutions to known
problems
• Automobile designers don't design cars from scratch using the laws of physics
• Instead, they reuse standard designs with successful track records, learning from experience
• Should software engineers make use of patterns? Why?
• “Be sure that you make everything according to the pattern I have shown you here on the
mountain.” Exodus 25:40.
• Developing software from scratch is also expensive
• Patterns support reuse of software architecture and design
The “gang of four” (GoF)
7

• Erich Gamma, Richard Helm, Ralph Johnson & John Vlissides (Addison-
Wesley, 1995)
• Design Patterns book catalogs 23 different patterns as solutions to different classes of
problems
• The problems and solutions are broadly applicable, used by many people over many
years
• Patterns suggest opportunities for reuse in analysis, design and programming
• GOF presents each pattern in a structured format
Elements of Design Patterns
8

• Name
• Describes the pattern
• Adds to common terminology for facilitating communication (i.e., not just sentence
enhancers)
• Problem
• Describes when to apply the pattern
• Answers - What is the pattern trying to solve?
• Solution
• Describes elements, relationships, responsibilities, and collaborations which make up the
design
• Consequences
• Results of applying the pattern
• Benefits and Costs
• Subjective depending on concrete scenarios
Design
Patterns

Design pattern classification


Creational Structural Behavioral
9

Erich Gamma, Richard Helm, Ralph Johnson and John Vlisides in their
Design Patterns book define 23 design patterns divided into three
types:
▪ Creational patterns are ones that create objects for you, rather than
having you instantiate objects directly. This gives your program more
flexibility in deciding which objects need to be created for a given case.
▪ Structural patterns help you compose groups of objects into larger
structures, such as complex user interfaces or accounting data.
▪ Behavioral patterns help you define the communication between objects
in your system and how the flow is controlled in a complex program.
Design pattern
types

10
Creational design patterns
Creational design patterns
12

▪ The creational design patterns deal with the best way to create instances
of objects.
▪ In Java, the simplest way to create an instance of an object is by using the
new operator.
Fred = new Fred(); //instance of Fred class

▪ This amounts to hard coding, depending on how you create the object
within your program.
▪ In many cases, the exact nature of the object that is created could vary
with the needs of the program and abstracting the creation process into a
special “creator” class can make your program more flexible and general.
Creational design patterns

Singleton Prototype
Design Pattern Design Pattern

Factory Design Abstract Factory


Pattern Design Pattern

Builder Design
Pattern
13
Singleton pattern – intent
14

▪ Sometimes it’s appropriate to have exactly one instance of a class:


▪ window managers,
▪ print spoolers,
▪ database connection
▪ With the Singleton design pattern, you can:
▪ Ensure that only one instance of a class is created.
▪ Provide a global point of access to the object.
▪ Allow multiple instances in the future without affecting a singleton
class' clients.
Singleton Pattern – problem
15

▪ The Singleton pattern ensures a class has only one instance and provides a global
point of access to it.
▪ The class itself is responsible for keeping track of its sole instance. The class can
ensure that no other instance can be created (by intercepting requests to create
new objects), and it can provide a way to access the instance.
▪ Singletons maintain a static reference to the sole singleton instance and return a
reference to that instance from a static instance() method.
Singleton pattern – discussion
16

public class ClassicSingleton {


private static ClassicSingleton instance = null;
protected ClassicSingleton() {
// exists only to defeat instantiation.
}
public static ClassicSingleton getInstance() {
if(instance == null) {
instance = new ClassicSingleton();
}
return instance;
}
}

The ClassicSingleton class maintains a static reference to the lone singleton instance
and returns that reference from the static getInstance() method.
Singleton pattern – discussion
17

▪ The ClassicSingleton class employs a technique known as lazy


instantiation to create the singleton; as a result, the singleton
instance is not created until the getInstance() method is called for the
first time. This technique ensures that singleton instances are created
only when needed.

Prototype pattern – intent
18

▪ The Prototype pattern specifies the kinds of objects to create using a prototypical
instance and create new objects by copying this prototype.
▪ A Prototype pattern is used when creating an instance of a class is very time-
consuming or complex in some way.
▪ Then, rather than creating more instances, you make copies of the original instance and
modify them as appropriate.
Prototype pattern – Problem
19

▪ Application "hard wires" the class of object to create in each "new"


expression.
Prototype pattern – discussion
20

You can make a copy of any Java object using the clone method.

Jobj j1 = (Jobj)j0.clone();

The clone method always returns an object of type Object. You must cast it
to the actual type of the object you are cloning. There are three other
significant restrictions on the clone method:
▪ It is a protected method and can only be called from within the same class or the
module that contains that class.
▪ You can only clone objects which are declared to implement the Cloneable
interface.
▪ Objects that cannot be cloned throw the CloneNotSupported Exception.
Factory pattern - intent
21

▪ The Factory pattern returns an instance of one of several possible


classes depending on the data provided to it.
▪ Define an interface for creating an object, but let subclasses decide
which class to instantiate. Factory Method lets a class defer
instantiation to subclasses.
Factory pattern - Problem
22

▪ 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.
Factory pattern - Discussion
23

▪ Here, x is a base class and classes xy and xz are derived from it.
▪ The Factory is a class that decides which of these subclasses to return depending
on the arguments you give it.
▪ The getClass() method passes in some value abc, and returns some instance of
the class x. Which one it returns doesn't matter to the programmer since they all
have the same methods, but different implementations.
Factory pattern - Example
24

 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, action figure,
etc.) is determined by the mold.
Factory pattern – when to use
25

▪ A class can’t anticipate the class of 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
26

THANK YOU

You might also like