设计模式的目的
编写软件过程中,程序员面临着来自 耦合性,内聚性以及可维护性,可扩展性,重用性,灵活性 等多方面的挑战,设计模式是为了让程序(软件),具有更好
1) 代码重用性 (即:相同功能的代码,不用多次编写)
2) 可读性 (即:编程规范性, 便于其他程序员的阅读和理解)
3) 可扩展性 (即:当需要增加新的功能时,非常的方便,称为可维护)
4) 可靠性 (即:当我们增加新的功能后,对原来的功能没有影响)
5) 使程序呈现高内聚,低耦合的特性
分享金句:
设计模式包含了面向对象的精髓, “懂了设计模式,你就懂了面向对象分析和设计 (OOA/D)的精要”Scott Mayers 在其巨著《Effective C++》就曾经说过:C++老手和 C++新手的区别就是前者手背上有很多伤疤
设计模式原则,其实就是程序员在编程时,应当遵守的原则,也是各种设计模式的基础(即:设计模式为什么这样设计的依据)
设计模式常用的七大原则有:
1) 单一职责原则
2) 接口隔离原则
3) 依赖倒转(倒置)原则
4) 里氏替换原则
5) 开闭原则
6) 迪米特法则
7) 合成复用原则
单一职责原则
基本介绍
对类来说的,即一个类应该只负责一项职责。如类A负责两个不同职责:职责1,职责2。 当职责1需求变更而改变A时,可能造成职责2执行错误,所以需要将类A的粒度分解为A1,A2
应用实例
1. public class SingleResponsibility1 { 2. public static void main(String[] args) { 3. Vehicle vehicle = new Vehicle(); 4. vehicle.run("摩托车"); 5. vehicle.run("汽车"); 6. vehicle.run("飞机"); 7. } 8. } 9. //交通工具类 10. 11. class Vehicle { 12. public void run(String vehicle) { 13. System.out.println(vehicle + " 在公路上运行...."); 14. } 15. }
1. 在上面的方式 的 run 方法中,违反了单一职责原则
2. 解决的方案非常的简单,根据交通工具运行方法不同,分解成不同类即可
1. public class SingleResponsibility2 { 2. public static void main(String[] args) { 3. RoadVehicle roadVehicle = new RoadVehicle(); 4. roadVehicle.run("摩托车"); 5. roadVehicle.run("汽车"); 6. AirVehicle airVehicle = new AirVehicle(); 7. airVehicle.run("飞机"); 8. } 9. } 10. 11. //方案 2 的分析 12. //1. 遵守单一职责原则 13. //2. 但是这样做的改动很大,即将类分解,同时修改客户端 14. //3. 改进:直接修改 Vehicle 类,改动的代码会比较少=>方案 3 15. class RoadVehicle { 16. public void run(String vehicle) { 17. System.out.println(vehicle + "公路运行"); 18. } 19. } 20. 21. class AirVehicle { 22. public void run(String vehicle) { 23. System.out.println(vehicle + "天空运行"); 24. } 25. } 26. 27. class WaterVehicle { 28. public void run(String vehicle) { 29. System.out.println(vehicle + "水中运行"); 30. } 31. }
1. 遵守单一职责原则
2. 但是这样做的改动很大,即将类分解,同时修改客户端
3. 改进:直接修改 Vehicle 类,改动的代码会比较少=>方案 3
1. public class SingleResponsibility3 { 2. public static void main(String[] args) { 3. Vehicle2 vehicle2 = new Vehicle2(); 4. vehicle2.run("汽车"); 5. vehicle2.runWater("轮船"); 6. vehicle2.runAir("飞机"); 7. } 8. } 9. 10. class Vehicle2 { 11. public void run(String vehicle) { 12. //处理 13. System.out.println(vehicle + " 在公路上运行...."); 14. } 15. 16. public void runAir(String vehicle) { 17. System.out.println(vehicle + " 在天空上运行...."); 18. } 19. 20. public void runWater(String vehicle) { 21. System.out.println(vehicle + " 在水中行...."); 22. } 23. //方法 2. 24. //.. 25. //.. 26. //... 27. }
1.这种修改方法没有对原来的类做大的修改,只是增加方法
2.这里虽然没有在类这个级别上遵守单一职责原则,但是在方法级别上,仍然是遵守单一职责
注意事项和细节
单一职责原则注意事项和细节:
1) 降低类的复杂度,一个类只负责一项职责。
2) 提高类的可读性,可维护性
3) 降低变更引起的风险
4) 通常情况下,我们应当遵守单一职责原则,只有逻辑足够简单,才可以在代码级违反单一职责原则;只有类中方法数量足够少,可以在方法级别保持单一职责原则
接口隔离原则(Interface Segregation Principle)
基本介绍
客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口上
类 A 通过接口 Interface1 依赖类 B,类 C 通过接口 Interface1 依赖类 D,如果接口 Interface1 对于类 A 和类 C来说不是最小接口,那么类 B 和类 D 必须去实现他们不需要的方法。
按隔离原则应当这样处理:
将接口 Interface1 拆分为独立的几个接口(这里我们拆分成 3 个接口),类 A 和类 C 分别与他们需要的接口建立依赖关系。也就是采用接口隔离原则
应用实例
没有使用接口隔离原则代码
1. public class Segregation1 { 2. public static void main(String[] args) { 3. 4. } 5. } 6. 7. interface Interface1 { 8. void operation1(); 9. 10. void operation2(); 11. 12. void operation3(); 13. 14. void operation4(); 15. 16. void operation5(); 17. } 18. 19. class B implements Interface1 { 20. public void operation1() { 21. System.out.println("B 实现了 operation1"); 22. } 23. 24. public void operation2() { 25. System.out.println("B 实现了 operation2"); 26. } 27. 28. public void operation3() { 29. System.out.println("B 实现了 operation3"); 30. } 31. 32. public void operation4() { 33. System.out.println("B 实现了 operation4"); 34. } 35. 36. public void operation5() { 37. System.out.println("B 实现了 operation5"); 38. } 39. } 40. 41. class D implements Interface1 { 42. public void operation1() { 43. System.out.println("D 实现了 operation1"); 44. } 45. 46. public void operation2() { 47. System.out.println("D 实现了 operation2"); 48. } 49. 50. public void operation3() { 51. System.out.println("D 实现了 operation3"); 52. } 53. 54. public void operation4() { 55. System.out.println("D 实现了 operation4"); 56. } 57. 58. public void operation5() { 59. System.out.println("D 实现了 operation5"); 60. } 61. } 62. 63. class A { //A 类通过接口 Interface1 依赖(使用) B 类,但是只会用到 1,2,3 方法 64. public void depend1(Interface1 i) { 65. i.operation1(); 66. } 67. 68. public void depend2(Interface1 i) { 69. i.operation2(); 70. } 71. 72. public void depend3(Interface1 i) { 73. i.operation3(); 74. } 75. } 76. 77. class C { //C 类通过接口 Interface1 依赖(使用) D 类,但是只会用到 1,4,5 方法 78. public void depend1(Interface1 i) { 79. i.operation1(); 80. } 81. 82. public void depend4(Interface1 i) { 83. i.operation4(); 84. } 85. 86. public void depend5(Interface1 i) { 87. i.operation5(); 88. } 89. }
应传统方法的问题和使用接口隔离原则改进
1) 类 A 通过接口 Interface1 依赖类 B,类 C 通过接口 Interface1 依赖类 D,如果接口 Interface1 对于类 A 和类 C来说不是最小接口,那么类 B 和类 D 必须去实现他们不需要的方法
2) 将接口 Interface1 拆分为独立的几个接口,类 A 和类 C 分别与他们需要的接口建立依赖关系。也就是采用接口隔离原则
3) 接口 Interface1 中出现的方法,根据实际情况拆分为三个接口
1. public class Segregation1 { 2. public static void main(String[] args) { 3. // TODO Auto-generated method stub 4. // 使用一把 5. A a = new A(); 6. a.depend1(new B()); // A 类通过接口去依赖 B 类 7. a.depend2(new B()); 8. a.depend3(new B()); 9. C c = new C(); 10. c.depend1(new D()); // C 类通过接口去依赖(使用)D 类 11. c.depend4(new D()); 12. c.depend5(new D()); 13. } 14. } 15. 16. // 接口 1 17. interface Interface1 { 18. void operation1(); 19. } 20. 21. // 接口 2 22. interface Interface2 { 23. void operation2(); 24. 25. void operation3(); 26. } 27. 28. // 接口 3 29. interface Interface3 { 30. void operation4(); 31. 32. void operation5(); 33. } 34. 35. class B implements Interface1, Interface2 { 36. public void operation1() { 37. System.out.println("B 实现了 operation1"); 38. } 39. 40. public void operation2() { 41. System.out.println("B 实现了 operation2"); 42. } 43. 44. public void operation3() { 45. System.out.println("B 实现了 operation3"); 46. } 47. } 48. 49. class D implements Interface1, Interface3 { 50. public void operation1() { 51. System.out.println("D 实现了 operation1"); 52. } 53. 54. public void operation4() { 55. System.out.println("D 实现了 operation4"); 56. } 57. 58. public void operation5() { 59. System.out.println("D 实现了 operation5"); 60. } 61. } 62. 63. class A { // A 类通过接口 Interface1,Interface2 依赖(使用) B 类,但是只会用到 1,2,3 方法 64. public void depend1(Interface1 i) { 65. i.operation1(); 66. } 67. 68. public void depend2(Interface2 i) { 69. i.operation2(); 70. } 71. 72. public void depend3(Interface2 i) { 73. i.operation3(); 74. } 75. } 76. 77. class C { // C 类通过接口 Interface1,Interface3 依赖(使用) D 类,但是只会用到 1,4,5 方法 78. public void depend1(Interface1 i) { 79. i.operation1(); 80. } 81. 82. public void depend4(Interface3 i) { 83. i.operation4(); 84. } 85. 86. public void depend5(Interface3 i) { 87. i.operation5(); 88. } 89. }