Lec12 Pattern Design Text Extracted
Lec12 Pattern Design Text Extracted
Content
Design patterns
Page
Materials
Design patterns
Page
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
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
Page
1. Creational
Design patterns
Page
1. Creational
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
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
Design patterns
Page
1. Creational
Design patterns
Page
1. Creational
Design patterns
Page
1. Creational
Design patterns
Page
1. Creational
Design patterns
Page
1. Creational
Example
Use a Vehicle Factory to create related objects: Cars and Trucks for either
Electric and Gasoline engines
Design patterns
Page
1. Creational
Design patterns
Page
1. Creational
Design patterns
Page
1. Creational
Design patterns
Page
1. Creational
Design patterns
Page
Adapter pattern
Design patterns
Page
2. Structural
E.g.
Design patterns
Page
E.g.
Design patterns
Page
Composite pattern
Design patterns
Page
Composite pattern
Design patterns
Page
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
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
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
Design patterns
Page
3. Behavioral
Design patterns
Page
3. Behavioral
Example
a payment system that supports multiple payment methods (credit card,
Paypal , and Google Pay)
Design patterns
Page
3. Behavioral
Design patterns
Page
3. Behavioral
Design patterns
Page
3. Behavioral
Summary