23 Design Patterns Learning Navigation (Java Full Version)

This article is a summary of the author’s study of 23 design patterns, including three categories of design patterns (5 creative types , 7 structural types, 11 behavioral types) and seven object-oriented design principles. In order to facilitate everyone's learning, the links to the 23 design patterns are now organized. I hope it will be useful to everyone. help. 

23 design patterns source code address:GitHub - lkydog/Design-Pattern: ⭐java implementation, 23 design patterns

1. What is a design pattern?

Design pattern is a very important programming technique in software development. It provides proven solutions that can make the code more readable, extensible and maintainable. We can abstract common patterns to form a A common set of solutions for different situations.

ps: Design patterns represent a kind of thought, and it’s up to everyone as to how they change.

2. Three major classifications of design patterns and corresponding patterns (navigation)

Note: The following star ratings for learning difficulty and usage frequency represent only personal opinions.

2.1 Creation mode (5 types)

Definition: It mainly involves the mechanism of object creation, avoiding direct creation of objects in an appropriate way, and reducing the burden of object creation and management during system operation.

1.Singleton PatternLearning difficulty:★☆☆☆☆< /span>★★★★☆, Usage rate:

Definition: Ensure that a class has at most one instance and provide a global access point.

Description: Including 2 types of Hungry Man style, 4 types of Lazy Man style, static inner class, enumeration and other implementation methods.

2.Factory Pattern【Learning difficulty:★★☆☆☆, use频率:★★★★★

Definition: Provide a public interface so that objects can be created without exposing object creation logic.

Description: Containssimple factory pattern, method factory pattern and abstract factory pattern. Simple factories and method factories take selling buns as an example, and abstract factories take selling buns and cakes as examples.

3. Abstract factory pattern (Factory pattern is already included and will not be described again)

4.Builder Pattern【Learning difficulty:★★★☆☆, Usage rate:★★☆☆☆

Definition: Encapsulates a complex object construction process and allows step-by-step construction.

Description: Take the KFC meal as an example.

5.Prototype PatternLearning difficulty:★★★☆☆★★★☆☆,Usage rate:

Definition: Create a new instance by copying an existing instance without knowing the corresponding class information.

Description: Take Sylas in League of Legends as an example of stealing other heroes’ ultimate moves.

2.2 Structural patterns (7 types)

Definition: Mainly focus on the way objects are combined, solve the relationship between objects, and solve the coupling and expansion problems between subsystems.

1. Adapter PatternLearning difficulty:★★☆☆☆★★★☆☆,Usage rate:

Definition: Allows cooperation between incompatible interfaces.

Description: Taking English-Chinese translation as an example, it includesclass adapter, object adapter, and interface adapter.

2.Decorator PatternLearning difficulty:★★★☆☆< /span>★★★☆☆, Usage rate:

Definition: Dynamically attach new functionality to an object.

Description: Take the blind monk learning skills (Q, W, E, R) in LOL as an example.

3. Proxy PatternLearning difficulty:★★☆☆☆★★★★☆,Usage rate:

Definition: Provide a proxy object for an object, and the proxy object controls the reference to the original object.

Description: Taking the housing agency as an example, it includesstatic proxy, JDK dynamic proxy and Cglib proxy.

4.Facade PatternLearning difficulty:★☆☆☆☆★★★★★,Usage rate:

Definition: Hides the complexity of the system and provides an interface to the client that can access the system.

Description: Take a travel agency as an example.

5.Bridge PatternLearning difficulty:★★★☆☆★★☆☆☆,Usage rate:

Definition: Separate the abstract part from its implementation part so that they can change independently.

Description: Take different brands of mobile phones and Bluetooth headsets as examples.

6.Composite PatternLearning difficulty:★★★☆☆★★★★☆,Usage rate:

Definition: Combining objects into a tree-shaped hierarchical structure to represent the "whole-part" relationship.

Description: Take arithmetic expressions as an example.

7.Kyoyuan Model-Flyweight PatternLearning Difficulty:★★★★☆< /span>★★☆☆☆, Usage rate:

Definition: Reduce the number of objects created through sharing to reduce memory usage and improve performance.

Description: Take shared bicycles as an example.

2.3 Behavioral patterns (11 types)

Definition: Mainly focused on communication and collaboration between objects, and solving interaction problems between classes or objects.

1.Strategy PatternLearning difficulty:★☆☆☆☆★★★★★,Usage rate:

Definition: Defines a series of algorithms and encapsulates each algorithm so that they can be replaced with each other.

Description: Take browsing videos and choosing a video platform as an example.

2.Template PatternLearning difficulty:★★☆☆☆★★★★☆,Usage rate:

Definition: Define the skeleton of the algorithm in an abstract class, leaving specific operations to subclasses.

Description: Take ordering takeout as an example.

3.Observer PatternLearning difficulty:★★★☆☆< /span>★★★★☆, Usage rate:

Definition: It defines a one-to-many dependency relationship between objects. When the state of an object changes, all its dependent objects will automatically receive notifications and update themselves.

Description: Take a newspaper and newspaper subscribers as an example.

4.Iterator PatternLearning difficulty:★★★☆☆< /span>★★★★☆, Usage rate:

Definition: Provides a unified way to access elements in a collection object without exposing the internal representation of the collection.

