Six principles of design pattern Two: the principle of the Richter scale substitution

Source: Internet
Author: User

Definition 1: if each object of type T1 is O1, there is an object O2 of type T2, so that all program P defined in T1 is replaced with O1 for all object O2, and the behavior of program p does not change, then type T2 is a subtype of type T1.

definition 2: All references to base classes must be able to transparently use objects of their subclasses.

problem Origin: There is a functional P1, which is done by Class A. Now need to extend the function P1, the function of the extension is P, where p is composed of the original function P1 and the new function P2. The new function p is done by subclass B of Class A, and sub-class B, while completing the new function P2, may cause the original function P1 to fail.

Solution: when using inheritance, follow the Richter substitution principle. When Class B inherits from Class A, try not to rewrite the parent class A's method, but also try not to reload the parent Class A's method, except to add a new method to complete new functionality P2.

Inheritance contains a layer of meaning: in a parent class, a well-implemented method (as opposed to an abstract method) is actually a set of specifications and contracts, although it does not enforce that all subclasses must comply with these contracts, but if the subclasses arbitrarily modify these non-abstract methods, the entire inheritance system will be destroyed. The principle of replacing the Richter scale is to express this level of meaning.

as one of the three characteristics of object-oriented, inheritance brings great convenience to program design, and it also has drawbacks. For example, the use of inheritance will be intrusive to the program, the portability of the program, increase the coupling between objects, if a class is inherited by other classes, when the class needs to be modified, must take into account all subclasses, and after the parent class modification, all the functions that involve subclasses are likely to fail.

To illustrate the risk of inheritance, we need to complete a two-digit subtraction function, which is the responsibility of Class A.

12345678910111213 class A{    public int func1(int a, int b){        return a-b;    }} public class Client{    public static void main(String[] args){        A a = new A();        System.out.println("100-50="+a.func1(100, 50));        System.out.println("100-80="+a.func1(100, 80));    }}

Operation Result:

100-50=50
100-80=20

Later, we need to add a new function: Complete the two-number addition, and then sum it up with 100, which is the responsibility of Class B. That is, Class B needs to complete two functions:

    • Subtract two numbers.
    • The two numbers are added and then added 100.

Since Class A has already implemented the first function, Class B inherits Class A and only needs to complete the second function, the code is as follows:

123456789101112131415161718 class B extends A{    public int func1(int a, int b){        return a+b;    }        public int func2(int a, int b){        return func1(a,b)+100;    }}public class Client{    public static void main(String[] args){        B b = new B();        System.out.println("100-50="+b.func1(100, 50));        System.out.println("100-80="+b.func1(100, 80));        System.out.println("100+20+100="+b.func2(100, 20));    }}

After Class B is complete, run the result:

100-50=150
100-80=180
100+20+100=220

we found that the normal subtraction function had been wrong. The reason is that Class B inadvertently overrides the method of the parent class when giving the method a name, causing all code that runs the subtraction function to call the overridden method of Class B, resulting in an error in the function that was normally functioning. in this example, an exception occurs after a function that references the completion of base class A is replaced with subclass B. In the actual programming, we often by rewriting the parent class method to complete the new function, so that although simple to write, but the entire inheritance system reusability will be poor, especially when the use of polymorphic more frequent, the probability of the program run error is very large. If you do not want to override the method of the parent class, the common practice is that the original parent class and subclass inherit a more popular base class, the original inheritance relationship is removed, the dependency, aggregation, composition and other relationships are substituted.

the principle of the Richter substitution is, in layman's terms: Subclasses can extend the functionality of the parent class, but cannot change the original functionality of the parent class . it contains the following 4 levels of meaning:

    • Subclasses can implement the abstract methods of the parent class, but cannot override the non-abstract methods of the parent class.
    • Subclasses can add their own unique methods.
    • When a method of a subclass overloads a method of the parent class, the method's preconditions (that is, the parameter of the method) are more lenient than the input parameters of the parent class method.
    • When a method of a subclass implements an abstract method of the parent class, the post condition of the method (that is, the return value of the method) is stricter than the parent class.

It looks incredible, because we will find that in our own programming often violates the Richter scale replacement principle, the program still runs well. So everyone will have this question, if I do not follow the Richter scale replacement principle will have what consequences?

The consequence is that the odds of your code writing will increase dramatically.

Six principles of design pattern Two: the principle of the Richter scale substitution

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.