Introduction#
Design Patterns are a set of code design experiences that are repeatedly used in software design, aimed at improving the reusability, scalability, and maintainability of code. These patterns were summarized and refined by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, who detailed these patterns in their book "Design Patterns," thus they are also known as GoF (Gang of Four).
Design Principles#
- Open/Closed Principle: Software should be open for extension but closed for modification.
- Liskov Substitution Principle: Subclass objects should be able to replace all instances of their parent class without affecting the correctness of the program.
Overview of Design Patterns#
Abstract Factory#
Provides an interface for creating a family of related or dependent objects without specifying their concrete classes.
Adapter#
Converts the interface of a class into another interface that clients expect, allowing classes that cannot work together due to incompatible interfaces to work together.
Bridge#
Separates abstraction from its implementation so that both can vary independently.
Builder#
Separates the construction of a complex object from its representation so that the same construction process can create different representations.
Chain of Responsibility#
Gives multiple objects a chance to handle a request, thereby avoiding coupling between the sender and receiver of the request.
Command#
Encapsulates a request as an object, allowing you to parameterize clients with different requests.
Composite#
Composes objects into tree structures to represent part-whole hierarchies.
Decorator#
Dynamically adds additional responsibilities to an object.
Facade#
Provides a unified interface to a set of interfaces in a subsystem.
Factory Method#
Defines an interface for creating an object, letting subclasses decide which class to instantiate.
Flyweight#
Uses sharing to effectively support a large number of fine-grained objects.
Interpreter#
Given a language, defines a representation for its grammar and an interpreter that uses this representation to interpret sentences in the language.
Iterator#
Provides a way to sequentially access the elements of an aggregate object.
Mediator#
Encapsulates a set of object interactions with a mediator object.
Memento#
Captures an object's internal state without violating encapsulation and saves this state outside the object.
Observer#
Defines a one-to-many dependency between objects.
Prototype#
Specifies the kinds of objects to create using a prototypical instance and creates new objects by copying this prototype.
Proxy#
Provides a surrogate or placeholder for another object to control access to it.
Singleton#
Ensures that a class has only one instance and provides a global access point to that instance.
State#
Allows an object to alter its behavior when its internal state changes.
Strategy#
Defines a family of algorithms, encapsulates each one, and makes them interchangeable.
Template Method#
Defines the skeleton of an algorithm in an operation, deferring some steps to subclasses.
Visitor#
Represents an operation to be performed on the elements of an object structure.