0% found this document useful (0 votes)
5 views14 pages

Design Patterns 3

The document discusses behavioral design patterns in software development, focusing on how objects communicate and change behavior based on their internal state. It highlights eleven patterns, including State and Strategy, providing examples and UML diagrams for each. Additionally, it includes practice problems for drawing UML diagrams based on real-world scenarios.

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)
5 views14 pages

Design Patterns 3

The document discusses behavioral design patterns in software development, focusing on how objects communicate and change behavior based on their internal state. It highlights eleven patterns, including State and Strategy, providing examples and UML diagrams for each. Additionally, it includes practice problems for drawing UML diagrams based on real-world scenarios.

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
Behavioral Patterns
• How object communicate

• Eleven behavioral patterns


• State
• Strategy
• Interpreter
• Template Method
• Chain of Responsibility
• Command
• Iterator
• Mediator
• Memento
• Observer
• Visitor
2
State Pattern
State Method

• Allow an object to alter its behavior when its internal state changes.

• Object's behavior is a function of its state, and it must change its


behavior at run-time.

4
Example: Remote

5
OnState OffState

+pressSwitch(Remote):int +pressSwitch(Remote):int

<<Interface>>
SwitchState

+pressSwitch(Remote):int

Remote
- state: SwitchState

+ Remote(SwitchState) Client
+ getState(): SwitchState +main()
+ setState(SwitchState)
+ pressButton()

6
Example: Queue

7
Busy Available

+doAction(counter):int +doAction(counter):int

<<Interface>>
State

+doAction(counter):int

Counter
- state: State
- count
+ Counter(State) Client
+ getState(): State +main()
+ setState(State)
+ addNew()
+ remove()

8
Practice Problems
Draw UML diagram for following scenarios using appropriate design
patterns:
• Think of the traffic lights. The red, green and yellow colors refer to
particular state of the vehicular regulation.

• Think of the different sound profiles in a mobile phone. You can


switch between general, silent, and vibration.

9
Strategy Pattern
Strategy Method

• Define a family of algorithms, and make them interchangeable

• Select the behavior of an algorithm dynamically at runtime.

11
StrategyAdd StrategySub StrategyMul

+doAction(int a, int b):int +doAction(int a, int b):int


+doAction(int a, int b):int

<<Interface>>
Strategy

+doAction(int a, int b):int

OpContext
- strategy: Strategy

Client
+ applyStrategy(int a, int b):int +main()

12
BubbleSort QuickSort InsertionSort

+sort(int[] a) +sort(int[] a)
+sort(int[] a)

<<Interface>>
SortingStrategy

+sort(int[] a)

SortingContext
- strategy: SortingStrategy

Client
+ applyStrategy(int[] a) +main()

13
Practice Problems
Draw UML diagram for following scenarios using appropriate design
patterns:
• Depending on the weather we clothe us differently. For instance, we
wear light cloths under sun, raincoat in overcast, and airtight fabrics
during winter.
• Think of the different postures a cricket umpire shows on different
events.

14

You might also like