SWE : Design Pattern — Summary

Part 5 of Design Pattern series

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

Common Concepts in Design Pattern

Delegation & Composition

  • Decorator
  • Command
  • Strategy

Abstraction, New Interface

  • Bridge
  • Facade
  • Mediator

Enhancement

  • Decorator
  • Proxy
  • Visitor

Limit Object Creation, Save Resource

  • Flyweight
  • Proxy

Undo, Rollback

  • Memento

Copy, Clone

  • Memento

Input at Runtime

  • Command
  • Observer
  • State
  • Strategy

Recursion

  • Decorator
  • Chain of Responsibility
  • Command
  • Iterator

Step of Process

  • Template Method

Creational Design Pattern

  • At other times they are complementary: Abstract Factory might store a set of Prototypes from which to clone and return product objects. Abstract Factory, Builder, and Prototype can use Singleton in their implementation.

Abstract Factory

Builder

  • Builder lets you customize construction steps of objects. Abstract Factory returns the objects immediately.
  • You can use Builder to create Composite by recursive construction steps.
  • You can combine Builder with Bridge: the director class of Bridge act as abstraction, while Builder can act as implementation.

Prototype

  • Prototype can be used as simpler form of Memento, in case that state you want to store in the history, is fairly straightforward.
  • Applying the Prototype lets you clone complex structures like Composite.

Structural Design Pattern

Adapter

  • Decorator supports recursive composition, which isn’t possible with Adapters.

Bridge

Composite

  • Decorator adds additional functionality to the wrapped object, while Composite just sums up its component’s results.

Decorator

  • Decorator can be add recursively, Proxy can’t

Facade

  • Facade and Mediator have similar jobs — they try to organize collaboration between lots of subsystems.
  • Facade defines a simplified interface to subsystems but it doesn’t introduce any new functionality. The subsystems are unaware of the Facade — subsystems can communicate to each other directly.
  • Mediator centralizes communication between subsystems. Each subsystem is only know about the Mediator — subsystems cannot communicate to each other directly.

Flyweight

  • Flyweight would resemble Singleton as resource management.
  • But there are two fundamental differences between these patterns — Singleton should be only one instance, whereas a Flyweight class can have multiple instances with different intrinsic states. The Singleton object can be mutable. Flyweight objects are immutable.

Behavioral Design Pattern

  • Chain of Responsibility passes a sender request along a chain of potential receivers.
  • Command specifies a unidirectional connection of sender-receiver.
  • Mediator eliminates direct connections between senders and receivers, forcing them to communicate indirectly via Mediator.
  • Observer lets multiple receivers dynamically subscribe and unsubscribe to sender at run-time.

Chain of Responsibility

  • However, there are differences in detail of how to execute — The Chain of Responsibility handlers can execute arbitrary operations independently of each other. They can also stop passing the request further at any point.
  • On the other hand, Decorator can extend the object’s behavior while keeping it consistent with the base interface. In addition, Decorator aren’t allowed to break or stop the flow of the request.
  • Handlers in Chain of Responsibility can be implemented as Command. In this case, you can execute a lot of different operations over the same context object, represented by a request.
  • There’s another approach, where the request itself is a Command. In this case, you can execute the same operation in a series of different contexts linked into a chain.

Command

  • You can use Command and Memento together when implementing undo or queue.
  • Command and Strategy may look similar because you can use both to parameterize an object to take action.
  • However, they have very different intents — Command lets you defer execution of the operation by queuing or storing the history of commands, then execute commands to specific object from outside.
  • On the other hand, Strategy usually describes different ways of doing things and let you swap these algorithms inside object and execute on its own.

Interpreter

  • Terminal symbols within Interpreter’s abstract syntax tree can be shared with Flyweight.

Template Method

  • Strategy is based on composition — Strategy uses delegation to vary the entire algorithm. you can alter parts of the object’s behavior by supplying it with different strategies that correspond to that behavior.
  • Template Method works at the class level, so it’s static. Strategy works on the object level, letting you switch behaviors at runtime.

Visitor