Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 界面分离原理的对立面_Java_Design Patterns_Solid Principles_Interface Segregation Principle - Fatal编程技术网

Java 界面分离原理的对立面

Java 界面分离原理的对立面,java,design-patterns,solid-principles,interface-segregation-principle,Java,Design Patterns,Solid Principles,Interface Segregation Principle,我今天在一次采访中被问到什么是界面隔离原则,什么是与此相反的情况或原则 ISP对我来说很清楚,但我不知道问题的第二部分,ISP的相反原则是什么 接口隔离原则(ISP)规定,不应强制任何客户端依赖于它不使用的方法 与此相反的是,客户端被迫依赖于它不使用的方法。这可能表现在实现一个它不需要的接口、在一个层中有太多方法的接口,或者一个定义了多个抽象方法的类,这些抽象方法对于客户端来说是不必要的 示例(首先是接口): 抽象方法示例: public abstract class Shape {

我今天在一次采访中被问到什么是界面隔离原则,什么是与此相反的情况或原则

ISP对我来说很清楚,但我不知道问题的第二部分,ISP的相反原则是什么

接口隔离原则(ISP)规定,不应强制任何客户端依赖于它不使用的方法

与此相反的是,客户端被迫依赖于它不使用的方法。这可能表现在实现一个它不需要的接口、在一个层中有太多方法的接口,或者一个定义了多个抽象方法的类,这些抽象方法对于客户端来说是不必要的

示例(首先是接口):

抽象方法示例:

public abstract class Shape {

    public abstract double getVolume();
    public abstract double getHeight();
    public abstract double getLength();
    public abstract double getWidth();
    public abstract Color getColor();
}

public class Line extends Shape {

    public double length;
    public Color color;

    // Kind of forced to have a volume...
    public double getVolume() {
        return 0;
    }

    /// ...and a height...
    public double getHeight() {
        return 0;
    }

    // ...and a width...
    public double getWidth() {
        return 0;
    }

    public double getLength() {
        return length;
    }

    public Color getColor() {
        return color;
    }
}

我想@Makoto,一条线可以是一个形状吗?…也可能违反LSP。
public abstract class Shape {

    public abstract double getVolume();
    public abstract double getHeight();
    public abstract double getLength();
    public abstract double getWidth();
    public abstract Color getColor();
}

public class Line extends Shape {

    public double length;
    public Color color;

    // Kind of forced to have a volume...
    public double getVolume() {
        return 0;
    }

    /// ...and a height...
    public double getHeight() {
        return 0;
    }

    // ...and a width...
    public double getWidth() {
        return 0;
    }

    public double getLength() {
        return length;
    }

    public Color getColor() {
        return color;
    }
}