Java 23 full factorial design patterns

A classification, design patterns

Overall, the design patterns into three categories:

Create a schema, a total of five categories: factory method pattern, abstract factory pattern, singleton, the builder pattern, prototype model.

Structural model, a total of seven kinds: adapter mode, decorative mode, proxy mode, the appearance mode, bridge mode, combination mode, Flyweight.

Behavioral patterns, a total of eleven kinds: Strategy pattern, the template method pattern, observer mode, iterator pattern, the responsibility chain mode, command mode, the memo mode state mode, the visitor pattern, intermediary model to explain the mode.

In fact, there are two types: concurrency pattern and thread pool mode. With a picture to describe the whole:

 

Six principles Second, the design patterns

General principles: the principle of opening and closing (Open Close Principle)

That is the principle of opening and closing open for extension, but closed for modification. When the program needs to expand, not to modify the original code, but to expand the existing code to achieve the effect of a hot-swappable. So summarized in one sentence: In order to make the program better scalability, ease of maintenance and upgrades. Want to achieve this effect, we need to use interfaces and abstract classes, etc., follow the detailed design, we will mention this point.

1. Single Responsibility Principle

Do not type more than one cause of change, which means that each class should implement a single responsibility, failing that, they should split the class.

2, Richter substitution principle (Liskov Substitution Principle)

One of the basic principles of Richter substitution principle (Liskov Substitution Principle LSP) object-oriented design. Richter substitution principle in that place any base class can appear, sub-class will be able to appear. LSP is inherited cornerstone multiplexed only when the derived class can replace the base class, the software unit of functionality is not affected, the base class can really be reused, derived class can add new behavior on the basis of the base class on . Richter substitution principle is - to add "on-off" principle. To achieve "open - closed" principle is a key step in abstraction. The inheritance relationship with a base class is a subclass of the abstract concrete realization, so Richter substitution principle is the specification for concrete steps to achieve the abstract. - From Baidu Encyclopedia

History substitution principle, the subclass try not to rewrite and overloading of the parent class. Because the parent class represents a well-defined structure, through this interface specifications to interact with the outside world, the subclass should not just destroy it.

3、依赖倒转原则(Dependence Inversion Principle)

这个是开闭原则的基础,具体内容:面向接口编程,依赖于抽象而不依赖于具体。写代码时用到具体类时,不与具体类交互,而与具体类的上层接口交互。

4、接口隔离原则(Interface Segregation Principle)

这个原则的意思是:每个接口中不存在子类用不到却必须实现的方法,如果不然,就要将接口拆分。使用多个隔离的接口,比使用单个接口(多个接口方法集合到一个的接口)要好。

5、迪米特法则(最少知道原则)(Demeter Principle)

就是说:一个类对自己依赖的类知道的越少越好。也就是说无论被依赖的类多么复杂,都应该将逻辑封装在方法的内部,通过public方法提供给外部。这样当被依赖的类变化时,才能最小的影响该类。

最少知道原则的另一个表达方式是:只与直接的朋友通信。类之间只要有耦合关系,就叫朋友关系。耦合分为依赖、关联、聚合、组合等。我们称出现为成员变量、方法参数、方法返回值中的类为直接朋友。局部变量、临时变量则不是直接的朋友。我们要求陌生的类不要作为局部变量出现在类中。

6、合成复用原则(Composite Reuse Principle)

原则是尽量首先使用合成/聚合的方式,而不是使用继承。

Java的23中设计模式详解:链接

 

Guess you like

Origin www.cnblogs.com/lucky1024/p/11226056.html