Introduction to Java Design Patterns

Introduction to Java Design Patterns

 

 

First, the classification of design patterns

 

Overall, the 23 design patterns fall into three categories:

 

1. Creation mode

 

    5 kinds: factory method pattern, abstract factory pattern, singleton pattern, builder pattern, prototype pattern.


2. Structural model

 

 7 kinds: adapter mode, decorator mode, proxy mode, appearance mode, bridge mode, combination mode, flyweight mode.

 

3. Behavioral Model

 

    11 kinds: strategy pattern, template method pattern, observer pattern, iterative sub pattern, responsibility chain pattern, command pattern, memorandum pattern, state pattern, visitor pattern, mediator pattern, interpreter pattern.

 

 

 

2. 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. So one sentence summary is: in order to make the program expandable, easy to maintain and upgrade. To achieve this effect, we need to use interfaces and abstract classes, which we will mention in the specific design later.

 

2. Liskov Substitution Principle

 

Liskov Substitution Principle (LSP) is one of the basic principles of object-oriented design. The Liskov Substitution Principle states that wherever a base class can appear, a subclass must appear. LSP is the cornerstone of inheritance and reuse. Only when the derived class can replace the base class and the function of the software unit is not affected, the base class can be truly reused, and the derived class can also add new behaviors based on the base class. . The Liskov substitution principle is a complement to the "open-closed" principle. A key step in implementing the "open-closed" principle is abstraction. The inheritance relationship between the base class and the subclass is the concrete realization of abstraction, so the Liskov substitution principle is the specification of the concrete steps to realize abstraction.

 

3. Dependence Inversion Principle

 

This is the basis of the open-closed principle, the specific content: true programming to the interface, 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.

 

 

Guess you like

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