Why learn GoF Design Patterns?

"Designs that do not account for change may be at risk of significant redesign in the future.": Erich Gamma

Patterns are a typical solution to common problems in object-oriented design. When a solution comes up again and again in various projects, eventually someone will give it a name and describe it in detail. This is how patterns are discovered.

Patterns are like pre-made blueprints that we can customize to solve recurring problems in our code. A pattern is not a specific piece of code, but a general idea or concept to solve a specific problem. We can follow the pattern details and implement a program-appropriate solution.

In 1995, Erich Gamma, John Vlissides, Ralph Johnson and Richard Helm published the book "Design Patterns: Elements of Reusable Object-Oriented Software" (Chinese: Design Patterns: Reusable Object-Oriented Software Design), they applied design patterns for programming. It's called the GOF book or Gang of Four's book because of the long name.

Why learn these design patterns

Design patterns are a set of proven solutions to common software design problems. Even if you have never encountered these problems, learning design patterns is still valuable because it teaches us how to use the principles of object-oriented design to solve various problems.

Design patterns also facilitate communication between team members by providing pattern terminology.

Classification of Design Patterns

Design patterns fall into three categories:

  1. Creation mode  : Provides an object creation mechanism that increases the flexibility and reusability of existing code.
  2. Structural Patterns  : Explains how to assemble objects and classes into larger structures while maintaining the structure's flexibility and efficiency.
  3. Behavioral Patterns  : Responsible for effective communication and assignment of responsibilities between objects.

Create design patterns

Creational patterns provide various object creation mechanisms that increase the flexibility and reuse of existing code.

Five creational design patterns are defined in the GoF Design Patterns:

  1. Singleton  : A singleton is a creational design pattern that ensures that a class has only one instance and provides a global point of access to that instance.
  2. Factory  : The factory method is a creational design pattern that provides an interface for creating objects in a superclass while allowing subclasses to change the type of objects created.
  3. Abstract Factory  : An abstract factory is a creational design pattern that allows to generate classes of related objects without specifying their concrete classes.
  4. Builder  : A builder is a creative design pattern that builds complex objects incrementally. Different types and representations of objects can be created using the same construction code.
  5. Prototype  : Prototype is a creational design pattern that allows you to copy existing objects without making your code dependent on their class.

Structural Design Patterns

Structural design patterns explain how to assemble objects and classes into larger structures while maintaining their flexibility and efficiency.

Seven structural design patterns are defined in GoF Design Patterns:

  1. Adapter  : Adapter is a structural design pattern that allows objects with incompatible interfaces to communicate.
  2. Composite  : Composite is a structural design pattern that allows you to compose objects into tree structures and then work with these structures as if they were single objects.
  3. Proxy  : Proxy is a structural design pattern that allows to provide a placeholder or substitute for another object. Using a proxy, you can control access to the original object, allowing certain operations to be performed before or after the request is passed to the original object.
  4. Flyweight  : With flyweight, you can fit more objects into RAM by sharing a common part of the state among multiple objects instead of keeping all the data in each object.
  5. Facade  : Facade is a design pattern that provides a simplified interface to a library, framework, or any other collection of classes.
  6. Bridging : Allows splitting a large class or group of closely related classes into two separate hierarchies: abstraction and implementation, for independent development.
  7. Decorator  : Using the decorator design pattern, new behavior can be attached to an object by placing the new behavior in a special wrapper object that contains the new behavior.

Behavioral Design Patterns

Behavioral patterns involve algorithms and the assignment of responsibilities between objects.

There are 11 behavioral design patterns defined in the GoF Design Patterns:

  1. Template method  : A template method defines the skeleton of an algorithm in a superclass, allowing subclasses to override specific steps without modifying its structure.
  2. Mediators  : Using mediators, you can reduce confusing dependencies between objects. This pattern restricts direct communication between objects and forces them to cooperate only through intermediaries.
  3. Chain of Responsibility:  Chain of Responsibility allows you to pass requests along a chain of handlers. After receiving a request, each handler decides whether to process the request or pass it on to the next handler in the chain.
  4. Observer  : The Observer pattern allows you to define a mechanism to notify multiple objects of events occurring on the object they are observing.
  5. Strategies  : Using Strategies, you can define a family of algorithms, put them into different classes, and make their objects interchangeable.
  6. Command  : Command is a behavioral design pattern that converts requests into self-contained objects that contain all the information about them. With this transformation, you can use various request parameterization methods, delay or queue the execution of requests, and support undoable operations.
  7. State  : As a behavioral design pattern, state allows an object to change its behavior when its internal state changes. It looks as if the object changed its class.
  8. Visitor  : Using the Visitor pattern, you can separate algorithms from the objects they operate on.
  9. Interpreter  : An interpreter is a behavioral design pattern
    that defines a grammatical representation of a language and provides an interpreter to process that grammar.
  10. Iterators  : A behavioral design pattern called iterators allows you to iterate over the elements of a collection without exposing their underlying representation.
  11. Memento : The memento pattern allows saving and restoring the previous state of an object without revealing its implementation details.

The above is a brief introduction to the 23 patterns. In the next month's article, let's learn these design patterns in detail.

Guess you like

Origin blog.csdn.net/stone1290/article/details/126925353