设计模式主要是为了解决在编写代码过程中,面临的耦合性、内聚性、可维护性、可扩展行、重用性、灵活性等多方面的挑战。
设计模式原则,其实就是程序员在编程时,应当遵守的原则,也是各种设计模式的基础。
对类来说的,即一个类应该只负责一项职责。如类A负责两个不同职责:职责1,职责2。当职责1需求变更而改变A时,可能造成职责2执行错误,所以需要将类A的粒度分解为A1,A2
/** * 方案1:违反单一职责,所有的交通工具采用同一种方式运行 */ public class SingleResponsibility1 { public static void main(String[] args) { Vehicle vehicle = new Vehicle(); vehicle.run("摩托车"); vehicle.run("汽车"); vehicle.run("飞机"); } } class Vehicle{ public void run(String vehicle) { System.out.println(vehicle + " 在公路上运行...."); } }
/** * 方案2的分析 * 1. 遵守单一职责原则 * 2. 但是这样做的改动很大,即将类分解,同时修改客户端 * 3. 改进:直接修改Vehicle 类,改动的代码会比较少=>方案3 */ public class SingleResponsibility2 { public static void main(String[] args) { RoadVehicle roadVehicle = new RoadVehicle(); roadVehicle.run("摩托车"); roadVehicle.run("汽车"); AirVehicle airVehicle = new AirVehicle(); airVehicle.run("飞机"); } } class RoadVehicle { public void run(String vehicle) { System.out.println(vehicle + " 公路运行"); } } class AirVehicle { public void run(String vehicle) { System.out.println(vehicle + " 天空运行"); } }
/** * 方式3的分析 * 1. 这种修改方法没有对原来的类做大的修改,只是增加方法 * 2. 这里虽然没有在类这个级别上遵守单一职责原则,但是在方法级别上,仍然是遵守单一职责 */ public class SingleResponsibility3 { public static void main(String[] args) { Vehicle2 vehicle2 = new Vehicle2(); vehicle2.run("汽车"); vehicle2.runWater("轮船"); vehicle2.runAir("飞机"); } } class Vehicle2 { public void run(String vehicle) { System.out.println(vehicle + " 在公路上运行...."); } public void runAir(String vehicle) { System.out.println(vehicle + " 在天空上运行...."); } public void runWater(String vehicle) { System.out.println(vehicle + " 在水中行...."); } }
客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口上。
类A通过接口Interface1依赖类B,类C通过接口Interface1依赖类D。但是类A只需要操作类B中的operation1、operation2和operation3,同时类C也只需要操作类D中的operation1、operation4和operation5,但是由于类B和类D都是接口Interface1的实现类,故而都重写了不需要的方法。
源码:基本案例
public interface Interface1 { void operation1(); void operation2(); void operation3(); void operation4(); void operation5(); } public abstract class A { public void dependecy1(Interface1 interface1) { interface1.operation1(); } public void dependecy2(Interface1 interface1) { interface1.operation2(); } public void dependecy3(Interface1 interface1) { interface1.operation3(); } } public class B implements Interface1{ @Override public void operation1() { System.out.println("B实现了operation1"); } @Override public void operation2() { System.out.println("B实现了operation2"); } @Override public void operation3() { System.out.println("B实现了operation3"); } @Override public void operation4() { System.out.println("B实现了operation4"); } @Override public void operation5() { System.out.println("B实现了operation5"); } } public class C { public void dependecy1(Interface1 interface1) { interface1.operation1(); } public void dependecy4(Interface1 interface1) { interface1.operation4(); } public void dependecy5(Interface1 interface1) { interface1.operation5(); } } public class D implements Interface1{ @Override public void operation1() { System.out.println("D实现了operation1"); } @Override public void operation2() { System.out.println("D实现了operation2"); } @Override public void operation3() { System.out.println("D实现了operation3"); } @Override public void operation4() { System.out.println("D实现了operation4"); } @Override public void operation5() { System.out.println("D实现了operation5"); } }
将接口Interface1拆分为独立的几个接口,类A和类C分别与他们需要的接口建立依赖关系,即采用接口隔离原则。
源码:接口隔离实现
public interface Interface1 { void operation1(); } public interface Interface2 { void operation2(); void operation3(); } public interface Interface3 { void operation4(); void operation5(); } public class A { public void dependecy1(Interface1 interface1) { interface1.operation1(); } public void dependecy2(Interface2 interface2) { interface2.operation2(); } public void dependecy3(Interface2 interface2) { interface2.operation3(); } } public class B implements Interface1, Interface2 { @Override public void operation1() { System.out.println("B实现了operation1"); } @Override public void operation2() { System.out.println("B实现了operation2"); } @Override public void operation3() { System.out.println("B实现了operation3"); } } public class C { public void dependecy1(Interface1 interface1) { interface1.operation1(); } public void dependecy4(Interface3 interface3) { interface3.operation4(); } public void dependecy5(Interface3 interface3) { interface3.operation5(); } } public class D implements Interface1, Interface3 { @Override public void operation1() { System.out.println("D实现了operation1"); } @Override public void operation4() { System.out.println("D实现了operation4"); } @Override public void operation5() { System.out.println("D实现了operation5"); } }
/** * 方案1:完成Person接受消息的功能 * 简单易懂 * 如果我们获取的对象是 微信、短信等,则新增类,同时Person也要增加对应的接收方法 */ public class Person { public void receive(Email email){ System.out.println(email.getInfo()); } } class Email{ public String getInfo() { return "电子邮件信息:hello word"; } }
/** * 方案2:依赖倒置原则 * 引入一个抽象的接口IReceiver接口,让不同的接收消息方法都实现这个接口重写getInfo方法 * 由调用方决定传入的具体对象 */ public class Person { public void receive(IReceiver receiver) { System.out.println(receiver.getInfo()); } } interface IReceiver{ public String getInfo(); } class Email implements IReceiver{ @Override public String getInfo() { return "电子邮件信息:hello word"; } } class WeiXin implements IReceiver { @Override public String getInfo() { return "微信信息:hello wechat"; } }
/** * 接口传递的方式,将接口ITV传递open方法 */ class OPenAndClose implements IOPenAndClose { @Override public void open(ITV itv) { itv.play(); } } interface IOPenAndClose { public void open(ITV itv); } interface ITV { public void play(); }
//方式二:构造方法传递 interface IOpenAndClose { public void open();//抽象方法 } interface ITV {//ITV接口 public void play(); } class OpenAndClose implements IOpenAndClose { public ITV tv;//成员 public OpenAndClose(ITV tv) {//构造方法 this.tv = tv; } @Override public void open() { this.tv.play(); } }
//方式三:setter方法传递 interface IOpenAndClose { public void open();//抽象方法 } interface ITV {//ITV接口 public void play(); } class OpenAndClose implements IOpenAndClose { private ITV tv; public void setTv(ITV tv) { this.tv = tv; } @Override public void open() { this.tv.play(); } }
public class A { public int fuc1(int a, int b) { return a - b; } } /** * 类B继承A * 由于B无意识重写了A的fuc1方法,导致最终调用时发现预期类A的fuc1不生效 */ class B extends A { public int fuc1(int a, int b) { return a + b; } public int fuc2(int a, int b) { return a * b; } }
//定义基类 public class Base { } class A extends Base { public int fuc1(int a, int b) { return a - b; } } /** * 类A和类B继承Base类 * B使用组合的方式使用A,这样fuc3仍然是类A的方法 */ class B extends Base { A a = new A(); //这里,重写了 A 类的方法, 可能是无意识 public int fuc1(int a, int b) { return a + b; } public int fuc2(int a, int b) { return fuc1(a, b) + 9; } //我们仍然想使用 A 的方法 public int fuc3(int a, int b) { return this.a.fuc1(a, b); } }
public class OCP { public static void main(String[] args) { GraphicEditor graphicEditor = new GraphicEditor(); graphicEditor.drawShape(new Rectangle()); graphicEditor.drawShape(new Circle()); } } //绘图的类,使用方 class GraphicEditor{ //根据不同的type绘制不同的图 public void drawShape(Shape s) { if (s.type == 1) { drawRectangle(s); } else if (s.type == 2) { drawRectangle(s); } } public void drawRectangle(Shape r) { System.out.println("绘制矩形"); } public void drawCircle(Shape r) { System.out.println("绘制圆形"); } } //基类 abstract class Shape{ int type; } class Rectangle extends Shape { public Rectangle() { super.type = 1; } } class Circle extends Shape { public Circle() { super.type = 2; } }
问题:该方案违反了设计模式的开闭原则,当需要增加一个图形种类(例:三角形),不仅需要新建类,而且使用方代码也需要添加适配
public class OCP { public static void main(String[] args) { GraphicEditor graphicEditor = new GraphicEditor(); graphicEditor.draw(new Rectangle()); graphicEditor.draw(new Circle()); } } class GraphicEditor{ public void draw(Shape s) { s.draw(); } } abstract class Shape{ public abstract void draw(); } class Rectangle extends Shape { @Override public void draw() { System.out.println("绘制矩形"); } } class Circle extends Shape { @Override public void draw() { System.out.println("绘制圆形"); } }
有一个学校,下属有各个学院和总部,现要求打印出学校总部员工 ID 和学院员工的 id
public class Demeter { public static void main(String[] args) { SchoolManage manage = new SchoolManage(); manage.printAllEmp(new CollegeManage()); } } //学校总部员工 class Employee { private String id; public String getId() { return id; } public void setId(String id) { this.id = id; } } //学院员工 class CollegeEmployee { private String id; public String getId() { return id; } public void setId(String id) { this.id = id; } } //学院管理 class CollegeManage { public List<CollegeEmployee> getAllEmployee() { List<CollegeEmployee> list = new ArrayList<CollegeEmployee>(); for (int i = 0; i < 10; i++) { CollegeEmployee emp = new CollegeEmployee(); emp.setId("学院员工 id= " + i); list.add(emp); } return list; } } //学校总部管理 class SchoolManage { public List<Employee> getAllEmployee() { List<Employee> list = new ArrayList<Employee>(); for (int i = 0; i < 10; i++) { Employee emp = new Employee(); emp.setId("学校总部员工 id= " + i); list.add(emp); } return list; } //这里的 CollegeEmployee 不是 SchoolManager 的直接朋友 //违反了迪米特法则 public void printAllEmp(CollegeManage collegeManage) { List<CollegeEmployee> allEmployee = collegeManage.getAllEmployee(); System.out.println("------------学院员工------------"); for (CollegeEmployee e : allEmployee) { System.out.println(e.getId()); } //获取到学校总部员工 List<Employee> list2 = this.getAllEmployee(); System.out.println("------------学校总部员工------------"); for (Employee e : list2) { System.out.println(e.getId()); } } }
问题:由于在SchoolManage中,CollegeEmployee类并不是SchoolManage类的直接朋友。按照迪米特法则,应该避免类中出现这样非直接朋友关系的耦合
优化:完整代码
//学院管理 class CollegeManage { public List<CollegeEmployee> getAllEmployee() { List<CollegeEmployee> list = new ArrayList<CollegeEmployee>(); for (int i = 0; i < 10; i++) { CollegeEmployee emp = new CollegeEmployee(); emp.setId("学院员工 id= " + i); list.add(emp); } return list; } //在学院管理中增加遍历类,方便直接被调用 public void printCollegeEmp() { List<CollegeEmployee> allEmployee = this.getAllEmployee(); System.out.println("------------学院员工------------"); for (CollegeEmployee e : allEmployee) { System.out.println(e.getId()); } } } //学校总部管理 class SchoolManage { public List<Employee> getAllEmployee() { List<Employee> list = new ArrayList<Employee>(); for (int i = 0; i < 10; i++) { Employee emp = new Employee(); emp.setId("学校总部员工 id= " + i); list.add(emp); } return list; } public void printAllEmp(CollegeManage collegeManage) { collegeManage.printCollegeEmp(); //获取到学校总部员工 List<Employee> list2 = this.getAllEmployee(); System.out.println("------------学校总部员工------------"); for (Employee e : list2) { System.out.println(e.getId()); } } }
原文:https://www.cnblogs.com/bbgs-xc/p/14975479.html