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

Lec12 Pattern Design Text Extracted

Uploaded by

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

Lec12 Pattern Design Text Extracted

Uploaded by

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

Design Patterns

Vũ Thị Hồng Nhạn


([email protected])
Dept. Software Engineering, FIT, UET,
Vietnam National Univ., Hanoi

Content

Design patterns

Page

Benefits of design patterns


3 Categories of design patterns
Creational design patterns
Structural design patterns
Behavioral design patterns

Materials

Bruce Eckel , Thinking in Patterns


Erich Gamma, Design Patterns – Elements of Reusable Object-Oriented Software

Design patterns

Page

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

Design patterns

Page

Introduction

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++

Design patterns

Page

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 between objects

Design patterns

Page

Three categories…

Design patterns

Page

Singleton

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

Design patterns

Page

1. Creational

Example using singleton


Design patterns

Page

1. Creational

Example using singleton…

Design patterns

Page

1. Creational

2 references to Singleton object

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

Design patterns

Page

Design patterns

Page

1. Creational

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 metho d , 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

Factory method pattern

Design patterns
Page

1. Creational

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

Factory method pattern

Design patterns

Page

1. Creational

Factory method pattern

Design patterns

Page

1. Creational

Factory method pattern…

Design patterns

Page

1. Creational

Factory method pattern…

Design patterns

Page

1. Creational

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
Abstract factory pattern

Design patterns

Page

1. Creational

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

Abstract factory pattern

Design patterns

Page

1. Creational

Abstract factory pattern

Design patterns

Page

1. Creational

Abstract factory pattern

Design patterns

Page

1. Creational

Abstract factory pattern

Design patterns

Page

1. Creational

Abstract factory pattern

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

Design patterns

Page

Adapter pattern

Design patterns

Page

2. Structural

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

E.g.

Design patterns

Page

E.g.

Design patterns

Page

Composite pattern

Design patterns

Page

2. Structural design 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)

Composite pattern

Design patterns

Page

2. Structural design 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

Composite

Design patterns

Page

2. Structural

Component interface

Leaf class

Design patterns

Page

2. Structural

Design patterns
Page

2. Structural

Decorator

Design patterns

Page

2. Structural

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

Decorator

Design patterns

Page

2. Structural

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

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

Design patterns

Page
Design patterns

Page

Design patterns

Page

Design patterns

Page

Decorator

2. Structural

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

Design patterns

Page

3. Behavioral

Strategy design pattern

Design patterns

Page

3. Behavioral

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

Strategy design pattern

Design patterns

Page

3. Behavioral

Strategy design pattern

Design patterns

Page

3. Behavioral

Strategy design pattern

Design patterns

Page

3. Behavioral

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