Understanding Design Patterns: Creational, Structural, and Behavioral Categories

Below is a detailed overview of all the 23 classic design patterns introduced by the “Gang of Four” (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides) in their book “Design Patterns: Elements of Reusable Object-Oriented Software.” These patterns are grouped into three categories: Creational, Structural, and Behavioral patterns.

1. Creational Patterns

These patterns deal with object creation mechanisms, trying to create objects in a manner suitable to the situation.

1.1. Singleton

  • Intent: Ensures that a class has only one instance and provides a global point of access to that instance.
  • Use Cases:
    • When you need exactly one instance of a class to control access to shared resources.
  • Example: A configuration manager that reads settings from a file and provides them throughout an application.

1.2. Factory Method

  • Intent: Defines an interface for creating an object, but lets subclasses alter the type of objects that will be created.
  • Use Cases:
    • When a class cannot anticipate the class of objects it needs to create.
  • Example: A document editor that can create different types of documents (e.g., text, spreadsheet) based on user input.

1.3. Abstract Factory

  • Intent: Provides an interface for creating families of related or dependent objects without specifying their concrete classes.
  • Use Cases:
    • When the system needs to be independent of how its products are created, composed, and represented.
  • Example: A UI toolkit that can create windows, buttons, and scrollbars for different operating systems.

1.4. Builder

  • Intent: Separates the construction of a complex object from its representation, allowing the same construction process to create different representations.
  • Use Cases:
    • When the construction process of an object is complex.
  • Example: Building different meal combinations (vegan, non-vegan) in a restaurant.

1.5. Prototype

  • Intent: Creates new objects by copying an existing object, known as a prototype.
  • Use Cases:
    • When the type of objects to create is determined by a prototypical instance.
  • Example: Cloning complex objects like graphic editors where creating a new instance from scratch is costly.

2. Structural Patterns

These patterns deal with object composition or the arrangement of objects to form larger structures.

2.1. Adapter

  • Intent: Allows incompatible interfaces to work together. It converts the interface of a class into another interface clients expect.
  • Use Cases:
    • When you need to use an existing class, and its interface does not match the one you need.
  • Example: A card reader that acts as an adapter between a memory card and a USB interface.

2.2. Bridge

  • Intent: Decouples an abstraction from its implementation so that the two can vary independently.
  • Use Cases:
    • When you want to avoid a permanent binding between an abstraction and its implementation.
  • Example: A graphics library that can work with different rendering engines (e.g., OpenGL, DirectX).

2.3. Composite

  • Intent: Composes objects into tree structures to represent part-whole hierarchies. Clients can treat individual objects and compositions of objects uniformly.
  • Use Cases:
    • When you have objects that must be composed into tree-like structures.
  • Example: A file system, where files and directories are treated uniformly.

2.4. Decorator

  • Intent: Adds additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
  • Use Cases:
    • When you need to add functionality to individual objects without affecting other objects.
  • Example: Adding scrolling, borders, and shadows to a text box.

2.5. Facade

  • Intent: Provides a simplified interface to a complex subsystem.
  • Use Cases:
    • When you want to provide a simple interface to a complex system.
  • Example: A user-friendly interface to a complex library of classes in a financial system.

2.6. Flyweight

  • Intent: Reduces the cost of creating and manipulating a large number of similar objects by sharing as much data as possible.
  • Use Cases:
    • When you need to support a large number of objects that are similar in some way.
  • Example: Characters in a text editor, where each character is represented by a small object sharing common data.

2.7. Proxy

  • Intent: Provides a surrogate or placeholder for another object to control access to it.
  • Use Cases:
    • When you need to control access to an object (e.g., for lazy initialization, access control, logging).
  • Example: A security proxy that controls access to sensitive operations in a system.

3. Behavioral Patterns

These patterns are concerned with algorithms and the assignment of responsibilities between objects.

3.1. Chain of Responsibility

  • Intent: Passes a request along a chain of handlers. Each handler can either process the request or pass it to the next handler in the chain.
  • Use Cases:
    • When multiple objects can handle a request, but the handler is not known a priori.
  • Example: Event handling systems, where events are passed through a chain of handlers.

3.2. Command

  • Intent: Encapsulates a request as an object, thereby allowing for parameterization of clients with queues, requests, and operations.
  • Use Cases:
    • When you want to parameterize objects with operations, delay execution of an operation, or queue a request.
  • Example: A menu system in a GUI application where each menu item is associated with a command.

3.3. Interpreter

  • Intent: Defines a grammar for a language and an interpreter that uses the grammar to interpret sentences in the language.
  • Use Cases:
    • When you have a language that needs to be interpreted.
  • Example: Parsing and evaluating mathematical expressions or commands in a scripting language.

3.4. Iterator

  • Intent: Provides a way to access elements of a collection object sequentially without exposing its underlying representation.
  • Use Cases:
    • When you need to traverse a collection without exposing its internal structure.
  • Example: Iterating over a list of items in a collection, such as a list or array.

3.5. Mediator

  • Intent: Reduces coupling between classes by using a mediator object to handle communication between them.
  • Use Cases:
    • When you have multiple classes that communicate with each other, and you want to simplify their interactions.
  • Example: A chat application where the mediator handles messages between users.

3.6. Memento

  • Intent: Captures and externalizes an object’s internal state so that the object can be restored to this state later, without violating encapsulation.
  • Use Cases:
    • When you need to save and restore the state of an object.
  • Example: Undo functionality in text editors.

3.7. Observer

  • Intent: Defines a one-to-many dependency between objects, so when one object changes state, all its dependents are notified and updated automatically.
  • Use Cases:
    • When an object’s state change should trigger updates in other objects.
  • Example: A news agency that notifies subscribers when a new article is published.

3.8. State

  • Intent: Allows an object to alter its behavior when its internal state changes. The object will appear to change its class.
  • Use Cases:
    • When an object’s behavior depends on its state, and it must change its behavior at runtime.
  • Example: A vending machine that behaves differently when in different states (e.g., waiting for coin, dispensing item).

3.9. Strategy

  • Intent: Defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
  • Use Cases:
    • When you need to switch between different algorithms dynamically.
  • Example: Different sorting algorithms that can be swapped out depending on the situation.

3.10. Template Method

  • Intent: Defines the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing its structure.
  • Use Cases:
    • When you have multiple classes that share a common structure but differ in specific steps.
  • Example: An abstract class defining a method to create a game, where subclasses define the specific steps to set up the game.

3.11. Visitor

  • Intent: Represents an operation to be performed on elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.
  • Use Cases:
    • When you need to perform operations on a collection of objects of different types.
  • Example: Tax calculation

References

Leave a comment

I’m Tran Minh

Hi, I’m Trần Minh, a Solution Architect passionate about crafting innovative and efficient solutions that make technology work seamlessly for you. Whether you’re here to explore the latest in tech or just to get inspired, I hope you find something that sparks joy and curiosity. Let’s embark on this exciting journey together!

Let’s connect