Object-oriented design principle

zhaozj2021-02-16  82

By Dever

A good object-oriented design requires some basic principles, such as a single responsibility (SRP), Open-Closed Principles (OCP), Liskov Replacement Principle (LSP), Dependency Imperial Principle (DIP), Interface Separation Principle (ISP), etc.

1. Single Responsibility Principles (SRP) Description: Only one of the cases should only cause change. Application: When constructing the object, separate the different responsibilities of the object into two or more classes, ensuring that only one reason for causing this class. Benefits: Improve consolidation and reduce coupling. Personal perspective: This principle can effectively reduce coupling, reducing references to unnecessary resources. However, consequences are increased by the source file, which is inconvenient to management, so in practical applications, the modules that often use or often need to be changed.

2, Open-Closed Principles (OCP) Description: "Open for Extension". This means that the behavior of the module can be expanded. When the demand for the application changes, the module can be expanded to make it a new behavior that meets the change. That is, we can change the functionality of the module. "CLOSE for moderation". When the module behavior is expanded, the source code or binary code of the module is not necessary. Application: Interfaces in advanced languages ​​and virtual classes. Benefits: Improve flexibility, reusability, maintainability. Personal view: The key to OCP is abstraction, the abstract purpose is to create a base class that can describe a group of possible behavior. The possible behavior of this group is expressed as derived class. For changes in the base class is closed, the method inside it cannot be changed once it is determined (changing the method in the interface will bring catastrophic consequences). The module is referenced by abstract base classes, and the extension of the party does not affect the entire module, so it is open. Following the cost of OCP is also expensive, creating the right abstraction is cost development time and energy, and abstraction also adds the complexity of software design. Therefore, effective predictive changes are the main points of OCP design, which requires us to conduct appropriate investigations, propose correct issues, and use our experience and general knowledge to make judgments. The correct approach is that it only makes abstracted parts in the program, refuses immature abstraction and abstract itself.

3, LISKOV Replacement Principle (LSP) Description: If the object O1 of each type S, there is a type T object O2, so that in all the program p written for T, after the O1 is replaced with O2, the program P behavior function The same, S is the subtype of t. Application: When implementing inheritance, subtype must be able to replace their base type (Base Type). If a software entity is used by the base class, it must also be applied to the subclass. But the replacement of the contrary is not true. Personal perspective: LSP is one of the main principles that make OCPs, and the violation of LSP will lead to violation of OCP, both of which are abstract and polymorphic foundations in OD, and behave in OOPL as inheritance. In the Advanced Language (Java, C #), as long as we strictly follow the grammar specifications of the interface and virtual class, we can also follow this principle, and we should also avoid some more subtle violations. For example, the square and rectangle, the rectangle can be used as a square base class because the square is also a rectangle, but for the square, setWidth () and setHeight () are redundant, and it is easy to cause errors, such a design It violates the LSP principles. If there are two specific classes A and B violate the LSP, one can be selected in the following two reconstruction schemes: 1. Create a new abstract class C, as the superclars of the two specific classes, will A and B common behavior moved to C to solve problems that are incompletely consistent with A and B behaviors. 2. Rewritten from the inheritance relationship between B to A as delegate relationships. 4. Relying on the Invert Principles (DIP) Description: a. High-level module should not depend on the low-level module. Both should depend on abstraction. B. Abstraction should not depend on the details. Details should depend on abstraction. Application: To rely on abstraction, do not depend on the specific. That is, programming is programmed for the interface. The interface programming is that the type declaration of the variable should be used to use the interface and abstract class, the type declaration of the parameters, the return type declaration of the method, and the conversion of the data type. Don't implement programming, it is to say that the type of variable should not be used, and the type of parametry declaration, the method of return type declaration, and the conversion of data types. Conclusion: Although DIP is powerful, it is not easy to implement, because of relying on reverse, the creation of the object is likely to use the object factory to avoid direct reference to the specific class, this principle will result in a large number of class files. Bring unnecessary trouble to maintenance. Therefore, the correct approach is to rely on inverted only the frequent variable part of the program.

5. Interface Isolation Principles (ISP) Description: Do not force customers to rely on their unused methods. Application: A class of dependencies on another class should be based on the smallest interface. If the client only needs some way, then the method of these needs should be provided to the client, instead of providing unwanted methods. Providing an interface means that it is committed to the client, too much commitment will cause unnecessary burden to the system's maintenance. Conclusion: Use multiple special interfaces to be better than using a single interface.

PS: I think this book is "Agile Software Development Principles, Mode and Practice", because it is written by foreigners, read more effort, if you have a sense of learning OOD and mode, I recommend a serious written doctor " Java and mode ", although I didn't read, the word of mouth was good.

转载请注明原文地址:https://www.9cbs.com/read-14389.html

New Post(0)