Previous Microservices-design-patterns Factory-vs-Abstract-Factory-Pattern Next

Design Patterns

What Are Design Patterns?

Design patterns are recurring solutions to common problems in software architecture.

One of the most cited definitions comes from Christopher Alexander:

“Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.”

Interestingly, Alexander was referring to architecture—buildings and structures. The Gang of Four adapted this concept to software development.

The Gang of Four

The Gang of Four (GoF) refers to the authors of the influential book Design Patterns: Elements of Reusable Object-Oriented Software—Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. Originally published in 1994, the book remains highly relevant today.

GoF Design Pattern Categories

The GoF cataloged 23 design patterns, grouped into three main categories:

  • Creational Patterns – Deal with object creation mechanisms.
  • Structural Patterns – Concerned with object composition and relationships.
  • Behavioral Patterns – Focus on communication between objects.
Creational Patterns
  Abstract FactoryCreates an instance of several families of classes
  BuilderSeparates object construction from its representation
  Factory MethodCreates an instance of several derived classes
  PrototypeA fully initialized instance to be copied or cloned
  SingletonA class of which only a single instance can exist

Mnemonic for Creational Design Patterns

Mnemonic Phrase: A Big Friendly Panda Sleeps

  • A → Abstract Factory
  • B → Builder
  • F → Factory Method
  • P → Prototype
  • S → Singleton
Structural Patterns
  AdapterMatch interfaces of different classes
  BridgeSeparates an object’s interface from its implementation
  CompositeA tree structure of simple and composite objects
  DecoratorAdd responsibilities to objects dynamically
  FacadeA single class that represents an entire subsystem
  FlyweightA fine-grained instance used for efficient sharing
  ProxyAn object representing another object

Mnemonic for Structural Design Patterns

Mnemonic Phrase: All Brave Cats Dance For Fancy Parties

  • All → Adapter
  • Brave → Bridge
  • Cats → Composite
  • Dance → Decorator
  • For → Facade
  • Fancy → Flyweight
  • Parties → Proxy
Behavioural Patterns
  Chain of Resp.A way of passing a request between a chain of objects
  CommandEncapsulate a command request as an object
  InterpreterA way to include language elements in a program
  IteratorSequentially access the elements of a collection
  MediatorDefines simplified communication between classes
  MementoCapture and restore an object's internal state
  ObserverA way of notifying change to a number of classes
  StateAlter an object's behaviour when its state changes
  StrategyEncapsulates an algorithm inside a class
  Template MethodDefer the exact steps of an algorithm to a subclass
  VisitorDefines a new operation to a class without change

Mnemonic for Behavioral Design Patterns

Mnemonic Phrase: Calm Cats Interpret Ideas, Making Memories Of Strong Strategic Thinking Ventures

  • Calm → Chain of Responsibility
  • Cats → Command
  • Interpret → Interpreter
  • Ideas → Iterator
  • Making → Mediator
  • Memories → Memento
  • Of → Observer
  • Strong → State
  • Strategic → Strategy
  • Thinking → Template Method
  • Ventures → Visitor

Design Patterns Overview

Creational Patterns

Creational patterns provide mechanisms to instantiate single objects or groups of related objects. There are five key patterns:

  • Abstract Factory: Provides a client with a set of related or dependent objects. The "family" of objects is determined at run-time.
  • Builder: Creates complex objects with constituent parts that must be constructed in a specific order or using a defined algorithm. An external class controls the construction process.
  • Factory Method: Replaces class constructors, abstracting object creation so the type can be determined at run-time.
  • Prototype: Instantiates a new object by cloning an existing one. Useful when object creation is resource-intensive.
  • Singleton: Ensures only one instance of a class exists. All references point to the same underlying object.

Structural Patterns

Structural patterns define relationships between classes or objects to form larger structures.

  • Adapter: Bridges incompatible interfaces by wrapping the adaptee with a compatible class.
  • Bridge: Separates abstraction from implementation, allowing changes without affecting the abstraction.
  • Composite: Builds recursive tree structures where individual and composite objects are treated uniformly.
  • Decorator: Dynamically extends or modifies object behavior by wrapping it with a decorator class.
  • Facade: Provides a simplified interface to a complex subsystem.
  • Flyweight: Minimizes memory usage by sharing common data among many similar objects.
  • Proxy: Acts as a placeholder for another object, adding a level of indirection and control.

Behavioral Patterns

Behavioral patterns define communication strategies between objects and classes.

  • Chain of Responsibility: Passes requests along a chain of handlers until one processes it.
  • Command: Encapsulates a request as an object, allowing for parameterization and queuing.
  • Interpreter: Defines a grammar and interprets expressions within that grammar.
  • Iterator: Provides a standard way to traverse elements in a collection without exposing its structure.
  • Mediator: Centralizes communication between objects to reduce direct dependencies.
  • Memento: Captures and restores an object's state without violating encapsulation.
  • Observer: Allows objects to subscribe and react to changes in another object's state.
  • State: Alters an object's behavior based on its internal state, enabling dynamic transitions.
  • Strategy: Defines a family of algorithms, selecting one at run-time based on context.
  • Template Method: Outlines the steps of an algorithm, allowing subclasses to override specific steps.
  • Visitor: Separates operations from object structures, enabling new functionality without modifying classes.
Previous Microservices-design-patterns Factory-vs-Abstract-Factory-Pattern Next
*