First, the six principles of design patterns

Six principles of design patterns

[1], the principle of opening and closing

Open-Close Principle (OCP): a software entity, such as classes, modules and functions should be open for extension but closed for modification. The purpose of the program is to ensure scalability, easy maintenance and upgrades.

Opening and closing principle is known as the cornerstone of object-oriented design, in fact, other principles can be seen as a realization of the principle of opening and closing of tools and instruments. Means: software should be open for extension, modification is closed, it is popular, the development of a software function should be extended, while making these extensions, it does not require modifications to the original program.

Benefits are: the availability of the software is very flexible, and strong expansion. When a new feature, you can add new modules to meet the new demands. In addition, as the original module is not modified, so do not worry about stability.

[2], Single Responsibility Principle

Single-Responsibilitiy Principle (SRP): for a class, it should be the cause of it is just a change. If more than one motivation to change a class, then the class will have more than one role, you should separate out the extra duties, go create some classes to complete each role.

For example: a person several roles, but these things less relevant, even conflict, and that he would not be a good solution to these problems responsibilities should be assigned to different people do it.

Single responsibility principle is the best way to achieve high cohesion and low in the coupling, not one.

[3], Richter substitution principle

Liskov Substitution Principle: subclass can extend the functionality of the parent class, but can not change the parent class of the original function.

In the first principle of opening the closure principle, that "abstract" and "polymorphism." Maintain design encapsulation "Abstract" is the language provides functionality, "polymorphic" implemented by inheritance semantics. So how to measure the quality of inheritance relationship?

The answer is: inheritance must explicitly ensure that the superclass (parent class) owned properties are still set up in a subclass.

In object-oriented thinking, an object is a combination of a set of states and a series of actions. Status is an inherent characteristic of an object, the behavior is the external characteristics of the object. LSP formation is expressed in the same inheritance hierarchy should have common behavioral characteristics.

[4], Dependency Inversion Principle

Dependence Inversion Principle (DIP): calling a rules between class and class. Here is coupling-dependent code. Layer module should not rely on the underlying module, both of which should depend abstract; abstract does not depend on the details; details should depend abstraction. Interface programming.

The main idea is this: If a member of a class or a specific type of parameter to be, then this class will depend on the specific type. If an inheritance structure, the upper member of a class or a lower parameter type, then the inheritance structure is dependent on the underlying level, or should try for abstract programming interface.

For example: the presence of a Driver class, a Car object members, and a Driver () method, a Car object has two methods start () and stop (). Obviously Driver dependence Car, Driver class that is called method Car class. But when the increase of support for the Bus Driver class class (there is a need to open the bus driver), you must change the code in the Driver, undermines the open closed principle. Fundamental reason is that the coupling of the top and bottom of the Car Driver class of the class only together. One solution is: The Bus Car class and abstract class, the introduction of an abstract class Automoble. The Car and Bus is a generalization of the Automobile.

After such a transformation find that rely on the underlying high-level, top and bottom at the same time become dependent on abstraction. This is the Dependency Inversion principle of nature.

[5], the interface segregation principle

Interface segregation principle (Interface Segregation Principle): for dividing roles and appropriate interfaces, it has two meanings: 1, it does not rely on the user should not an excuse; 2, dependencies between classes should be based on the smallest interface .

These two definitions summarized in one sentence: the establishment of a single interface, instead of the bloated interface. Popular is this: try to refine the interface, as far as possible while ensuring less interface methods. When an interface contains a lot of behavior will lead to normal relations are not dependent on the client to do is separate interface that allows decoupling.

Back to the Single Responsibility Principle above, requiring separate interface Interface refined behavior, feeling somewhat the same. But in fact, single responsibility principle of duty class with a single interface, focusing on the responsibility, the interface does not require as much as possible less.

Interface segregation principle, it is required to make use of a plurality of dedicated interfaces. Special interface that is provided to interface to multiple modules. To provide several modules should have several interfaces, instead of creating a bloated interface, all modules can be accessed.

But the design of the interface is limited. The more flexible system interface design smaller the particle size, it is true, but it also makes the interface too complex structure, difficult maintenance. So in practice, how to grasp the experience and common sense to rely on the development.

[6], the principles of Demeter

Law of Demeter (minimum knowledge of principles): An object should have a minimal understanding of other objects. Popular, is that a class of their own need to know the couple, or at least call the classes within your class how complicated, I do not care, that's your thing, I know you have so many common method, I can call.

Dimitris principle does not want to establish direct contact between classes. If you really need to have contact, then to convey by their friend class. For example: You need to buy a house, there are three suitable properties for sale A, B, C now, but you do not have to go directly to the real estate to buy a flat, but in the sales office to understand the situation. This reduces the coupling between you (buyers) and two real estate classes.

But the application of the principle of Demeter is likely to result in a consequence: the system there are a lot of intermediary classes (such as sales offices above class) exist to call each other the relationship between the transfer class, which will It increases the complexity of the system extent.

Dimitris core principle idea is: decoupling between classes, weak coupling.

Guess you like

Origin www.cnblogs.com/lee0527/p/11873100.html