接口隔离原则

  • 在介绍接口隔离原则之前我们先看下面举例说明中的第一个例子——反例

一、举例说明

1.反例

(1)类图说明

  • 因为类图比较清晰,我们先看类图

在这里插入图片描述

  • 可以看出, DogPlays.java 和 CatPlays.java 分别实现了接口PetInterface ,DogPlaysUse 和 CatPlaysUse 又分别通过该接口依赖 DogPlays 和 CatPlays ,但是都没有用完实现类里的所有方法,造成资源浪费

(2)代码说明

  • 如果类图不是很明白的,直接看代码感受一下!
  • 直接上代码:
package com.liu.susu.principle.segregation.example1;

import org.springframework.stereotype.Service;

/**
 * @Description 接口隔离原则——反例
 * @Author susu
 * @date 2022-02-12
 **/
public interface PetInterface {
    void playBall();//玩球
    void climbTree();//爬树
    void catchMouse();//抓老鼠
    void likeToGoOut();//遛狗
}

@Service("dog")
class DogPlays implements PetInterface{
    @Override
    public void playBall() {
        System.out.println("狗狗喜欢玩球……");
    }
    public void climbTree() {
        System.out.println("DogPlays类 实现了 climbTree 方法");
    }
    public void catchMouse() {
        System.out.println("DogPlays类 实现了 catchMouse 方法");
    }
    public void likeToGoOut() {
        System.out.println("狗狗喜欢每天被遛遛……");
    }
}

@Service
class CatPlays implements PetInterface{
    public void playBall() {
        System.out.println("小猫喜欢玩球……");
    }
    public void climbTree() {
        System.out.println("小猫喜欢爬树……");
    }
    public void catchMouse() {
        System.out.println("小猫喜欢抓老鼠……");
    }
    public void likeToGoOut() {
        System.out.println("CatPlays 类 实现了 likeToGoOut 方法");
    }
}

/**
 * DogPlaysUse 类通过接口PetInterface 依赖(使用)DogPlays类,
 * 但是只用到 playBall 和 likeToGoOut 方法
 */
@Service
class DogPlaysUse {
    public void playBall(PetInterface petInterface){
        petInterface.playBall();
    }
    public void likeToGoOut(PetInterface petInterface) {
        petInterface.likeToGoOut();
    }
}
/**
 * CatPlaysUse 类通过接口PetInterface 依赖(使用)DogPlays类,
 * 但是只用到 playBall、climbTree 和 catchMouse 方法
 */
@Service
class CatPlaysUse {
    public void playBall(PetInterface petInterface){
        petInterface.playBall();
    }
    public void climbTree(PetInterface petInterface) {
        petInterface.climbTree();
    }
    public void catchMouse(PetInterface petInterface) {
        petInterface.catchMouse();
    }
}


class Test {
    public static void main(String[] args) {
        //DogPlaysUse 这个类通过接口 依赖 DogPlays 类
        DogPlaysUse dogPlaysUse = new DogPlaysUse();
        dogPlaysUse.playBall(new DogPlays());
        dogPlaysUse.likeToGoOut(new DogPlays());

        System.out.println("\n======小猫开始表演……=====\n" );
        //DogPlaysUse 这个类通过接口 依赖 CatPlays 类
        CatPlaysUse catPlaysUse = new CatPlaysUse();
        catPlaysUse.playBall(new CatPlays());

    }
}

测试类

package com.liu.susu.principle.segregation.example1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @FileName PetController
 * @Description
 * @Author susu
 * @date 2022-02-12
 **/
@Controller
@RequestMapping("/dog")
public class PetController {

    @Autowired
    @Qualifier("dog")
    private PetInterface petInterface;

    @Autowired
    @Qualifier("catPlays")
    private PetInterface petInterface2;

