当前位置: 首页 > 图灵资讯 > 技术篇> 接口隔离原则

接口隔离原则

来源:图灵教育
时间:2024-08-27 13:27:25

接口隔离原则

任何客户端都不应该被迫依赖它不使用的方法

考虑到办公空间的示例,用户表示各种输出设备

接口隔离原则前:

i多功能接口

/**
 * @imultifunction interface has methods related to all output devices present in office space
 * for devices like printer, scanner, fax machines, etc
*/
public interface imultifunction {
    public void print();
    public void getprintspooldetails();
    public void scan();
    public void scanphoto();
    public void fax();
    public void internetfax();
}

上述通用接口现在已经实现了各种设备

具有所有功能的 xeroxworkcenter 类

/**
 * 
 * you must have seen xerox work station device which has all the features in one like printing, scanning, xerox,
 * fax etc
*/
public class xeroxworkcenter implements imultifunction {

    @override
    public void print() {
        // real printing code
    }

    @override
    public void getprintspooldetails() {
        // real get print spool details code
    }

    @override
    public void scan() {
        // read scanning code
    }

    @override
    public void scanphoto() {
        // real scan photo code 
    }

    @override
    public void fax() {
        // real fax code
    }

    @override
    public void internetfax() {
        // real internet fax code
    }

}

hpprinternscanner 该类别具有打印和扫描功能

public class hpprinternscanner implements imultifunction {

    @override
    public void print() {
        // real printing code
    }

    @override
    public void getprintspooldetails() {
        // real get print spool details code
    }

    @override
    public void scan() {
        // read scanning code
    }

    @override
    public void scanphoto() {
        // real scan photo code 
    }

    //since hpprinternscanner has only printing and scanning abilities fax() and internetfax() will have empty body
    @override
    public void fax() {}

    @override
    public void internetfax() {}

}

canonprinter 类只有打印功能

public class canonprinter implements imultifunction {

    @override
    public void print() {
        // real printing code
    }

    @override
    public void getprintspooldetails() {
        // real get print spool details code
    }

    //since the canonprinter has only printing ability rest of the method will have an empty body
    @override
    public void scan() {}

    @override
    public void scanphoto() {}

    @override
    public void fax() {}

    @override
    public void internetfax() {}

}

非法识别isp技能

点击下载“修复打印机驱动工具”;

  • 胖接口(有两个多方法声明的接口)
  • 低内聚接口(不太可能相互关联的接口)
  • *空方法实现*(当他们被迫实现自己不使用的方法时,他们会留空方法的实现)

接口隔离原则后:

public interface iprint {
    public void print();
    public void getprintspooldetails();
}

public interface iscan {
    public void scan();
    public void scanphoto();
}

public interface ifax {
    public void fax();
    public void internetfax();
}

/**
 * 
 * you must have seen the xerox workstation device which has all the features in one like printing, scanning, xerox, fax, etc.
*/
public class xeroxworkcenter implements iprint,iscan,ifax {

    @override
    public void print() {
        // real printing code
    }

    @override
    public void getprintspooldetails() {
        // real get print spool details code
    }

    @override
    public void scan() {
        // read scanning code
    }

    @override
    public void scanphoto() {
        // real scan photo code ̰
    }

    @override
    public void fax() {
        // real fax code
    }

    @override
    public void internetfax() {
        // real internet fax code
    }

}

public class hpprinternscanner implements iprint,iscan {

    @override
    public void print() {
        // real printing code
    }

    @override
    public void getprintspooldetails() {
        // real get print spool details code
    }

    @override
    public void scan() {
        // read scanning code
    }

    @override
    public void scanphoto() {
        // real scan photo code 
    }
}

public class CanonPrinter implements IPrint {

    @Override
    public void print() {
        // real printing code
    }

    @Override
    public void getPrintSpoolDetails() {
        // real get print spool details code
    } 
}

每个接口都有一个单一的职责,现在更干净了。

isp 与其他 solid 原则的关系

单一责任 将接口划分为不同的接口后,现在所有的接口(例如 iprint、iscan)都有单一的职责

里氏替换 由于隔离,所有类别(实现接口)都遵循里氏替换,因为所有子类型或实现类都可以用它们的接口引用变量替换

以上是接口隔离原则的详细内容。请关注图灵教育的其他相关文章!

上一篇:

里氏替换原则

下一篇:

返回列表