设计模式的六大原则
1.1基本介绍
对类来说的,即一个类应该只负责一项职责。如类 A负责两个不同职责:职责 1,职责 2。当职责 1需求变更
而改变 A时,可能造成职责 2执行错误,所以需要将类 A的粒度分解为 A1,A2
1.2应用案例
1 1) 方案 1 [分析说明] 2 package com.atguigu.principle.singleresponsibility; 3 public class SingleResponsibility1 { 4 public static void main(String[] args) { 5 // TODOAuto-generated method stub 6 Vehicle vehicle = new Vehicle(); 7 vehicle.run("摩托车"); 8 vehicle.run("汽车"); 9 vehicle.run("飞机"); 10 } 11 } 12 // 交通工具类 13 // 方式 1 14 // 1. 在方式 1 的 run方法中,违反了单一职责原则 15 // 2. 解决的方案非常的简单,根据交通工具运行方法不同,分解成不同类即可 16 class Vehicle { 17 public void run(String vehicle) { 18 System.out.println(vehicle + " 在公路上运行...."); 19 } 20 }
1 2 2) 方案 2 [分析说明] 3 package com.atguigu.principle.singleresponsibility; 4 public class SingleResponsibility2 { 5 public static void main(String[] args) { 6 // TODOAuto-generated method stub 7 RoadVehicle roadVehicle = new RoadVehicle(); 8 roadVehicle.run("摩托车"); 9 roadVehicle.run("汽车"); 10 AirVehicle airVehicle = new AirVehicle(); 11 airVehicle.run("飞机"); 12 } 13 } 14 //方案 2的分析 15 //1. 遵守单一职责原则 16 //2. 但是这样做的改动很大,即将类分解,同时修改客户端 17 //3. 改进:直接修改 Vehicle 类,改动的代码会比较少=>方案 3 18 class RoadVehicle { 19 public void run(String vehicle) { 20 System.out.println(vehicle + "公路运行"); 21 } 22 } 23 class AirVehicle { 24 public void run(String vehicle) { 25 System.out.println(vehicle + "天空运行"); 26 } 27 } 28 class WaterVehicle { 29 public void run(String vehicle) { 30 System.out.println(vehicle + "水中运行"); 31 } 32 }
1 3) 方案 3 [分析说明] 2 package com.atguigu.principle.singleresponsibility; 3 public class SingleResponsibility3 { 4 public static void main(String[] args) { 5 // TODOAuto-generated method stub 6 Vehicle2 vehicle2 = new Vehicle2(); 7 vehicle2.run("汽车"); 8 vehicle2.runWater("轮船"); 9 vehicle2.runAir("飞机"); 10 } 11 } 12 //方式 3的分析 13 //1. 这种修改方法没有对原来的类做大的修改,只是增加方法 14 //2. 这里虽然没有在类这个级别上遵守单一职责原则,但是在方法级别上,仍然是遵守单一职责 15 class Vehicle2 { 16 public void run(String vehicle) { 17 //处理 18 System.out.println(vehicle + " 在公路上运行...."); 19 } 20 public void runAir(String vehicle) { 21 System.out.println(vehicle + " 在天空上运行...."); 22 } 23 public void runWater(String vehicle) { 24 System.out.println(vehicle + " 在水中行...."); 25 } 26 //方法 2. 27 //.. 28 //.. 29 //... 30 }
1.3 单一职责原则注意事项和细节
1) 降低类的复杂度,一个类只负责一项职责。
2) 提高类的可读性,可维护性
3) 降低变更引起的风险
4) 通常情况下,我们应当遵守单一职责原则,只有逻辑足够简单,才可以在代码级违反单一职责原则;只有类中
方法数量足够少,可以在方法级别保持单一职责原则
二丶接口隔离原则
2.1基本介绍
1) 客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口上
2) 先看一张图:
3) 类 A 通过接口 Interface1 依赖类 B,类 C 通过接口 Interface1 依赖类 D,如果接口 Interface1 对于类 A 和类 C
来说不是最小接口,那么类 B和类 D必须去实现他们不需要的方法。
4) 按隔离原则应当这样处理:
将接口 Interface1拆分为独立的几个接口(这里我们拆分成 3个接口),类 A和类 C分别与他们需要的接口建立
依赖关系。也就是采用接口隔离原则
2.2应用实例
1) 类 A通过接口 Interface1依赖类 B,类 C通过接口 Interface1依赖类 D,请编写代码完成此应用实例。
2) 看老师代码-没有使用接口隔离原则代码
1 package com.atguigu.principle.segregation; 2 public class Segregation1 { 3 public static void main(String[] args) { 4 // TODOAuto-generated method stub 5 } 6 } 7 //接口 8 interface Interface1 { 9 void operation1(); 10 void operation2(); 11 void operation3(); 12 void operation4(); 13 void operation5(); 14 } 15 class B implements Interface1 { 16 public void operation1() { 17 System.out.println("B 实现了 operation1"); 18 } 19 public void operation2() { 20 System.out.println("B 实现了 operation2"); 21 } 22 public void operation3() { 23 System.out.println("B 实现了 operation3"); 24 } 25 public void operation4() { 26 System.out.println("B 实现了 operation4"); 27 } 28 public void operation5() { 29 System.out.println("B 实现了 operation5"); 30 } 31 } 32 class D implements Interface1 { 33 public void operation1() { 34 System.out.println("D 实现了 operation1"); 35 } 36 public void operation2() { 37 System.out.println("D 实现了 operation2"); 38 } 39 public void operation3() { 40 System.out.println("D 实现了 operation3"); 41 } 42 public void operation4() { 43 System.out.println("D 实现了 operation4"); 44 } 45 public void operation5() { 46 System.out.println("D 实现了 operation5"); 47 } 48 } 49 class A { //A 类通过接口 Interface1 依赖(使用) B类,但是只会用到 1,2,3方法 50 public void depend1(Interface1 i) { 51 i.operation1(); 52 } 53 public void depend2(Interface1 i) { 54 i.operation2(); 55 } 56 public void depend3(Interface1 i) { 57 i.operation3(); 58 } 59 } 60 class C { //C 类通过接口 Interface1 依赖(使用) D类,但是只会用到 1,4,5方法 61 public void depend1(Interface1 i) { 62 i.operation1(); 63 } 64 public void depend4(Interface1 i) { 65 i.operation4(); 66 } 67 public void depend5(Interface1 i) { 68 i.operation5(); 69 } 70 }
2.4.3应传统方法的问题和使用接口隔离原则改进
1) 类