0% found this document useful (0 votes)
5 views

Lec12 Pattern design (1)

Uploaded by

23020666
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lec12 Pattern design (1)

Uploaded by

23020666
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

Design Patterns

Vũ Thị Hồng Nhạn

([email protected])

Dept. Software Engineering, FIT, UET,

Vietnam National Univ., Hanoi


Content
 Benefits of design patterns
 3 Categories of design patterns
 Creational design patterns
 Structural design patterns
 Behavioral design patterns

12/06/2024 Design patterns Page 2


Materials
 Bruce Eckel, Thinking in Patterns
 Erich Gamma, Design Patterns – Elements of Reusable
Object-Oriented Software

12/06/2024 Design patterns Page 3


Introduction
Benefits of design patterns
 They help construct reliable software by leveraging proven
architectures and accumulated industry experiences

 Promote design reuse in future systems

 Aid in identifying common mistakes and pitfalls that may


occur during system development

 Enable the design of system independently of the


programming language in which they will be implemented

 Establish a common design vocabulary among developers


and shorten the design phase of the software development
process

12/06/2024 Design patterns Page 4


Introduction…
 Design patterns are neither classes nor objects

 Instead, they are reusable solutions to common problems in


software design

 Designers use design patterns to organize and construct


sets of classes and objects effectively

 To use design patterns effectively, designers must become


familiar with the most popular and widely-used patterns in
SE industry

 This lesson introduces several popular design patterns in


Java. However, these patterns can also be implemented in
nay object-oriented programming languages, such as C++

12/06/2024 Design patterns Page 5


Three categories

 Creational patterns: focus on object creation to ensure flexibility


and reuse
 Structural patterns: focus on organizing and composing objects
for efficient relationships
 Behavioral patterns: focus on communication and responsibility

12/06/2024 between objects Design patterns Page 6


Three categories…
1. Creational 2. Structural 3. Behavioral

Singleton, Adapter, Memento,

Factory method , Composite, State,

Abstract factory, Decorator, Chain of responsibility,

Prototype Proxy, Command,

Bridge, Observer,

Façade Strategy,

Template method,

Iterator

12/06/2024 Design patterns Page 7


1. Creational

Singleton
Singleton
static uniqueInstance
data //Instance()
return uniqueInstance;
static Instance()
operation()
getData()

 Occasionally, a system needs to ensure that only one


instance of a class exists
 i.e., once the program creates that instance, it should
prevent the creation of additional instances of the same
class
 E.g., some systems connect to a database using a single
object that manages database connections.
 This approach ensures that no other objects can initialize
unnecessary connections, which could slow down the system
12/06/2024 Design patterns Page 8
1. Creational

Example using singleton

12/06/2024 Design patterns Page 9


1. Creational

Example using singleton…

2
reference
s to
Singleton
object

12/06/2024 Design patterns Page 10


Remark

 Private Constructor
 Prevents a class from being subclassed
 Prevents the creation of an object outside the class
 If all the methods are static, then a private constructor
can be used
 If we try to extend a class that has a private
constructor, a compile-time error will occur

12/06/2024 Design patterns Page 11


1. Creational

Factory method pattern


 Is a design pattern that provides an interface for creating objects,
but delegates the responsibility of determining the specific class to
instantiate to subclasses

 Instead of directly calling the constructor to create an object, the


client uses a factory method, which can return different types of
objects based on the context or conditions at runtime

 Purpose: to define a method for creating objects, but allow


subclasses to decide which class to instantiate

 Use case: it’s useful when the system needs to determine which
class to instantiate at runtime, based on different conditions or
input
12/06/2024 Design patterns Page 12
1. Creational

Factory method pattern


 Example:

A system needs to create different types of


products (e.g. Car or Truck)

A factory method can be used to decide which


product type should be created without hardcoding
the specific class

12/06/2024 Design patterns Page 13


1. Creational

Factory method pattern

12/06/2024 Design patterns Page 14


1. Creational

Factory method pattern…

12/06/2024 Design patterns Page 15


1. Creational

Factory method pattern…

12/06/2024 Design patterns Page 16


1. Creational

Abstract factory pattern


 is a creational design pattern that provides an interface for creating
families of related or dependent objects without specifying their
concrete classes

 It is used when there are multiple related products, and the system
needs to remain decoupled from their concrete implementations
Factory method Abstract factory

Create one type of object Create families of related objects

Relies on subclassing, where each Relies on object composition.


class implements the factory The factory itself contains methods to create
method multiple products

Use when the focus on creating Use when you need to create a group of related
one specific product objects with different implementation

12/06/2024 Design patterns Page 17


1. Creational

