Design Patterns

Creational

Abstract factory

Factory method

  • Java

    Traditional approach is based on abstract class.

    The better modern approach is based on interface and default method.

Singleton

Prototype

  • Specifies the kind of objects to create using a prototypical instance.
  • The client replaces all references to the new operator with calls to the factory method.

Builder

  • Java

    • java.util.stream.Stream

Structural

Adapter

  • Intent

    Converts one interface to another so that it matches what the client is expecting

Facade

  • Intent

    Provides a simplified interface

Flyweight

Decorator

  • Intent

    Dynamically adds responsibility to the interface by wrapping the original code

  • Notes

    • Adding new behaviors to an individual object dynamically
    • A flexible alternative to subclassing for extending functionality
      • When using subclassing, different subclasses extend a class in different ways. But an extension is bound to the class at compile-time and can't be changed at runtime.
    • The desired behaviors can be composed by adding different decorator objects to the base class object.
    • Both the target object and the decorator implement the same interface.
  • Java

    • Use Lambda and java.util.function.Function to simplify creation of decorator objects
  • Resources

Composite

Behavioral

Chain-of-responsibility

  • Make requests pass a single processing pipeline that contains many possible handlers.
  • Need to efficiently process the requests without hard-wiring handler relationships and precedence, or request-to-handler mappings.
  • Examples

Template Method

  • Define the skeleton of an algorithm in an operation, deferring some steps to client subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure
  • Base class declares algorithm 'placeholders', and derived classes implement the placeholders

Observer

  • Subject holds a list of Observers, and notifies them by calling their update method when target action is triggered
  • Reactive Programming is a modern Observer pattern implementation

Command

  • Create objects encapsulating actions and parameters
  • FP: Could use lambda to simplify creation of action objects

Iterator

  • Access the elements of an aggregate object in a sequential way

  • Abstract out the traversal of an aggregate object

  • Decouple data structure and algorithms

  • Java

    • java.lang.Iterable

State

  • Can be used to model a state machine

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

  • Java

    • Use sealed class to limit the number of states and make the state machine more robust

Strategy

  • Notes

    • Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it.
  • Java

    • Use Lambda to simplify creation of strategy objects
    • java.util.Comparator

Visitor

Reference