Description: Take traversing class students as an example.

5. Interpreter PatternLearning difficulty:★★★★★< /span>★☆☆☆☆, Usage rate:

Definition: Defines a language and defines interpreters for statements in the language, allowing programmatic definition of grammars, parsing of grammars, and interpretation of interpreters in grammars.

Description: Take the four arithmetic operations as an example.

6.Mediator PatternLearning difficulty:★★★☆☆< /span>★★☆☆☆, Usage rate:

Definition: Used to reduce the coupling between multiple objects and coordinate the interaction between objects by introducing a mediator object.

Description: Take the logistics company coordinating transportation companies and merchants as an example.

7.Chain Patternlearning difficulty:★★★☆☆< /span>★★☆☆☆, Usage rate:

Definition: Use a chain to process requests, which are passed sequentially along the chain until an object handles the request, thereby achieving the purpose of decoupling the request sender and the request handler.

Description: Take the school leave approval process as an example.

8.Command PatternLearning difficulty:★★★☆☆★★★★☆,Usage rate:

Definition: Encapsulate a request into an object so that different requests can be parameterized, queued, logged, undoed and redone.

Description: Take ordering at a restaurant as an example.

9.Memento PatternLearning difficulty:★★★☆☆★★★☆☆,Usage rate:

Definition: Allows the internal state of an object to be saved externally, allowing the object to be restored to its previous state when needed.

Description: Take this editor as an example.

10.State PatternLearning difficulty:★★★☆☆★★☆☆☆,Usage rate:

Definition: Allows an object to change its behavior when its internal state changes.

Description: Take the vending machine status as an example.

11.Visitor PatternLearning difficulty:★★★★☆< /span>★☆☆☆☆, Usage rate:

Definition: Used to define new operation methods without modifyingthe existing object structure.

Description: Take "tourist visit" as an example.

3. Seven Principles

The seven principles of design patterns, also known as "SOLID principles", are the basic principles of object-oriented design.

3.1 Single Responsibility Principle (SRP)

  • Concept:A class should have only one responsibility, that is, a class should only be responsible for the corresponding responsibilities in one functional area.
  • Example: For example, if a "Fruit" class includes both "Fruit basic attributes" and "Fruit storage method", then this class has two responsibilities, violating the single responsibility principle. The correct approach is to split "Fruit Basic Properties" and "Fruit Storage Method" into two categories.

3.2 Open-Closed Principle (OCP)

  • Concept:Open for extension, closed for modification.
  • Example: For example, in a calculator program, if the original code must be modified every time a new calculation method is added, then this program violates the open-closed principle. The correct approach is to write a separate class or method for each calculation method, so that when you need to add a new calculation method, you only need to add a new class or method without modifying the original code.

3.3 Liskov Substitution Principle (LSP)

  • Concept:Any place that references a base class object can transparently use its subclass object without affecting the correctness of the program.
  • Example: For example, an "animal" class and its subclass "bird" class. If the birds cannot inherit the methods of the animal class or need to override the methods of the animal class to meet its unique characteristics, then this subclass violates Liskov substitution principle.

3.4 Interface Segregation Principle (ISP)

  • Concept:Clients should not rely on interfaces that they do not need. That is, the dependence of a class on a class should be based on the smallest interface.
  • Example: For example, if a car class contains all interfaces that may be used by cars (acceleration, braking, steering, etc.), but some cars may not use these interfaces, then this class violates the principle of interface isolation. The correct approach is to put different interfaces into different classes.

3.5 Dependency Inversion Principle (DIP)

  • Concept:High-level modules should not depend on low-level modules, but on their abstractions. That is, modules should depend on interfaces or abstract classes rather than implementation classes.
  • Example: For example, in a sales management system, the low-level module "database operation" module depends on the high-level module "order management" module. Such a design will not only increase coupling, but also lead to data leakage. The correct approach is to put the abstract "database operation" module into a higher abstraction layer and be called by the "order management" module.

3.6 Law of Demeter (LoD)

  • Concept:Reduce the dependence between objects, that is, one object should know as little as possible about other objects and should not talk to strangers.
  • Example: For example, on an e-commerce website, if a class wants to send an email to notify customers of a successful order, the information should only include product information and customer account information, and should not include product warehouse information, logistics information, etc. Such a design It can reduce dependencies and avoid coupling between classes.

3.7 Composition/Aggregation Reuse Principle (CARP)

  • Concept:Priority should be given to using combination or aggregation relationship reuse instead of using inheritance relationship reuse.
  • Example: For example, a "car wheel" class and its subclass "off-road vehicle wheel" class, if an inheritance relationship is used to implement off-road vehicle wheels, it may cause the "off-road vehicle wheels" to be too bloated, and may not fully meet actual needs. The correct approach is to incorporate the two classes into the "car" class and reuse them using combination or aggregation relationships (that is, the "car" class has instances of the "car wheel" class and the "off-road wheel" class).

4. Summary 

Using design patterns correctly can improve the maintainability and scalability of the code, but using them incorrectly may increase code complexity, learning costs, and performance overhead. Again, design pattern is just a tool, don't use it for the sake of using design pattern.

Guess you like

Origin blog.csdn.net/weixin_45433817/article/details/131037102