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

Design Patterns 201

The document discusses software design patterns, which are standard solutions to common programming problems, enhancing code flexibility and understandability. It categorizes design patterns into three types: Creational, Structural, and Behavioral, detailing specific patterns within each category. Additionally, it covers the Factory Pattern, Abstract Factory Pattern, and Singleton Pattern, providing examples and consequences of their use in software development.

Uploaded by

sishahed420
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)
9 views26 pages

Design Patterns 201

The document discusses software design patterns, which are standard solutions to common programming problems, enhancing code flexibility and understandability. It categorizes design patterns into three types: Creational, Structural, and Behavioral, detailing specific patterns within each category. Additionally, it covers the Factory Pattern, Abstract Factory Pattern, and Singleton Pattern, providing examples and consequences of their use in software development.

Uploaded by

sishahed420
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

CSE 3421

Design Pattern

MD. RAFI-UR-RASHID
LECTURER, DEPT. OF CSE, UIU
Why This Lesson?
One of the Most Fundamental Concepts in
OOP based Software Development
What is a software design pattern?
• A standard solution to a common programming problem

• It is a description or template that can be reused in many different


situations

• A technique for making code more flexible or efficient

• Ensures Loose coupling in your code

• Increase understandability of your program


4
Gang of Four

• 23 design patterns
• Three categories:
❑ Creational
❑ Structural
❑ Behavioral

5
Creational Patterns

• How objects are instantiated

• Five creational patterns


• Factory method
• Abstract factory
• Singleton
• Builder
• Prototype

6
Structural Patterns
• How objects / classes can be
combined

• Seven structural patterns


• Adapter
• Bridge
• Composite
• Decorator
• Façade
• Flyweight
• Proxy
7
Behavioral Patterns
• How object communicate

• Eleven behavioral patterns


• Interpreter
• Template Method
• Chain of Responsibility
• Command
• Iterator
• Mediator
• Memento
• Observer
• State
• Strategy
• Visitor
8
UML Revisited

Aggregation: “is part of”

Composition: “is entirely made of”

Generalization
Dependency: “uses”
9
Factory Pattern
Factory Method

• Define an interface for creating related classes.

• A factory class takes responsibility of object instantiation through a factory


method.

• Client gets class instance using that factory method.

11
Example: Shape Drawing

<<interface>> Painter
Shape + main()
+ draw()

Circle Square Rectangle ShapeFactory


+getShape(type):Shape
+ draw() + draw() + draw()

12
Example: Kiosk

<<interface>> Kiosk
Payment
+ main()
+ processPayment()

PaymentFactory
Cash CreditCard Coupon
+getPayment(type): Payment
+ prcessPayment() + prcessPayment() + prcessPayment()

13
Consequences
• Factory pattern removes the instantiation of actual implementation
classes from client code.
• Factory pattern makes our code more robust, less coupled and easy
to extend. For example, we can easily change lower class
implementation because client program is unaware of this.
• Factory pattern provides abstraction between implementation and
client classes through inheritance.

14
Practice Problems
Draw UML diagram for following scenarios using appropriate design
patterns:
• In travel site, we can book train ticket as well bus tickets and flight
ticket. In this case user can give his travel type as ‘bus’, ‘train’ or
‘flight’.

• Consider a Garment Factory which produces various types of


garments like shirt, pant, trousers. The consumers can request for the
required types of garments through the factory.

15
Abstract Factory Pattern
Abstract Factory Method

• Provide an encapsulation mechanism to a group of factories.

• Each factory can be accessed through an abstract factory class.

• It uses the factory design pattern as a sub-concept, since each of the factories
follows the factory pattern here.

17
AbstractFactory FactoryProducer Arts
Example: Arts +getFactory(type): + main()
+getShape(type):Shape AbstractFactory
+getColor(name):Color

PaintFactory AnimationFactory
+getShape(type):Shape +getShape(type):Shape
+getColor(name):Color +getColor(name):Color

<<interface>> <<interface>>
Shape Color
+ draw() + fill()

Circle Square Red Green


+ draw() + draw() + fill() + fill()
18
VehicleFactory FactoryProducer
Example: Vehicle Vehicle
+ createWheels() +getFactory(type): + main()
+ createEngine() VehicleFactory

CycleFactory CarFactory
+ createWheels()
+ createWheels() + createEngine()

<<Interface>>
Parts
+stuffPart()
Engine Wheels
+stuffPart() +stuffPart()

CivicEngine AccordEngine AlloyWheels SteelWheels


+addCivicEngine() +addAccordEngine() +addAlloyWheel() +adSteelWheel()

19
Consequences
• Abstract Factory pattern is “factory of factories” and can be easily
extended to accommodate more products.
• Isolates the concrete classes from client.
• Promotes consistency among products

20
Practice Problems
Draw UML diagram for following scenarios using appropriate design
pattern:
• In a coffee shop, there are tea and coffee to serve to the customers.
Both tea and coffee need some ingredients to make them up like coffee
powder, tea leaves, sugar etc.

• We have two groups of utensils Microwave safe and non microwave


safe products. If we need microwave safe products we should use
microwave safe bowl, plate and cup. We can not mix microwave safe
and non microwave safe. ([Link]
pattern/)
21
Singleton Pattern
Singleton Method

• Ensure a class has only one instance, and provide a global point of access to it.

• Facilitates Lazy initialization

23
Example: Database Connection

DbConnection
- instance: DbConnection
Client

+ main()
- DbConnection()

+ getInstance(): DbConnection

24
Example: HTTP Connection

HTTPconnection
- instance: HTTPconnection
Client

+ main()
- HTTPconnection()

+ getInstance(): HTTPconnection

25
Thank You

You might also like