第二回 设计模式七大原则
1.单一职责原则
1.1基本介绍
对类来说的,即一个类应该只负责一项职责。如类 A 负责两个不同职责:职责 1,职责 2。当职责 1 需求变更 而改变 A 时,可能造成职责 2 执行错误,所以需要将类 A 的粒度分解为 A1,A2。
该原则注意事项:
1) 降低类的复杂度,一个类只负责一项职责。
2) 提高类的可读性,可维护性
3) 降低变更引起的风险
4) 通常情况下,我们应当遵守单一职责原则,只有逻辑足够简单,才可以在代码级违反单一职责原则;只有类中方法数量足够少,可以在方法级别保持单一职责原则
1.2应用实例
以交通工具案例
package com.wx.principle.singleresponsibility;
public class SingleResponsibility3 {
public static void main(String[] args) {
Vehicle2 vehicle2 = new Vehicle2();
vehicle2.run("汽车");
vehicle2.runWater("轮船");
vehicle2.runAir("飞机");
}
}
//方式 3 的分析
//1. 这种修改方法没有对原来的类做大的修改,只是增加方法
//2. 这里虽然没有在类这个级别上遵守单一职责原则,但是在方法级别上,仍然是遵守单一职责
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 + " 在水中行....");
}
}
2.接口隔离原则
2.1基本介绍
客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口上。
如上图中:类A通过接口Interface1依赖类B,类C通过接口Interface1依赖类D,如果接口Interface1对于类A和类C来说不是最小接口,那么类 B 和类 D 必须去实现他们不需要的方法。
按隔离原则应当这样处理:将接口 Interface1 拆分为独立的几个接口(这里我们拆分成 3 个接口),类 A 和类 C 分别与他们需要的接口建立 依赖关系。也就是采用接口隔离原则。
2.2应用实例
package com.wx.principle.segregation.improve;
public class Segregation1 {
public static void main(String[] args) {
A a = new A();
a.depend1(new B()); // A 类通过接口去依赖 B 类 a.depend2(new B());
a.depend3(new B());
C c = new C();
c.depend1(new D()); // C 类通过接口去依赖(使用)D 类 c.depend4(new D());
c.depend5(new D());
}
}
// 接口 1
interface Interface1 {
void operation1();
}
// 接口 2
interface Interface2 {
void operation2();
void operation3();
}
// 接口 3
interface Interface3 {
void operation4();
void operation5();
}
class B implements Interface1, Interface2 {
public void operation1() { System.out.println("B 实现了 operation1");}
public void operation2() { System.out.println("B 实现了 operation2");}
public void operation3() { System.out.println("B 实现了 operation3");}
}
class D implements Interface1, Interface3{
public void operation1() { System.out.println("D 实现了 operation1");}
public void operation4() { System.out.println("D 实现了 operation4");}
public void operation5() { System.out.println("D 实现了 operation5");}
}
class A { // A 类通过接口 Interface1,Interface2 依赖(使用) B 类,但是只会用到 1,2,3 方法
public void depend1(Interface1 i) { i.operation1();}
public void depend2(Interface2 i) { i.operation2();}
public void depend3(Interface2 i) { i.operation3();}
}
class C { // C 类通过接口 Interface1,Interface3 依赖(使用) D 类,但是只会用到 1,4,5 方法
public void depend1(Interface1 i) { i.operation1();}
public void depend4(Interface3 i) { i.operation4();}
public void depend5(Interface3 i) { i.operation5();}
}
3.依赖倒转原则
3.1基本介绍
依赖倒转原则(Dependence Inversion Principle)是指:
1) 高层模块不应该依赖低层模块,二者都应该依赖其抽象
2) 抽象不应该依赖细节,细节应该依赖抽象
3) 依赖倒转(倒置)的中心思想是面向接口编程
4) 依赖倒转原则是基于这样的设计理念:相对于细节的多变性,抽象的东西要稳定的多。以抽象为基础搭建的架构比以细节为基础的架构要稳定的多。在 java 中,抽象指的是接口或抽象类,细节就是具体的实现类
5) 使用接口或抽象类的目的是制定好规范,而不涉及任何具体的操作,把展现细节的任务交给他们的实现类去完成
注意事项:
1) 低层模块尽量都要有抽象类或接口,或者两者都有,程序稳定性更好.
2) 变量的声明类型尽量是抽象类或接口, 这样我们的变量引用和实际对象间,就存在一个缓冲层,利于程序扩展和优化
3) 继承时遵循里氏替换原则
3.2应用实例
package com.wx.principle.inversion.improve;
public class DependencyPass {
public static void main(String[] args) {
ChangHong changHong = new ChangHong();
// OpenAndClose openAndClose = new OpenAndClose();
// openAndClose.open(changHong);
//通过构造器进行依赖传递
// OpenAndClose openAndClose = new OpenAndClose(changHong);
// openAndClose.open();
//通过 setter 方法进行依赖传递
OpenAndClose openAndClose = new OpenAndClose();
openAndClose.setTv(changHong);
openAndClose.open();
}
}
// 方式 1: 通过接口传递实现依赖
// 开关的接口
//interface IOpenAndclose{public void open(ITV tv);//抽象方法,接收接口}
//ITV接口
//interface ITV{ public void play();}
//class ChangHong implements ITV{
// @Override
// public void play(){System.out.println("长虹电视机打开");}
// }
//实现接口
//Class OpenAndClose implements IOpenAndClose{
// public void open(){tv.play();}}
// 方式 2: 通过构造方法依赖传递
// interface IOpenAndClose {
// public void open(); //抽象方法 // }
//ITV 接口
// interface ITV { public void play();}
// class OpenAndClose implements IOpenAndClose{ // public ITV tv; //成员
// public OpenAndClose(ITV tv){ //构造器
// this.tv = tv;
// }
// public void open(){
// this.tv.play();
// }
// }
// 方式 3 , 通过 setter 方法传递 interface IOpenAndClose {
public void open(); // 抽象方法 public void setTv(ITV tv);
}
interface ITV { // ITV 接口 public void play();
}
class OpenAndClose implements IOpenAndClose { private ITV tv;
public void setTv(ITV tv) { this.tv = tv;
}
public void open() { this.tv.play();
} }
class ChangHong implements ITV {
@Override
public void play() {
// TODO Auto-generated method stub
System.out.println("长虹电视机,打开"); }
}