Six object-oriented principles of a law

Six object-oriented principles of a law

Single Responsibility law:
a class that the only thing to do.
Single rule duties want to say is "high cohesion", write code only six words of the ultimate principle of "high cohesion, low coupling", so-called high cohesion is a code module only completed a feature, object-oriented if a class can make it do things that do not involve other areas is practicing the principle of high cohesion, this class only a single responsibility. Another is modular, a good software system, which functions inside each module should be able to easily get the other systems in use, so as to achieve the goal of taking software.

The principle of opening and closing
software entities should be open for extension, but closed for modification.
In an ideal world, when we need to add new functionality to a software, just from the original class to derive new classes can be, does not require any modification of the original line of code. To achieve the opening and closing has two main points: 1 abstraction is critical: if a system is not an abstract class or interface, then the system is no extension point; variability Package 2: The packaging system of the various variables to an inheritance hierarchy, if a plurality of variable elements mixed together, the system becomes complicated and confusing.

Oriented programming interface:
return type, variable straightforward to say that principles and the specific point is the parameter type declarations of methods, the method of reference types, try to use abstract types rather than specific types because abstract type can be arbitrarily him a child replaced class.

Richter substitution principle:
any time you can replace a parent class with subclasses;
simply put, is to use local parent class will be able to type with subclasses. Richter substitution principle can check inheritance is reasonable, if a violation of the inheritance Richter substitution principle, then this is definitely the wrong inheritance, the need for code refactoring. Note that: subclass must increase the capacity of the parent class rather than reduce the ability of the parent class, because the ability to ability than the parent class to be a subclass of more and more ability to target objects as the ability to use less of course, there is no problem.

Interface segregation principle:
the interface to be small and specialized, not comprehensive.
Bloated interface is the contamination of the interface, since the interface represents the capacity, the interface should only indicate an ability to interface should be a high cohesive.
For example, Daoqiangjianji, ax hook fork should be designed to eight interfaces, each interface implements a feature, not write these functions in an interface, after all, not everyone has these capabilities, they will be separated from that person that function will also need to implement this interface can be, after all, it is to support multi java implementation. Interface represents the ability to Java, on behalf of the agreement, on behalf of roles, whether the proper use of the interface must be important to identify the level of programming levels.

Synthetic polymeric multiplexing principle:
the priority relationship polymerization or synthetic code reuse.
To code reuse through inheritance is an object-oriented programming is the most abused things. There are three kinds of relationship between classes, respectively, the relationship Is-A, Has-A relationship relationships and Use-A, representing inheritance relationships and dependencies. Wherein the association relationship according to which the degree of association may be further divided into association, polymerization and synthesis, but that white is Has-A relationship, synthetic polymeric multiplexing principles want to express priority Has-A relationship instead of Is-A relationship complex with code.
Remember: any time not inherit tools, tools to use and not for inheritance.

Demeter:

迪米特法则又叫最少知识原则,一个对象应该对其他对象有尽可能少的了解。目的是为了减少系统的耦合度和复杂度。
Published 10 original articles · won praise 0 · Views 118

Guess you like

Origin blog.csdn.net/DONG__CHI/article/details/103896238