Design patterns series | take you meet design patterns

Hello everyone, I am Wolf King, a programmer who loves playing ball

This is the first design Patterns article, and we start the series with brain maps, the Six Principles, pattern classification, and an introduction to 22 design patterns

1. Six principles

“Design patterns follow six principles” :

1) Single Responsibility Principle (SRP). Classes and methods do only one thing.) 2) Liskov Substitution Principle. 3) Dependence Inversion Principle (DIP) 4) Interface Segregation Principle (ISP for short) 5) “Law of Demeter” (LoD). 6) “Open closed principle” (Abstract architecture, extended implementation, general: Open for extension, closed for modification)

2. Classification of design patterns

“Design patterns can be divided into three categories in terms of their capabilities:

1) “Creative pattern” : This pattern provides a mechanism for creating objects, improving the flexibility and reusability of existing code. 2) “Structural patterns” : This pattern describes how to assemble objects and classes into a larger structure while keeping the structure flexible and efficient. 3) “Behavior patterns” : These patterns are responsible for effective communication between objects and delegation of responsibilities.

3. 22 design modes

3.1 Create Mode

“1: Factory Method Pattern”

It provides a method to create an object in the parent class, allowing subclasses to decide what type to instantiate the object.

“2: Abstract Factory Pattern”

It can create a series of related objects without specifying their concrete classes.

“3: Builder Mode”

Enables you to create complex objects step by step. This pattern allows you to generate objects of different types and forms using the same creation code.

“4: Prototype Mode”

Allows you to copy existing objects without having to make your code dependent on the class to which they belong.

“5: Singleton Mode”

Allows you to ensure that there is only one instance of a class and provide a global node to access that instance.

3.2 Structural mode

“1: Adapter Mode”

It enables objects with incompatible interfaces to cooperate with each other.

“2: Bridge Mode”

A large class or series of closely related classes can be split into two separate hierarchies of abstraction and implementation that can be used separately at development time.

“3: Combination Mode”

You can use it to group objects into a tree structure and use them as if they were individual objects.

“4: Decorator Mode”

Allows you to bind new behavior to an object by putting it in a special wrapper that contains behavior.

“5: Appearance Mode”

Provides a simple interface for libraries, frameworks, or other complex classes.

“6: Enjoy Yuan Mode”

Instead of storing all data in each object, it lets you load more objects into your limited memory by sharing the same state shared by multiple objects.

“7: Proxy Mode”

Allows you to provide a substitute for an object or its placeholder. The proxy controls access to the original object and allows some processing before and after the request is submitted to the object.

3.3 Behavior Patterns

“1: Chain of Responsibility Model”

Allows you to send requests down the handler chain. Upon receipt of the request, each handler can either process it or pass it on to the next handler on the chain.

“2: Command Mode”

It transforms the request into a single object that contains all the information associated with the request. This transformation allows you to parameterize methods, delay request execution, or queue them based on different requests, and implement undoable operations.

“3: Iterator Pattern”

Allows you to traverse all the elements of a collection without exposing the underlying representation of the collection (lists, stacks, trees, etc.).

“4: The Mediator Model”

It allows you to reduce the clutter of dependencies between objects. This pattern restricts direct interaction between objects, forcing them to collaborate through a mediator object.

“5: Memo Mode”

Allows you to save and restore an object’s previous state without exposing its implementation details.

“6: Observer Mode”

Allows you to define a subscription mechanism that notifies multiple other objects that “watch” an object when an event occurs.

“7: State Mode”

Allows you to change the behavior of an object as its internal state changes, making it look as if it were changing its own class.

“8: Strategy Mode”

It allows you to define a series of algorithms and put each algorithm into a separate class so that the objects of the algorithm can be replaced with each other.

“9: Template Method Pattern”

It defines a framework for an algorithm in a superclass that allows subclasses to override specific steps of the algorithm without modifying the structure.

“10: Visitor Pattern”

It isolates the algorithm from the object it works on.