java design patterns study notes - Richter substitution principle

Oo thinking and explanation of inheritance

1, inheritance includes this layer of meaning: the parent class who have achieved good way to actually set specifications and contract, although it is not mandatory for all subclasses must follow those in July, but if these subclasses the method has been implemented any changes, it will cause damage to the entire inheritance hierarchy.
2, in succession to the program designed to bring convenience, but also brings disadvantages. For example, the use of inheritance will bring invasive procedures, reduce the portability of the program, to increase the coupling between objects, if a class is inherited by other classes, then when this class needs to be modified, it must take into account all of the subclasses and the parent class is modified, all functions related to the subclass are likely to malfunction.
3, the issues raised: in programming, how to properly use inheritance? A: Follow the Richter substitution principle as much as possible.

basic introduction

1, Richter substitution principle in 1988, presented by the Massachusetts Institute of Technology's surnamed lady.
2, if each type of object T1 o1, o2 are an object of type T2 and T1 is defined such that all programs on all objects p o1 o2 are replaced by substituting, the program does not change the behavior of p, then T2 type is a subtype of type T1. In other words, all, all kinds of applications where the object must be used transparently its subclasses.
3, when using inheritance, follow the Richter substitution principle, in a subclass try not to override the parent class method.
4, Richter substitution principle tells us that, in fact, make two classes inherit coupling enhanced, in appropriate cases, by Syntagma, rely to solve the problem .

Applications

Do not use the Richter substitution principle

public class Liskov {
    public static void main(String[] args) {
        A a = new A();
        System.out.println("1-8=" + a.func1(1, 8));

        System.out.println("--------------------");
        B b = new B();
        System.out.println("11-3=" + b.func1(11, 3));
        System.out.println("1-8=" + b.func1(1, 8));
        System.out.println("11+3+9=" + b.func3(11, 3));
    }
}

class A {
    // 两个数的差
    public int func1(int num1, int num2) {
        return num1 - num2;
    }

    // 两个数的积
    public int funcBase(int num1, int num2) {
        return num1 * num2;
    }
}

//B类继承了A
//增加了一个新功能,完成两个数相加,然后和9求和
class B extends A {
    // 这里,重写了A的方法,可能是无意识
    public int func1(int a, int b) {
        return a + b;
    }

    public int func3(int a, int b) {
        return func1(a, b) + 9;
    }
}

Use Richter substitution principle

public class Liskov {
    public static void main(String[] args) {
        A a = new A();
        System.out.println("1-8=" + a.func1(1, 8));

        System.out.println("--------------------");
        B b = new B();
        System.out.println("11-3=" + b.func1(11, 3));
        System.out.println("1-8=" + b.func1(1, 8));
        System.out.println("11+3+9=" + b.func2(11, 3));

        // 使用组合来使用A的相关方法
        System.out.println("--------------------");
        System.out.println("11-3=" + b.func3(11, 3));
        System.out.println("1-8=" + b.func3(1, 8));
        System.out.println("11+3+9=" + b.func2(11, 3));
    }
}

class Base {
    // 把更加基础的方法和成员写到Base类
    // 两个数的积
    public int funcBase(int num1, int num2) {
        return num1*num2;
    }
}

class A extends Base {
    // 两个数的差
    public int func1(int num1, int num2) {
        return num1 - num2;
    }
}

//B类继承了A
//增加了一个新功能,完成两个数相加,然后和9求和
class B extends Base {
    // 如果B需要使用A类得方法,使用组合关系
    private A a = new A();

    // 这里,重写了A的方法,可能是无意识
    public int func1(int a, int b) {
        return a + b;
    }

    public int func2(int a, int b) {
        return func1(a, b) + 9;
    }

    // 使用A得方法
    public int func3(int a, int b) {
        return this.a.func1(a, b);
    }
}

The code above, the use of alternative principles Richter, more based approach funcBase () extracted, and then the class A and class B inherits the base class Base more, dependent employed, polymerization or coupling way to reduce parent coupling the class and subclass

Guess you like

Origin www.cnblogs.com/windowsxpxp/p/12177695.html