☼
⌃
☰

Software Design Patterns

Table of Contents

Software Design Patterns

Software Design Patterns are general, reusable, and proven solutions to commonly occurring problems within a given context in software design. They are not finished designs that can be directly converted into code, but rather templates or blueprints for how to solve a problem in a way that can be used in many different situations.

The concept was popularized by the book Design Patterns: Elements of Reusable Object-Oriented Software, written by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, who are collectively known as the "Gang of Four" (GoF).

Using design patterns helps to speed up the development process by providing tested paradigms. It also improves code readability and provides a common language for developers to communicate about complex software interactions.

Design patterns are typically divided into three main categories: Creational, Structural, and Behavioral.

Creational Patterns

Creational patterns deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. They provide flexibility by decoupling the client from the object initialization process.

  • Singleton: Ensures a class has only one instance and provides a global point of access to it.
  • Factory Method: Defines an interface for creating an object, but lets subclasses alter the type of objects that will be created.
  • Abstract Factory: Provides an interface for creating families of related or dependent objects without specifying their concrete classes.
  • Builder: Separates the construction of a complex object from its representation, so the same construction process can create different representations.
  • Prototype: Creates new objects by copying an existing object (the prototype), rather than creating them from scratch.

Structural Patterns

Structural patterns focus on how classes and objects are composed to form larger, more flexible structures. They use inheritance and composition to establish relationships between different entities.

  • Adapter (Wrapper): Allows objects with incompatible interfaces to work together by converting the interface of one class into another that a client expects.
  • Decorator: Attaches additional responsibilities to an object dynamically without altering its structure.
  • Facade: Provides a simplified, unified interface to a more complex subsystem of classes, making it easier to use.
  • Composite: Composes objects into tree-like structures to represent part-whole hierarchies. It lets clients treat individual objects and compositions of objects uniformly.
  • Bridge: Decouples an abstraction from its implementation so that the two can vary independently.
  • Proxy: Provides a surrogate or placeholder for another object to control access to it.
  • Flyweight: Reduces the cost of creating and manipulating a large number of similar objects by sharing common parts of their state.

Behavioral Patterns

Behavioral patterns are concerned with the communication between objects and how responsibilities are assigned among them.

  • Observer: Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
  • Strategy: Defines a family of algorithms, encapsulates each one, and makes them interchangeable. This lets the algorithm vary independently from the clients that use it.
  • Command: Turns a request into a stand-alone object that contains all information about the request. This allows you to parameterize clients with different requests, queue requests, or log them.
  • State: Allows an object to alter its behavior when its internal state changes. The object will appear to change its class.
  • Chain of Responsibility: Passes a request along a chain of handlers. Upon receiving a request, each handler decides either to process it or to pass it to the next handler in the chain.
  • Iterator: Provides a way to access the elements of an aggregate object (like a list or collection) sequentially without exposing its underlying representation.
  • Mediator: Reduces dependencies between a set of interacting objects by having them communicate through a central mediator object instead of directly with each other.