The first article of 23 design patterns in java development

      A design pattern is a set of repeated, known, categorized, and catalogued summaries of code design experience. The purpose of using design patterns is to reuse the code, make the code easy to understand by others, and ensure the reliability of the code; there is no doubt that the design pattern is a win-win for others' systems, and the design pattern makes the code compilation truly engineering, Design patterns are the cornerstone of software engineering, like the bricks of a building. Reasonable use of design patterns in the project can perfectly solve many problems. Each design pattern has a corresponding principle in the present, and each pattern describes a recurring problem around us, as well as the core of the problem. solution, which is why it is widely used;

First, the classification of design patterns:

       In general, design patterns fall into three categories:

       There are five types of creational patterns: factory pattern, abstract factory pattern, singleton pattern, builder pattern, prototype pattern;

       There are seven structural modes: adapter mode, decoration mode, proxy mode, appearance mode, bridge mode, combination mode, and flyweight mode;

       There are eleven behavioral patterns: strategy pattern, template method pattern, observer pattern, iteration sub pattern, responsibility chain pattern, command pattern, memorandum pattern, state pattern, visitor pattern, mediator pattern, interpreter pattern;

      There are two types: concurrent mode and thread pool mode;

Second, the six principles of design patterns

1. Open Close Principle

      The open-closed principle means that it is open for extension and closed for modification. When the program needs to be expanded, the original code cannot be modified to achieve a hot-plug effect.

2. Liskov Substitution Principle

       Its official description is relatively abstract, you can Baidu by yourself. In fact, it can be understood like this:

       1. The ability of the subclass must be greater than or equal to the parent class, that is, the methods that the parent class can use, the subclass can use.

       2, the return value is the same reason. Suppose a parent class method returns a List and the child class returns an ArrayList, which of course works. It doesn't make sense if the parent class method returns an ArrayList and the child class returns a List. Here the ability of the subclass to return a value is smaller than that of the parent class. 3. There are cases where exceptions are thrown. Any subclass method can declare a subclass that throws the exception declared by the superclass method. And cannot declare to throw exceptions not declared by the parent class.

3. Dependence Inversion Principle

      This is the basis of the open-closed principle, specific content: interface-oriented programming, relying on abstraction rather than concreteness.

4. Interface Segregation Principle

      What this principle means is that it is better to use multiple isolated interfaces than to use a single interface. It also means reducing the degree of coupling between classes. From here, we can see that a design pattern is actually a software design idea, starting from a large-scale software architecture, in order to facilitate upgrades and maintenance. Therefore, it appears many times in the above: reduce dependencies and reduce coupling.

5. Demeter Principle (Least Knowing Principle) (Demeter Principle)

      Why is it called the principle of least knowledge, that is, an entity should interact with other entities as little as possible, so that the functional modules of the system are relatively independent.

6. Composite Reuse Principle

      The principle is to use composition/aggregation as much as possible instead of inheritance.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326078762&siteId=291194637