Abstract factory pattern


 Example
 Use a Vehicle Factory to create related
objects: Cars and Trucks for either Electric and
Gasoline engines

12/06/2024 Design patterns Page 18


1. Creational

Abstract factory pattern


Abstract product interfaces

12/06/2024 Design patterns Page 19


1. Creational

Abstract factory pattern


Concrete product implementation

12/06/2024 Design patterns Page 20


1. Creational

Abstract factory pattern

12/06/2024 Design patterns Page 21


1. Creational

Abstract factory pattern

12/06/2024 Design patterns Page 22


2. Structural design pattern

 Describe common ways to organize classes and object in a

system

 Three out of 7 structural design patterns will be introduced

 Adapter

 Composite

 Decorator

12/06/2024 Design patterns Page 23


2. Structural

Adapter pattern
 Helps objects with incompatible interfaces collaborate by
acting as a bridge between them

 It provides an object with a new interface that adapts to


another object’s existing interface, enabling smooth
collaboration

12/06/2024 Design patterns Page 24


E.g.

12/06/2024 Design patterns Page 25


E.g.

12/06/2024 Design patterns Page 26


2. Structural design pattern

Composite pattern
 Provides a way to organize and manipulate objects in a
hierarchical structure
 It’s useful when the structure contains objects from
manipulate classes (e.g., a directory containing both files
and subdirectories)
 Components
 The abstraction for all objects in the structure

 Declare the interface that both leaf and composite objects


implements

 E.g., FileSystemItem (common interface for files and


directories)
12/06/2024 Design patterns Page 27
2. Structural design pattern

Composite pattern
 Leaf
 Represents individual objects in the hierarchy that don’t have children
 Implements all methods defined in the Component interface
 E.g., File
 Composite
 Represents composite objects that can have children (other
components)
 Implements methods to add, remove, and manipulate children, as well
as Component methods
 E.g., Directory

 Why use composite?


 Uniformity: the client can traverse and manipulate all elements in the
hierarchy uniformly without checking the type of each component
 Flexibility: make it easy to add new types of leaf and composite
components
12/06/2024 Design patterns Page 28
2. Structural

Composite
 Component interface  Leaf class

12/06/2024 Design patterns Page 29


2. Structural

12/06/2024 Design patterns Page 30


2. Structural

12/06/2024 Design patterns Page 31


2. Structural

Decorator
 Is a structural design pattern that allows you to dynamically add
new functionality to an object without altering its structure or
modifying its code

 It achieves this by ‘wrapping’ the object with additional behavior


through decorator classes

 Key concepts

 Core component: The object being decorated, which implements a


common interface or inherits from a base class

 Decorator class: wraps the core component and adds extra behavior
or responsibilities while still conforming to the same interface

12/06/2024 Design patterns Page 32


2. Structural

Decorator
 How it works?
 Both core component and decorator implement a shared
interface or inherit from the same base class
 The decorator class contains a reference to the core
component and delegates method calls to it, adding additional
behavior either before or after the delegation

12/06/2024 Design patterns Page 33


 Example: Assume to have a basic coffee object, and we want to
add options like milk, sugar, or whipped cream dynamically

12/06/2024 Design patterns Page 34


12/06/2024 Design patterns Page 35
12/06/2024 Design patterns Page 36
2. Structural

Decorator

12/06/2024 Design patterns Page 37


3. Behavioral

Strategy design pattern


 Defines a family of algorithms, encapsulates each one in a
separate class, and makes them interchangeable

 This allows the algorithm to vary independently from the context


using it

 Key concepts

 Strategy interface: define a common interface for all supported


algorithms
 Concrete strategies: implement the various algorithms, each in its
own class
 Context class: maintains a reference to a Strategy object and uses it
to execute the algorithm
 The strategy pattern used when
 You want to choose an algorithm’s behavior dynamically at runtime
 Multiple classes differ only in their behavior
12/06/2024 Design patterns Page 38
3. Behavioral

Strategy design pattern


 Example
 a payment system that supports multiple payment methods
(credit card, Paypal, and Google Pay)

12/06/2024 Design patterns Page 39


3. Behavioral

Strategy design pattern

12/06/2024 Design patterns Page 40


3. Behavioral

Strategy design pattern

12/06/2024 Design patterns Page 41


3. Behavioral

Strategy design pattern

12/06/2024 Design patterns Page 42


Summary
 Design patterns are reusable solutions to common software
design problems
 Creational patterns: focus on object creation to ensure
flexibility and reuse
 Structural patterns: focus on organizing and composing
objects for efficient relationships
 Behavioral patterns: focus on communication and
responsibility between objects

You might also like