SOLID three design principles: the principle of replacement in style

In the replacement principle (LSP: Liskov Substitution Principle)

Functions that use pointers of references to base classes must be able to use objects of derived classes without knowing it. (Subclass object (object of subtype / derived class) can be replaced by a program (Program) in the parent object (anywhere object of base / parent class) occurs and ensure that the logical behavior of the original program (behavior) and the correctness of the same is not destroyed.)

From the description and definition code implementation point of view, and polymorphism in the replacement is somewhat similar, but the angle of their concerns are not the same.
Polymorphism is a major feature of object-oriented programming, is also an object-oriented programming language syntax. It is the idea of A transcoding to achieve.
And where the replacement is a design principle, it is used to guide the inheritance neutron class how to design sub-class design to ensure that the replacement parent class, does not change the logic of the original program, and does not destroy the original program correctness.

In fact, there is another principle in the replacement by a more capable floor, more descriptive guidance, that is "Design By Contract", Chinese translation is "in accordance with the protocol design."
Subclass in the design, conduct parent to comply with the agreement (or called protocol). The parent class defines a function of behavior agreed that subclasses can change the internal implementation of the function logic, but can not change the behavior of the original function of the agreement.
Here's behavior conventions include: function function declaration to be achieved; input, output, abnormal convention; even include any special instructions in the comment are listed.
In fact, the definition of the relationship between parent and child classes, may be replaced with the relationship between the interface and implementation class.

Summarized
in the replacement principle is used to guide, a principle of how the inheritance of the neutron class design. Understand where the replacement principle, the core is to understand the "design by contract, according to the agreement to design," the words. The parent class defines the "agreement" function (or called protocols) that subclasses can change the internal implementation of the function logic, but can not change the original function of "agreement." Convention here includes: function declaration of the function to be achieved; input, output, abnormal convention; even include any special instructions as listed annotations.

Guess you like

Origin www.cnblogs.com/Utah-Lee/p/12391901.html