java in the "six principles of a law."

1. Single Responsibility Principle

A class only do what it should do.

Single Responsibility Principle want to express is the "high cohesion", write the code the ultimate principle of only six words "high cohesion, low coupling", so-called high cohesion is a code module only completed a feature, object-oriented , if you just let it complete a class thing to do, and not to the field and it is not related to practicing the principles of high cohesion, this class only single responsibility. Another modular, good bicycle is assembled vehicle, the damper from the fork, to the transmission brakes, all parts are re-assembled and can be disassembled, the table tennis racket good shot is not finished, and the rubber base must be removed points and self-assembly, a good software system, each functional module inside it also should be able to easily get the other systems in use, so as to achieve the goal of software reuse. Simple point is distinct division of work.

2. Open Closed Principle

Software entities should be open for extension, but closed for modification.

In an ideal state, when we need to add new functionality to a software system, just from the original system to derive new classes can, without modifying the original line of code. To do the opening and closing there are two main points:

  1. Abstraction is critical, if not a system, 抽象类or 接口the system is no extension point;

  2. Variability package, the package a variety of variables in the system to a inheritance hierarchy, if a plurality of variables mixed together, the system becomes complex and chaotic change, if the package is not clear how the variability can refer " chapter in a book on bridge mode to explain design patterns refined solution. "

3. Dependency Inversion principle

Oriented programming interface, polymorphism.

When this principle is right parameter types and specific as straightforward method is to declare the return type of a reference type variable method, using as abstract types rather than specific types, because the abstract type can be replaced by any of its sub-type Please refer to the following Richter substitution principle. Reference blog

4. Richter substitution principle

Any time you can replace the parent type sub-type.

Description of the substitution principle Richter, Ms. Barbara Liskov description is much more complex than this, but simply means that a parent can be the type of place we will be able to use subtypes. Richter substitution principle can check inheritance is reasonable, if a violation of the inheritance Richter substitution principle, then the inheritance must be wrong, the need for code refactoring. For example let the cat inherits a dog, cat or dog inheritance, or let rectangular square inherit inheritance are wrong, because you can easily find a violation of the principle of replacing the Richter scene. Note that: subclass must increase the capacity of the parent class rather than reduce the ability of the parent class, because the more ability the child analogy parent class, the ability to target as much less ability to use objects of course without any problem.

5. Interface Segregation Principle

Interface to small and specialized, must not be comprehensive.

Bloated interface is the contamination of the interface, since the interface represents ability, then only one interface should describe a capability within the interface should be highly cohesive. For example, poetry and painting should be four interfaces are designed to be and should not be designed as a four interface method, because if a method designed to four interfaces, then this interface is difficult to use, after all, poetry and painting four samples are proficient or a small number of people, and if designed four interfaces, several will be to achieve several interfaces, so the possibility of being multiplexed each interface is very high. 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.

6. The synthetic polymeric multiplexing principles

Or preferentially synthesized polymerization relationship code reuse.

To code reuse through inheritance is an object-oriented programming is the most abused thing, because all the textbooks without exception inheritance was preached and thereby mislead the beginner, between classes Simply put, there are three relationships , Is-A关系, Has-A关系, Use-A关系, represent 继承, 关联and 依赖. Wherein the association relationship according to the intensity of its associated can be further divided into 关联, 聚合and 合成, but that white is Has-A relationship, synthetic polymeric multiplexing principles want to express priority Has-A relationship instead of Is-A relationship between multiplexing Code, the reason, can themselves find a million reasons from Baidu, should be noted that, even in the Java API, there are many examples of abuse of inheritance, such as Properties class inherits the Hashtable class, Stack class extends the Vector class, the inheritance is obviously wrong, the better practice is to place a member of a Hashtable class type in the Properties and its keys and values are set to strings to store data, while the Stack class design should also be put in the Stack class a Vector object to store data.

Remember: any time not inherit tools, tools that can be owned and used, rather than bring inherited.

7. Demeter

Demeter also known as the principle of a minimum of knowledge, an object should have as little knowledge of other objects.

More complex systems can provide the user with a simple facade, Java Web development as a front controller Servlet or Filter Is not it a facade, server browser works ignorant, but by front-end controller You can get the appropriate service according to your request. Mediator pattern can also give a simple example to illustrate, for example, a computer, CPU, memory, hard drives, video cards, sound cards need to cooperate with each other to various devices work well, but if these things are directly connected together, computer the cabling is very complex, in this case, as the main board of the identity of a mediator, the respective device will be connected together directly without the need to exchange data between each device, thus reducing the coupling of the system and the complexity.

Original: zero.osai.club/java/2019/0...

Guess you like

Origin juejin.im/post/5d4e37d2f265da03bc1267fa