内存条
package com.jsxs.principles.demo3.before; /** * @Author Jsxs * @Date 2023/4/16 8:57 * @PackageName:com.jsxs.principles.demo3.before * @ClassName: neicun * @Description: TODO * @Version 1.0 */ public class neicun { public void save(){ System.out.println("使用内存条的数据"); } }
计算机整体
package com.jsxs.principles.demo3.before; /** * @Author Jsxs * @Date 2023/4/16 8:58 * @PackageName:com.jsxs.principles.demo3.before * @ClassName: Computer * @Description: TODO 组合关系 * @Version 1.0 */ public class Computer { // 电脑统一 private CPU cpu; private neicun neicun; private yingpan yingpan; public CPU getCpu() { return cpu; } public void setCpu(CPU cpu) { this.cpu = cpu; } public com.jsxs.principles.demo3.before.neicun getNeicun() { return neicun; } public void setNeicun(com.jsxs.principles.demo3.before.neicun neicun) { this.neicun = neicun; } public com.jsxs.principles.demo3.before.yingpan getYingpan() { return yingpan; } public void setYingpan(com.jsxs.principles.demo3.before.yingpan yingpan) { this.yingpan = yingpan; } public void run(){ System.out.println("计算机开始开机操作"); String s = yingpan.get(); System.out.println("从硬盘上获取的数据是:"+s); cpu.run(); neicun.save(); } }
客户端实现类
package com.jsxs.principles.demo3.before; /** * @Author Jsxs * @Date 2023/4/16 9:01 * @PackageName:com.jsxs.principles.demo3.before * @ClassName: ComputerTest * @Description: TODO * @Version 1.0 */ public class ComputerTest { public static void main(String[] args) { // 1.创建计算机对象 Computer computer = new Computer(); yingpan yingpan = new yingpan(); CPU cpu = new CPU(); neicun neicun = new neicun(); computer.setCpu(cpu); computer.setYingpan(yingpan); computer.setNeicun(neicun); computer.run(); } }
上面的代码可以看到已经组装了一台电脑,但是视乎组装的电脑只能是Inter的,内存条值能使金士顿的、硬盘只能是希捷的。这对用户来说肯定不是友好的,因为用户很喜欢自定义机箱。如果我们想要更改CPU肯定要修改Computer所以违反了开闭原则。
正列
CPU接口
package com.jsxs.principles.demo3.after; /** * @Author Jsxs * @Date 2023/4/16 9:23 * @PackageName:com.jsxs.principles.demo3.after * @ClassName: CPUInterface * @Description: TODO CPU接口 * @Version 1.0 */ public interface CPUInterface { public void run(); }
CPU实现类
package com.jsxs.principles.demo3.after; /** * @Author Jsxs * @Date 2023/4/16 9:24 * @PackageName:com.jsxs.principles.demo3.after * @ClassName: CpuImp * @Description: TODO * @Version 1.0 */ public class CpuImp implements CPUInterface{ @Override public void run() { System.out.println("自定义的CPU"); } }
内存条接口
package com.jsxs.principles.demo3.after; /** * @Author Jsxs * @Date 2023/4/16 9:24 * @PackageName:com.jsxs.principles.demo3.after * @ClassName: NeicunInterface * @Description: TODO * @Version 1.0 */ public interface NeicunInterface { public void save(); }
内存条实现类
package com.jsxs.principles.demo3.after; /** * @Author Jsxs * @Date 2023/4/16 9:25 * @PackageName:com.jsxs.principles.demo3.after * @ClassName: NeicunImp * @Description: TODO * @Version 1.0 */ public class NeicunImp implements NeicunInterface{ @Override public void save() { System.out.println("自定义的内存条存取数据"); } }
硬盘接口
package com.jsxs.principles.demo3.after; /** * @Author Jsxs * @Date 2023/4/16 9:21 * @PackageName:com.jsxs.principles.demo3.after * @ClassName: HardCode * @Description: TODO 硬盘接口 * @Version 1.0 */ public interface HardCode { public void save(String date); //存储数据 public String get(); //返回数据 }
硬盘实现类
package com.jsxs.principles.demo3.after; /** * @Author Jsxs * @Date 2023/4/16 9:25 * @PackageName:com.jsxs.principles.demo3.after * @ClassName: HardCodeImp * @Description: TODO * @Version 1.0 */ public class HardCodeImp implements HardCode{ @Override public void save(String date) { System.out.println("自定义硬盘存取的数据是: "+date); } @Override public String get() { System.out.println("自定义的硬盘数据"); return "自定的硬盘数据"; } }
Computer组合类
package com.jsxs.principles.demo3.after; /** * @Author Jsxs * @Date 2023/4/16 9:26 * @PackageName:com.jsxs.principles.demo3.after * @ClassName: ComputerImp * @Description: TODO * @Version 1.0 */ public class ComputerImp { private CPUInterface cpuInterface; private HardCode hardCode; private NeicunInterface neicunInterface; public CPUInterface getCpuInterface() { return cpuInterface; } public void setCpuInterface(CPUInterface cpuInterface) { this.cpuInterface = cpuInterface; } public HardCode getHardCode() { return hardCode; } public void setHardCode(HardCode hardCode) { this.hardCode = hardCode; } public NeicunInterface getNeicunInterface() { return neicunInterface; } public void setNeicunInterface(NeicunInterface neicunInterface) { this.neicunInterface = neicunInterface; } public void run(){ System.out.println("自定义的计算机开始工作!!"); cpuInterface.run(); String s = hardCode.get(); hardCode.save(s); neicunInterface.save(); } }
测试类
package com.jsxs.principles.demo3.after; /** * @Author Jsxs * @Date 2023/4/16 9:30 * @PackageName:com.jsxs.principles.demo3.after * @ClassName: ComputerTest * @Description: TODO * @Version 1.0 */ public class ComputerTest { public static void main(String[] args) { // 1.定义三个实现类 CpuImp cpuImp = new CpuImp(); HardCodeImp hardCodeImp = new HardCodeImp(); NeicunImp neicunImp = new NeicunImp(); // 2. 定义一个计算机 ComputerImp computerImp = new ComputerImp(); computerImp.setCpuInterface(cpuImp); computerImp.setHardCode(hardCodeImp); computerImp.setNeicunInterface(neicunImp); computerImp.run(); } }
(4).接口隔离原则 (接口最小化)
客户端不应该被迫依赖于它不适用的方法;一个类对另一个类的依赖应该建立在最小的接口上。
接口不应该被迫依赖它不适用的方法
一个类对另一个类的依赖应该建立在最小的接口上
eg : 安全门案列
我们需要一个 黑马 品牌的安全门,该安全门具有防火、防水、防盗的功能提取成一个接口,形成一套规范。类图如下:
接口
package com.jsxs.principles.demo4.befor; /** * @Author Jsxs * @Date 2023/4/16 10:13 * @PackageName:com.jsxs.principles.demo4.befor * @ClassName: SafeDoor * @Description: TODO 安全门接口 * @Version 1.0 */ public interface SafeDoor { void antiTheft(); //防盗 void fireProof(); //防火 void waterProof(); //防水 }