    @Autowired
    private DogPlaysUse dogPlaysUse;
    @Autowired
    private CatPlaysUse catPlaysUse;

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(){
        dogPlay();
        catPlay();
        return "hello world!";
    }
    public void dogPlay(){
        dogPlaysUse.playBall(petInterface);
        System.out.println("dogPlaysUse.playBall(petInterface)--->ok");
    }
    public void catPlay(){
        catPlaysUse.playBall(petInterface2);
        System.out.println("catPlaysUse.playBall(petInterface2)--->ok");
    }

}

(4)分析缺点(总结)
可以看出,PetInterface 的实现有两个,分别是 DogPlays.java 和 CatPlays.java
DogPlaysUse 这个类通过接口 PetInterface 依赖 DogPlays 类,但是只用到 playBall 和 likeToGoOut 方法。
而 DogPlays 类实现了 PetInterface 接口的所有方法,所以造成浪费
同样的,CatPlaysUse 这个类通过接口 PetInterface 依赖 CatPlays 类,但是只用到 playBall、climbTree 和 catchMouse 方法,所以也造成浪费
2.正例
那么对于上面的案例怎么来优化呢?
既然一个接口有浪费,那么我们就把它拆解,这其实就是我们的接口隔离原则,请继续……
(1)类图说明

在这里插入图片描述

  • 如图可见,对于上述案例,如果用接口隔离原则优化的话,我们设计的时候拆成3个接口最为合理

(2)代码说明

package com.liu.susu.principle.segregation.example2;

/**
 * @Description 接口隔离原则——正例
 * @Author susu
 * @date 2022-02-12
 **/
public interface PetInterface {
    void playBall();//玩球
}
interface DogInterface {
    void likeToGoOut();//遛狗
}
interface CatInterface {
    void climbTree();//爬树
    void catchMouse();//抓老鼠
}

class DogPlays implements PetInterface,DogInterface{
    public void playBall() {
        System.out.println("狗狗喜欢玩球……");
    }
    public void likeToGoOut() {
        System.out.println("狗狗喜欢每天被遛遛……");
    }
}

class CatPlays implements PetInterface,CatInterface{
    public void playBall() {
        System.out.println("小猫喜欢玩球……");
    }
    public void climbTree() {
        System.out.println("小猫喜欢爬树……");
    }
    public void catchMouse() {
        System.out.println("小猫喜欢抓老鼠……");
    }
}

class DogPlaysUse {
    public void playBall(PetInterface petInterface){
        petInterface.playBall();
    }
    public void likeToGoOut(DogInterface dogInterface) {
        dogInterface.likeToGoOut();
    }
}

class CatPlaysUse {
    public void playBall(PetInterface petInterface){
        petInterface.playBall();
    }
    public void climbTree(CatInterface catInterface) {
        catInterface.climbTree();
    }
    public void catchMouse(CatInterface catInterface) {
        catInterface.catchMouse();
    }
}

/**
 * 测试类
 */
class Test{
    public static void main(String[] args) {
        DogPlaysUse dogPlaysUse = new DogPlaysUse();
        dogPlaysUse.playBall(new DogPlays());
        dogPlaysUse.likeToGoOut(new DogPlays());

        System.out.println("\n======小猫开始表演……=====\n" );

        CatPlaysUse catPlaysUse = new CatPlaysUse();
        catPlaysUse.playBall(new DogPlays());
        catPlaysUse.climbTree(new CatPlays());
        catPlaysUse.catchMouse(new CatPlays());

    }
}

(4)方案评价
对比第一种写法,使用接口隔离的这种写法明显让旁人看着代码也是很清晰,接口拆开之后各行其职责,代码看着不臃肿,后续功能变更或新增的话维护起来也好维护
三、总结
客户端不应该依赖它不需要的接口
一个类对另外一个类的依赖,应建立在最小的接口上,其实就是一个接口里不要放一个功能模块外的其他方法,理解起来跟单一职责原则有点像,自己体会体会吧

posted @   蜡笔小新Belief  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示