A new interpretation of the 6 design principles of design patterns

Source: Internet
Author: User

One. " Single duty "principle (respnsibility Principle) SRP

The single responsibility principle is defined as: there should be and only one cause for class changes.

Benefits of a single responsibility principle:

1 , the complexity of classes is reduced, and what responsibilities are clearly defined;

2 , readability increases, complexity decreases, of course, readability is improved;

3 , maintainability improved, readability improved, it is easier to maintain;

4 , the risk of change caused by the reduction, change is essential, if a single interface to do a good job, an interface modification only to the corresponding implementation of the class has an impact on the other interfaces have no impact on the system's extensibility, maintainability has a great help.

The principle of single responsibility applies to interfaces, classes, and also to methods, what does it mean? One way to do as much as possible to do something. For example, a method "Modify user password", do not put this method in the "Modify user Information" method, this method of particles is very coarse.

For a single duty, the advice given is: the interface must be a single duty, class design as far as possible only one cause of change.

Note: The single-responsibility principle presents a standard for programming, with "responsibility" or "reason for change" to measure whether an interface or class is well designed, but "responsibility" and "reason for change" are not measurable and vary from project to environment.

two. the "Richter replacement" principle (Liskvo Substitution Principle) LSP

The Richter substitution principle is defined as: all references to the base class must be transparently used to the object of its child class.

The benefits of the Richter replacement principle:

1 , code sharing, reducing the effort to create classes, each of which has methods and properties of the parent class;

2 and improve the reusability of code;

3 , subclasses can resemble the parent class, but also different from the parent class, "Dragon Born Dragon, chicken born chicken, the mouse is born to play the hole" is said that the son has the father's "kind", "the world does not have two identical leaves" is indicates the son and the father difference;

4 , improve the extensibility of the code, the implementation of the method of the parent class can "do whatever", you can see that many of the open source framework of the extension interface is inherited by the parent class to complete;

5 , improve the openness of products or projects;

Of course, inheritance also has shortcomings, the disadvantages are as follows:

1 , inheritance is immersion. As long as inheritance, you must have all the properties and methods of the parent class;

2 and reduce the flexibility of the code. Subclasses must have all the properties and methods of the parent class, allowing the subclass to have more constraints in the free world;

3 and enhance the coupling. When the constants, variables, and methods of the parent class are modified, it is necessary to consider the modification of the subclass, and in the absence of a canonical environment, this modification can result in very bad results ---- Large pieces of code need to be refactored.

Note: If a subclass cannot implement a method of the parent class completely, or if some methods of the parent class have already "distorted" in the subclass, it is recommended that you break the parent-child inheritance relationship and replace the inheritance with dependencies, aggregation, composition, and so on. the Richter substitution principle can be used, but cannot be reversed, and in the place where the subclass appears, the parent may not be competent.

The Richter substitution principle defines a specification for good inheritance:

1 , subclasses must fully implement the method of the parent class;

2 , subclasses can have their own personality;

3 , overriding or implementing a method of the parent class, the input parameters can be magnified; ( a precondition for a method in a subclass ( that is, an incoming parameter type ) must be the same or looser than the predecessor condition of the method being overridden in the superclass ) .

4 , overwrite or implement the method of the parent class, the output result can be reduced;

three. the "dependency inversion" principle (dependence inversion Principle) DIP

the dependency inversion principle Java the expression in the language is:

1 , the dependencies between the modules can occur through abstraction, and the implementation class cannot have direct dependencies between them, and their dependencies are generated by interfaces or abstract classes;

2 , interface, or abstract class does not depend on the implementation class;

3 , implementing class-dependent interfaces or abstract classes;

a more streamlined definition is "interface-oriented programming" ------OOD one of the essence.

Benefits of the dependency inversion principle:

The dependency inversion principle can reduce the coupling between classes, improve the stability of the system, reduce the risk caused by concurrent development, and improve the readability and maintainability of the code.

  

Note: Whether the design has stability, as long as the appropriate "loose soil", observing "design blueprint" can also grow strong can be concluded that the stability of the design, in the surrounding environment changes frequently, can still do "I self-feuds."

A new interpretation of the 6 design principles of design patterns

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.