Design Patterns Design Patterns seven principles

Source: Jiujiang Website Optimization

A single responsibility principle

When coding, regardless of the method or the class should abide by the principle of single responsibility.

Notes and details:

    1. Reduce the complexity of the class, a class is only responsible for a duty;
    1. Improve the readability of the class, maintainability;
    1. Reduce risks caused by the change;
    1. Under normal circumstances, we should abide by the principle of single responsibility, only logic is simple enough, it can violate the principle of responsibility in a single code level; only a small number of class methods enough to maintain a single responsibility principle at the method level.

Second, the interface segregation principle

It does not should not rely on the interface when the business end of development, i.e., a class dependent on another class of the interface should be based on a minimum.

Third, the Dependency Inversion principle

    1. High-level modules should not depend on low-level modules, both of which should rely on its abstract;
    1. Abstract should not rely on the details, the details should depend abstract;
    1. Dependency Inversion (inversion) The central idea is oriented programming interface;
    1. Rely on the reverse design principle is based on the design concept: the details of the relative variability, abstract things much more stable. Abstract-based architecture to build than to detail the basis to build the infrastructure much more stable. In java, it refers abstract interface or an abstract class, details of that specific category;
    1. The purpose of using an interface or abstract class is to develop a good standard, but not related to specific operations, the task to show the details of their implementation classes to complete.

Notes and details:

    1. Lower module are possible abstract class interface, or both, and are, better stability program.
    1. Declare the types of variables as much as possible is an interface or an abstract class, so we have between variable references, and the actual object, there is a buffer layer, is conducive to the expansion and optimization program.
    1. Follow the Richter replace principles inherited.

Fourth, Richter substitution principle

    1. All uses of the base class, which must be transparent to the use of subclasses;
    1. When using inheritance, in a subclass try not to override the parent class method;
    1. Inheritance allows two classes actually enhanced the coupling, where appropriate, by polymerization, in combination, to solve the problem dependent.

OO inheritance of thinking and explanation:

    1. Inheritance contain this layer of meaning: Any parent who has achieved a good way, in fact, in setting specifications and contract, although he is not required to comply with all subclasses of these contracts, but if a subclass of these methods has been achieved will be modification, it will cause damage to the entire inheritance hierarchy.
    1. In succession to the program designed to bring convenience, but also brings disadvantages. For example, the use of inheritance will bring invasive procedures, reduce the portability of the program, to increase the coupling between objects, if a class is inherited by other classes, then when you need to modify this class, we must take into account all subclasses, and, after the parent class is modified, all subclasses are likely to malfunction.
    1. So in programming, how to use inheritance correct? Richter substitution principle

Fifth, the principle of opening and closing

    1. Program development, the most important and basic design principles;
    1. A software entity, such as classes, modules and functions should be open for extension but closed for modification. Abstract framework for building, extension detail Entity.
    1. When the software needs to be changed, as far as possible to achieve change by extending the behavior of the software entity, rather than to achieve change through existing code.
    1. Programming follow other principles, and design patterns principle purpose is to open and close.

Sixth, Demeter

    1. An object should be kept to a minimum understanding of other objects;
    1. The more closely the relationship between class and class, the greater the degree of coupling;
    1. Demeter also known as the smallest known rule that a class of their dependent classes know, the better. That is, no matter how complex the dependent classes, as much as possible the internal logic is encapsulated in the class. In addition to providing a method of external public and not to reveal any information;
    1. Demeter there is a more simple definition: only direct communication with friends;
    1. Direct Friends: Each object has with other objects in the system coupling relationship, as long as the coupling relationship between two objects, we say that friendship between the two objects. Many coupling mode, dependent, associated, combined, polymerization. Among them, we call appearance member variables, method arguments, method return values ​​directly in the class friends, and appeared in local variables of the class are not direct friends appear. That strange class, it is best not inside the class in the form of local variables.

Seven, synthetic multiplexing principle

Synthesis can be used, the polymerization manner, try not to use inheritance.

Because if you want to use the knowledge of another class, use inheritance will enhance the coupling between them. 

The core idea of ​​the design principles

    1. Identify the applications may vary at them independent, and do not need to change the code that mix;
    1. For interface programming, rather than programming;
    1. To loosely coupled interaction between objects and work.

Guess you like

Origin www.cnblogs.com/1994july/p/12078759.html