Object-oriented "six principles of a law."

  • Single Responsibility Principle: a class only do what it should do. (Single Responsibility Principle want to express is the "high cohesion", the ultimate principle to write code only six words "high cohesion, low coupling", so-called high cohesion is only one code module to complete a feature-oriented object, if only to allow a class to complete it should do, and not to the field and it is not related to practicing the principles of high cohesion, this class is only a single role. another is modular, the bike is good car assembly, 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 split and can be self-assembled, 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.)

  • 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 open and close two Important: ① abstraction is critical, if a system is not an abstract class or interface point of the system there is no extension; ② variability packaging, the packaging system to a variety of variables in the inheritance hierarchy, if a plurality of confounding variables together, the system becomes complex and chaotic change, if the variability is not clear how the package, can refer to "design patterns explained" book to explain the mode of the bridge section.)

  • Dependency Inversion Principle: oriented programming interface. (The principle is to put it bluntly, and some of the specific method of parameter type declaration, the return type, reference type, using as variables of abstract types rather than specific types because its abstract type can be any type of a child Instead, please refer to the following Richter substitution principle.)

  • Richter substitution principle: any time you can replace the parent type sub-type. Description (on the Richter substitution principle, Ms. Barbara Liskov description is much more complex than this, but simply means that a parent can place we will be able to use the type of sub-types. Richter substitution principle can check whether a reasonable inheritance, If an inheritance relationship inheritance violates the 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 inheritance so that rectangle square is wrong , because you're likely to find a violation of the principle of replacing the Richter scene should be noted: 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 multi the ability of an object as small objects with a course without any problem.)

  • Interface segregation principle: the interface to be small and specialized, must not be comprehensive. (Bloated interface is an interface contamination, since the interface represents capacity, then only one interface should describe a capability within the interface should also be highly cohesive. For example, it should be designed to poetry and painting four interfaces, respectively, without a four methods should be designed to interface, as if designed as a four methods interface, then this interface is difficult to use, after all, four kinds of people versed in poetry and painting are still a minority, but if designed four interfaces It will implement several interfaces to several possibilities so each interface are multiplexed interface represents the ability of a high .Java, on behalf of the agreement, on behalf of roles, whether the proper use of the level of the interface must be programmed the important identification.)

  • Synthetic polymeric multiplexing principle: precedence relationships synthetic polymeric or 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 relationship, is-a relationship, has-a relationship, Use-a relationship, representing inheritance relationships and dependencies among which relationships can be further divided according to the intensity of its associated of association, aggregation and synthesis, but it plainly are Has-a relationship, synthetic polymeric multiplexing principle want to express is a priority Has-a relationship instead of is-a relationship reusable code, reason Well you can find yourself a million reasons from Baidu, should be noted that, even in Java's API, there are many examples of abuse of inheritance, such as Properties class inherits the Hashtable class, Stack class extends the Vector class, which inherits obviously wrong, the better practice is to place a member of a Hashtable class type in the Properties and its key strings and values ​​are set to store data, and the Stack class design should also be put to a Vector object data stored in the Stack class. remember Live: any time not inherit tools, tools that can be owned and used, rather than bring inherited).

  • 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.

发布了31 篇原创文章 · 获赞 97 · 访问量 55万+

Guess you like

Origin blog.csdn.net/qq_43390235/article/details/102655379