码迷,mamicode.com
首页 > 其他好文 > 详细

接口隔离原则

时间:2021-04-19 14:48:17      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:ati   https   pack   介绍   隔离   ide   依赖   string   关系   

官方定义

接口隔离原则(Interface Segregation Principle),又称ISP原则
1、 客户端不应该依赖它不需要的接口
2、 类间的依赖关系应该建立在最小的接口上

基本介绍

通俗的来讲,不要在一个接口中定义多个方法,接口应该尽量细化

package jiekogeli;

public class SegregationDemo {
    public static void main(String[] args) {
        new FruitShop().cutApple(new CookZhang());
        new FruitShop().cutTomato(new CookZhang());
        new VegetableShop().cutTomato(new CookLi());
        new VegetableShop().cutPotato(new CookLi());
    }
}

interface Knife {
    void cutApple();

    void cutTomato();

    void cutPotato();
}

class CookZhang implements Knife {
    @Override
    public void cutApple() {
        System.out.println("张师傅在切苹果");
    }

    @Override
    public void cutTomato() {
        System.out.println("张师傅在切土豆");
    }

    @Override
    public void cutPotato() {
        System.out.println("张师傅在切番茄");
    }
}


class CookLi implements Knife {
    @Override
    public void cutApple() {
        System.out.println("李师傅在切苹果");
    }

    @Override
    public void cutTomato() {
        System.out.println("李师傅在切土豆");
    }

    @Override
    public void cutPotato() {
        System.out.println("李师傅在切番茄");
    }
}

class FruitShop {
    public void cutApple(Knife knife) {
        knife.cutApple();
    }

    public void cutTomato(Knife knife) {
        knife.cutTomato();
    }
}

class VegetableShop {
    public void cutPotato(Knife knife) {
        knife.cutPotato();
    }

    public void cutTomato(Knife knife) {
        knife.cutTomato();
    }
}

技术图片

接口隔离原则

标签:ati   https   pack   介绍   隔离   ide   依赖   string   关系   

原文地址:https://www.cnblogs.com/stu-jyj3621/p/14667837.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!