Object-oriented design principle (transfer)

xiaoxiao2021-04-03  240

Object-oriented cornerstone is "open-closed" principle. The principle of "opening a closed" is: A software entity should be open to the extension and turn it off for modification. This rule says that when designing a module, this module should be expanded without modification. From another perspective, it is the so-called "principle of variable package". "Variable Package Principle" means two points: 1. A variability should not be scattered in many corners of the code, but should be packaged into an object. Different representations of the same variability means the specific subclass of the same inheritance level structure. 2. A variability should not be mixed with another variability. That is, the inheritance structure of the class map generally should not exceed two layers. It is not an easy thing to do "open-closed" principles, but there are many rules that can be followed, and these rules are also design principles, which are tools for achieving the principle of open - closed principles. Ri Shi MRS SCD Principle: If the object O1 for each type T1 is O2, there is an object O2 type T2, so that all programs defined by T1 are replaced with O2 in all objects O1, the program P 's behavior has not changed, then the type T2 is the subtype of T1. That is, 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. If there are two specific classes A and B violate the Rich MRT, one can be selected in the following two reconstruction schemes: 1. Create a new abstract class C, as two specific classes The super class, moves A and B to c into C to solve problems that are incompletely consistent with A and B behaviors. 2. Record from the inheritance relationship between B to A as delegate relationships. Dependency reversal principles depends on reverse princumber: To rely on abstraction, don't rely on 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. Although it is strong, it is not easy to implement, because depending on the reversal, the creation of the object is likely to use the object factory to avoid direct reference to the specific class, this principle uses the use of a large number of classes. Maintaining such systems requires better object-oriented design knowledge. In addition, relying on reverse princumber assumes that all specific classes are changing, which is not always correct. There are some specific classes that may be quite stable. If they do not change, the client consumed this specific class instance can be fully dependent on this specific class. The principle of interface isolation principle interface isolation is that the use of multiple special interfaces is better than using a single interface. From the customer's perspective: 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. Synthesis, aggregation reuse principles, the principle of polymerization is to use some existing objects in a new object, making it a part of the new object, and new objects have reached multiplexed to these objects. The purpose of functionality. This principle has a short description: try to use synthesis, aggregate, try not to use inheritance. Synthesis, polymerization has the advantage: The only way to access component objects in new objects is the interface of the component object. This multiplex is a black box multiplex because the internal details of the ingredient object are not seen. Such multiplexing can be dynamically performed during runtime, and new objects can dynamically reference the same object as the ingredient object type. Synthesis, aggregation can be applied to any environment, while inheritance can only be applied to some limited environments. A common cause of the use of the incorrect and inheritance is the error of the "HAS-A" relationship as the "IS-A" relationship. If the two classes are "HAS-A" relationships, they should be used, aggregated, if it is "IS-A" relationship, then inheritable.

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

New Post(0)