SWE : Behavioral Design Pattern

Part 4 of Design Pattern series

Pisit J.
Pisit J.
Mar 4 · 8 min read
image

1. Chain of Responsibility

  • Link several handlers in specific order of chain. While passing a request to the chain, each handler is asked whether it can process it.

Pros

Cons

  • Others may reach the end of the chain unhandled.

Example

Middleware — linkWith & check & checNext
Middleware — Concrete Class
setMiddleware
middleware.linkWith & setMiddleware
Output — UserExistsMiddleware — Login successfully
Output — ThrottlingMiddleware — Request limit

Reference

2. Command

  • Decouple classes that invoke operations — Invoker — from classes that perform these operations — Operator — by declaring the Command interface.
  • Command can be used to queue operations, schedule their execution, or execute them remotely.
  • Command can be used to implement reversible operations — undo & rollback mechanics.

Pros

Cons

Example — Queue with Command

Command — execute
Invoker — addCommand & executeCommand
Operator
Main
Output

Reference

Example — Undo with Command

Command — backup & undo & execute
Command History — push & pop
Editor — init Command
Editor — executeCommand & undo
Editor
Command
Command History — Backup & Undo

Reference

3. Interpreter

  • Define & evaluate language grammar or expression by parsing context.
  • Use Case — Musician is example of Interpreter. The pitch of a sound and its duration can be represented in musical note. This note provides the language of music. Musician can reproduce the original pitch and duration of each sound by interpreting musical note.

Example

Main — Context & Interpreter
Output

Reference

4. Iterator

Pros

  • New traversal operations can be defined without changing its interface.

Cons

Example

Iterator — hasNext & next
Main

Reference

5. Mediator

Pros

  • You can introduce new mediators with no effect to the any existing components.

Cons

6. Memento

  • Capture and export internal state so that the object can later be restored to specific state.

Pros

Cons

Example

Memento — getState
Originator — setState, createMemento, restoreState
CareTaker — addMemento, popMemento
Main
Output

Reference

7. Null Object

Example

AbstractCustomer
ValidCustomer
InvalidCustomer for Null Object
Main
Output

Reference

8. Observer

  • Provide loose coupling by defining one-to-many relation of publisher & subscribers.
  • Publisher is responsible for maintaining a list of subscribers and notifying them of state changes by calling their update() operation.
  • Subscribers are responsible for subscribing or unsubscribing themselves on publisher.

Pros

  • You can establish subscribe or unsubscribe at runtime — subscribers can join or leave the list whenever they need to.

Cons

Example

Subscriber — execute
Publisher — subscribe & unsubscribe & notify
Main — subscribe & unsubscribe
4 subscription s— log on open, email on open, log on save & email on save — are notified.
only 2 remaining subscriptions — log on save & email on save — are notified.

Reference

9. State

  • State-specific behavior should be defined independently — adding new states should not affect the behavior of existing states.
  • Use Case — The buttons in your smartphone behave differently depending on the current state of the device — When the phone is unlocked, pressing buttons leads to executing various functions. When the phone is locked, pressing any button leads to the unlock screen. When the phone’s charge is low, pressing any button shows the charging screen.

Pros

Cons

10. Strategy

  • Use Case — For sorting algorithm, you can implement by various methods such as bubble sort, merge sort & quick sort.

Pros

Cons

Example

Strategy
Context — setStrategy & executeStrategy
Main

Reference

11. Template Method

Pros

Cons

Example

Template & runTemplateMethod
Process extends Template
Another process extends Template
Main
Output

Reference

12. Visitor

  • Provide the new behavior as a separate class called Visitor, instead of trying to integrate it into existing classes. The existing classes are now passed to specific method of Visitor as an argument to execute that new behavior.

Pros

  • Visitor can gather data from each class they are working with.

Cons

  • You need to make sure that Visitor has necessary access to the private fields and methods of the each class.

Example

Existing Class — accept Visitor as argument.
Visitor with specific method — visit — for each existing class.
Main
Output

Reference