Richter substitution principle (Liskov substitution principle) _ design mode

Source: Internet
Author: User
Role

It instructs us to inherit and derive correctly, and to reuse code reasonably! Defined

Subtypes must be able to replace their parent types and appear anywhere the parent class can appear.

This is to use the Polymorphic method of programming, that is, the GRASP mode of polymorphism.

If O1 for each object of type T1, there is a T2 object O2, so that all program P defined by T1 is substituted for O1 for all object O2, the behavior of the program P is not changed, then the type T2 is the subtype of type T1.

In other words, if a software entity uses a base class, it must apply to its subclasses, and it cannot detect the difference between a base class object and a subclass object at all.

In turn substitution is not tenable.

Main points of the Richter substitution principle:

1 The breach of LSP also potentially violates the OCP

2 The example of a square inheriting from a rectangle subtly violates this principle

3. When considering whether a particular design is appropriate, the solution cannot be viewed in isolation, and it must be viewed in the light of reasonable assumptions made by the user of the design.

4 The behavior of the object is the problem that the software really concerns, is a relation is the behavior

5 a derived class can only replace the original predecessor condition with an equal or weaker precondition, and only replace the original post condition with an equal or stronger precondition.

6 The completed function is less than the derived class of the base class usually cannot replace the base class and does not conform to the LSP

7) Derived classes should not throw exceptions that cannot be predicted by the base class, or violate the principle of LSP substitution

The Richter substitution principle can be expressed in layman's terms: If a base class object can be used in software, it must be able to use its subclass object. Replace the base class with its subclass, the program will not generate any errors and exceptions, the reverse is not true, if a software entity is using a subclass, then it is not necessarily able to use the base class. The principle of the Richter substitution is one of the important ways to realize the opening and closing principle. Because subclass objects can be used where the base class object is used, it is possible to use the base class type in the program to define the object, and then to determine its subclass type at run time, replacing the parent class object with the subclass object.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.