Java design Pattern (11) Richter replacement principle (LSP)

Source: Internet
Author: User

The basic concept of the Richter replacement principle (Liskov Substitution Principle) LSP
  • definition :
    • All references to base classes must be able to use objects of their subclasses transparently
    • As long as the parent class can appear where the subclass can appear, and the substitution of subclasses will not produce any errors or exceptions , the consumer may not need to know whether it is a parent class or a subclass ( the polymorphism caused by encapsulation )
  • Specification
    • Subclasses must fully implement the methods of the parent class
      It is necessary to use the parent class or interface when calling other classes in the class, and if the methods in the parent class are not supported in the subclass, it will naturally violate the LSP
    • Subclasses should have their own characteristics .
      • Subclasses are implemented on the basis of the parent class and have their own attributes
      • This also leads to the one- way LSP, where the subclass can appear, the parent may not be competent.
      • For example, in the actual development of the downward transformation is not correct, will throw classcastexception exception .
    • Parameters can be magnified when you overwrite or implement a method of a parent class
    • The preconditions for the parameters of a method in a subclass must be the same as or looser than the precondition of the method being overridden in the superclass class
    • Because different parameter types cause a different argument list, in fact we do not implement an overload , not overwrite , in the argument type
    • So in the case of a subclass where the method condition is more lenient, because of the overloaded invocation rule, the more well-matched overloads are called first , which means that our overload does not accidentally overwrite the method in the parent class ( For example, HashMap is preferred to match the map type. Can cause overloads to reach functions that override the parent class, but obviously this is not logical , ensuring the correctness of the LSP.
    • Output can be scaled down when you overwrite or implement a method of a parent class
      • The specific meaning of this narrowing is as follows: The return value of the method in the parent class is T, and the return value of the method covered in the subclass is S, then s either is the same type as T, or S is a subclass of T
      • Because under the LSP, it is possible to meet the narrowing conditions to ensure that the returned object is safe and fully implemented when it is used.
  • Advantages : The use of LSP can enhance the robustness of the program, version upgrade to enable a very good compatibility , even if you add subclasses, the original subclass can continue to run.
Because for the Java language, the LSP has already implemented the compile-time and language-level guarantees that when a violation throws an exception, we only need to follow the LSP when overriding and reloading to ensure that this principle is guaranteed.

Java design Pattern (11) Richter replacement principle (LSP)

Related Article

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.