### Design Patterns

The foundation of reusable object-oriented software

Design pattern is a set of repeatedly used, most people know, cataloged code Design experience summary. Design patterns are used to re-use code, make it easier for others to understand, and ensure code reliability. There is no doubt that design patterns are a win-win for yourself, others and systems. Design patterns make coding truly engineering. Design patterns are the cornerstones of software engineering, like the bricks and stones of a mansion. The rational application of design patterns in projects can perfectly solve many problems. Each pattern has corresponding principles to correspond to it at present. Each pattern describes a recurring problem around us and the core solution to the problem, which is also the reason why it can be widely used. This chapter is the beauty of Java [from novice to master evolution] series of design patterns, we will combine theory and practice to study this chapter, hope that the majority of program lovers, learn design patterns, do an excellent software engineer! ## 1. Classification of design patterns

Overall, design patterns fall into three categories:

There are five types of creation patterns: factory method pattern, Abstract factory pattern, singleton pattern, Builder pattern, and prototype pattern.

Structural mode, a total of seven: adapter mode, decorator mode, agent mode, appearance mode, bridge mode, composite mode, share mode.

There are eleven behavioral modes: strategy mode, template method mode, observer mode, iteration sub-mode, responsibility chain mode, command mode, memo mode, state mode, visitor mode, intermediary mode, interpreter mode.

There are actually two more types: syndication mode and thread pool mode. Use a picture to describe the whole picture:

## 2. Six principles of design patterns

1. Open Close Principle

The open closed principle means open for extensions, closed for modifications. When the program needs to be extended, you cannot modify the original code to achieve a hot plug effect. So a word summary is: in order to make the program expansibility, easy to maintain and upgrade. To achieve this, we need to use interfaces and abstract classes, which will be discussed later in the concrete design.

2. Liskov Substitution Principle

One of the basic principles of object-oriented design. Richter’s rule of substitution says that wherever a base class can appear, a subclass must appear. LSP is the cornerstone of inheritance reuse. The base class can be reused only when the derived class can replace the base class and the function of the software unit is not affected. The derived class can also add new behaviors on the base class. Richter’s substitution principle is a complement to the open – close principle. The key step in implementing the “open – close” principle is abstraction. The inheritance relationship between base class and subclass is the concrete realization of abstraction, so the Richter substitution principle is the specification of the concrete steps to realize abstraction. — Baidu Encyclopedia

3, Dependence Inversion Principle

This is the basis of the open close principle, the concrete content: true interface programming, relies on abstraction, not on concrete.

4. Interface Segregation Principle

This principle means that it is better to use multiple isolated interfaces than a single one. From here, we can see that in fact, design mode is a software design idea, starting from large software architecture, in order to upgrade and maintenance convenience. So many times above: reduce dependency, reduce coupling.

5. Demeter Principle

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

Composite Reuse Principle

The rule is to use composition/aggregation rather than inheritance whenever possible.

## 3, Java 23 design pattern creation pattern, altogether five kinds:

  • Factory method pattern
  • Abstract Factory pattern
  • The singleton pattern
  • Builder model
  • The prototype pattern

Structural mode, a total of seven:

  • Adapter mode
  • Decorator mode
  • The proxy pattern
  • The appearance model
  • The bridge model
  • Portfolio model
  • The flyweight pattern

There are eleven behavioral patterns:

  • The strategy pattern
  • Template method pattern
  • Observer model
  • Iterative subpattern
  • Chain of Responsibility model
  • Command mode
  • Memo mode
  • The state pattern
  • Visitor pattern
  • The mediator pattern
  • Interpreter mode.