"Open-Closed" principle (Open-Closed Principle, OCP)

xiaoxiao2021-03-06  45

"Open-Closed" principle (Open-Closed Principle, OCP)

A software entity should open the extension and close to modify.

Software Entities SHOULD BE Open for Extension, Butclosed for Modification.

When designing a module, this module should be expanded without modification.

"Variable Package Principle" explains how to achieve "open-closed" principle from the perspective of engineering.

"Variable package principle" means two points:

1. One variability should not be scattered in many corners of the code, but should be encapsulated into an object. Inheritance should be seen as a method of encapsulation, and should not be considered to generate a special object method from a general object.

2. One variability should not be mixed with another variability. The inheritance structure of all class diagrams generally does not exceed two layers, otherwise it means mixing two different variability together.

Relationship between "open-closed" principle and other principles:

The principle of replacement is that any places where the base category can occur, the subclass can appear.

The Rivien's replacement principle is to supplement the "open-closed" principle. The key steps to achieve the "open-closed" principle are abstraction, and the inheritance relationship between base classes and subclasses is the specific embodiment of abstraction, so the replacement principle is specification for the specific steps of achieving abstraction. In violation of the principle of replacement of the Rivie, it also violated the "open-closed" principle, and it is not necessarily established.

Relying on the reverse principle is that it is necessary to rely on abstraction, do not rely on implementation.

The principle of "opening-closed" is the goal, and the principle of relying on the reversal is means.

The principle of synthesis / polymerization is to use synthetic / aggregation as much as possible, rather than inheritance relationships to reach multiplexed purposes.

The synthesis / polymerization principle complements the principle of the Rivie, both of which are specific steps of implementing the principle of "open-closed" principles.

Dimit Block is that a software entity should interact with other entities as possible.

A system that complies with Dimit Principles is more easier when the function needs to be extended, and it is relatively easier to do it.

The principle of interface isolation is to provide the client to provide a separate interface as much as possible instead of providing a large total interface.

Interface Isolation Principles and Generalized Dimit Facts are limited to communication between a software entity and other software entities. Follow the principle of interface isolation, a software system will not pass the modified pressure to other objects in the process of the function extension.

Discussion on a reconstruction method

"Repairing the conditional transfer statement is a polymorphism" is a widely flowed code reconstruction practice.

This practice does not guarantee the "open-closed" principle, and should be judged if the principle of "open-closed" is needed to be rewritten. Conditional transfer is not an error, if necessary, you can choose to use the condition transfer.

If a conditional transfer statement does encapsulate a variability of a certain business logic, this variability package is in line with the "open-closed" principle design thoughts. If a conditional transfer statement does not involve important business logic, or does not change over time, it does not mean any scalability, then it does not involve any meaningful variability. At this time, this condition is changed in a polymorphism that is a meaningful waste.

Abstract classes should have as many common code as possible

In one inherited level structure, the common code should be moved above the level structure. Move the duplicate code from the subclass into the superclass, you can increase the multiplex rate of the code. When the code changes, the designer needs to modify a place.

Abstract classes should have as little data as possible

In contrast to the mobile direction of the code, the direction of movement of the data is from the abstraction class to the specific class, move below the level structure. An object's data will take up resources regardless of whether it is used, so it should be placed on the low end of the hierarchy.

When is it necessary to use inheritance multiplexing?

1. Subclasses are a special type of superclass, rather than a role of superclars, and IS-A is in line with inheritance relationship.

2. Never there is a case where you need to change the subcategory to another class.

3. Subclasses have the responsibility of extended superclars, rather than having override and Nullify. 4. Inheritance can only be used when there is meaningfulness in the classification point of view, do not inherit from the tool class.

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

New Post(0)