010 Design Pattern Creational 20250310
010 Design Pattern Creational 20250310
Pattern
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]
• 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
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
Builder Design
Pattern
13
Singleton pattern – intent
14
▪ 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
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 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
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
▪ 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
THANK YOU