- 前言
- 一、设计模式七大原则【综述】
- 二、单一职责原则
- 三、接口隔离原则
编写软件的过程中,设计师会面临着来自耦合性、内聚性、可维护性、可扩展性、重用性、灵活性等多方面的挑战。设计模式可让软件代码具有更好的:
① 代码重用性(相同功能的代码不用多次编写)
② 可读性(编程规范性,便于每个程序员阅读和理解)
③ 可扩展性(可方便地增加新功能)
④ 可靠性(增加新功能后对原有功能没有影响)
⑤ 使程序具有高内聚、低耦合的特性
设计模式(Design Pattern):针对软件设计中普遍存在(反复出现)的各种问题的解决方案。
一、设计模式七大原则【综述】
设计模式原则:为编写更加优秀的代码,设计师编程过程中应该遵守的原则。是各种设计模式的基础,是设计模式为什么如此设计的依据。
设计模式原则有7种:
① 弟:迪米特法则
② 里:里氏替换原则
③ 衣:依赖倒置原则
④ 开:开闭原则
⑤ 接:接口隔离原则
⑥ 单:单一职责原则
⑦ 合:合成复用原则
【弟弟里面的衣服开了,只能接单独地合作。】
英文:Single Responsibility Principle
描述:一个类只负责一项职责(一个类只有一个功能)
下面通过代码解释“单一职责原则”
public class Main1 {
public static void main(String[] args) {
Vehicle1 vehicle1 = new Vehicle1();
vehicle1.run("汽车");
vehicle1.run("单车");
vehicle1.run("火车");
// 飞机、轮船不可能在"公路"上运行
vehicle1.run("飞机");
vehicle1.run("轮船");
}
}
class Vehicle1 {
public void run(String vehicle) {
System.out.println(vehicle + "在公路上运行...");
}
}
【代码解释】上面的代码中,Vehicle1 类中有 run 方法。run 方法的作用是“打印某个交通工具在公路上运行…”。在 main 方法中创建了 Vehicle1 类的实例,并调用该实例的 run 方法,分别传入“汽车”、“单车”、“火车”、“飞机”、“轮船”作为该实例的 run 方法的参数。其中把“汽车”、“单车”、“火车”作为参数传给 run 方法没有任何问题,因为它们都是在公路上行驶的交通工具。但是,把“飞机”和“轮船”作为参数传给 run 方法就很有问题。“飞机”是在天空行驶的交通工具,“轮船”是在海面行驶的交通工具。可使用下面的代码解决该问题。
public class Main2 {
public static void main(String[] args) {
Vehicle2 vehicle2 = new Vehicle2();
vehicle2.run("汽车");
vehicle2.run("单车");
vehicle2.run("火车");
vehicle2.runAir("飞机");
vehicle2.runWater("轮船");
}
}
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 + "在水里面运行...");
}
}
【代码解释】上面的代码中,Vehicle2 类中有三个方法。分别是 run(在公路行驶的交通工具调用该方法)、runAir(在空中行驶的交通工具调用该方法)、runWater(在水面行驶的交通工具调用该方法)。根据不同的交通工具在何种地方行驶调用不同的方法即可解决第一份代码中出现的问题。
但是 Vehicle2 显然没有遵循单一职责原则(一个类只负责一项职责)。Vehicle2 既负责“在公路上行驶的交通工具”的方法,也负责“在天空中和水面行驶的交通工具”的方法。这是需要改进的…
把 Vehicle2 类的三个方法的职责分解为三个类的职责,每个类负责一种职责。如下面的代码所示:
public class Main3 {
public static void main(String[] args) {
RoadVehicle roadVehicle = new RoadVehicle();
roadVehicle.run("汽车");
roadVehicle.run("单车");
roadVehicle.run("火车");
AirVehicle airVehicle = new AirVehicle();
airVehicle.run("飞机");
WaterVehicle waterVehicle = new WaterVehicle();
waterVehicle.run("轮船");
}
}
class RoadVehicle {
public void run(String vehicle) {
System.out.println(vehicle + "在公路上运行...");
}
}
class AirVehicle {
public void run(String vehicle) {
System.out.println(vehicle + "在天空中运行...");
}
}
class WaterVehicle {
public void run(String vehicle) {
System.out.println(vehicle + "在水里面运行...");
}
}
【代码解释】在上面的代码中,分别创建了三个类:RoadVehicle、AirVehicle、WaterVehicle。它们分别负责在公路、在天空、在水面的交通工具的运行。与第二份代码不同,第二份代码是通过不同交通工具调用不同方法来解决第一份代码的问题。这样的话,在 Vehicle2 类中杂糅了三种不同类型的交通工具的处理代码。修改公路交通工具的代码的时候可能会影响到其他两种交通工具的代码。上面的代码,把不同类型的交通工具的处理代码分解在不同的类中,大大减少了可能因代码修改而导致连贯错误的可能性。修改“在公路运行的交通工具”的代码就在 RoadVehicle 类中,修改“在天空运行的交通工具”的代码就在 AirVehicle 类中,修改“在水面运行的交通工具”的代码就在 WaterVehicle 类中。修改某种交通工具的代码不会对另一个交通工具产生影响,而第二份代码把三种交通工具通过不同方法的形式写在同一个 Vehicle2 类中大大增加了互相影响的可能性。
单一职责原则好处:
① 降低类的复杂度(一个类只负责一项职责)
② 提高类的可读性、可维护性
③ 降低因类中职责变更而可能引起的“连带”风险
PS:通常情况下应在类的级别上遵守单一职责原则,但若类中方法足够少,可以在方法级别保持单一职责原则。
三、接口隔离原则
英文:Interface Segregation Principle
描述:一个类对另一个类的依赖应建立在最小的接口上。
通过下面的代码引出接口隔离原则。
public class Main1 {
public static void main(String[] args) {
A a = new A();
a.aRun();
C c = new C();
c.cRun();
}
}
interface Interface1 {
void operation1();
void operation2();
void operation3();
void operation4();
void operation5();
}
class BImpl implements Interface1 {
public void operation1() {
System.out.println("B - Interface1 - operation1");
}
public void operation2() {
System.out.println("B - Interface1 - operation2");
}
public void operation3() {
System.out.println("B - Interface1 - operation3");
}
public void operation4() {
System.out.println("B - Interface1 - operation4");
}
public void operation5() {
System.out.println("B - Interface1 - operation5");
}
}
class A { // 需求:A 类对 BImpl 类的依赖应该建立在最小接口上
// A 类只使用到 BImpl 中的1、2、3三个方法
// 但 BImpl 实现了 Interface1 接口中的5个方法
// 根据【接口隔离原则】, BImpl 类实现的接口应该只有1、2、3三个方法的声明
private Interface1 interface1 = new BImpl();
public void aRun() {
interface1.operation1();
interface1.operation2();
interface1.operation3();
}
}
class DImpl implements Interface1 {
public void operation1() {
System.out.println("D - Interface1 - operation1");
}
public void operation2() {
System.out.println("D - Interface1 - operation2");
}
public void operation3() {
System.out.println("D - Interface1 - operation3");
}
public void operation4() {
System.out.println("D - Interface1 - operation4");
}
public void operation5() {
System.out.println("D - Interface1 - operation5");
}
}
class C { // 需求:C 类对 DImpl 类的依赖应该建立在最小接口上
// C 类使用到 DIml 中的1、4、5三个方法
// 但 DImpl 实现了 Interface1 接口中的5个方法
// 根据【接口隔离原则】, DImpl 类实现的接口应该只有1、4、5三个方法的声明
private Interface1 interface1 = new D();
public void cRun() {
interface1.operation1();
interface1.operation4();
interface1.operation5();
}
}
【代码解释】在上面的代码中,声明了接口 Interface1,该接口中有 operation1、operation2、operation3、operation4、operation5 五个抽象方法可供实现。BImpl 类实现了 Interface1 接口,并实现了 Interface1 接口中的5个方法。BImpl 类实现了 Interface1 接口,并实现了 Interface1 接口中的5个方法。A 类中通过 Interface1 接口依赖 BImpl 类,A 类中存在方法 aRun,在方法 aRun 中通过 interface1 只调用 operation1、operation2、operation3等三个方法。但通过 Interface1 接口依赖的 BImpl 类中却实现了 Interface1 接口的5个方法,operation4 和 operation5 两个方法没有在 A 类中被使用到。根据接口隔离原则(一个类对另一个类的依赖应建立在最小的接口上),A 类对 BImpl 类的依赖并没有建立在最小接口上,Interface1 接口中存在 operation4 和 operation5 两个方法是多余的(A 类中没有使用到的)。按照接口隔离原则,BImpl 实现的接口中应该只有 3 个抽象方法的声明(即 operation1、operation2 和 operation2),但 Interface1 接口却有5个方法的声明。所以,A 类对 BImpl 类的依赖并没有建立在最小接口上。同样,DImpl 类和 C 类的关系和 BImpl 类和 A 类的关系是一样的。
为了遵循接口隔离原则,应该将 Interface1 接口拆分为3个接口。类 A 和类 C 分别与它们需要的接口建立依赖关系。
public class Main2 {
public static void main(String[] args) {
Anna anna = new Anna();
anna.depend1();
anna.depend2();
anna.depend3();
Candy candy = new Candy();
candy.depend1();
candy.depend2();
candy.depend3();
}
}
interface InterfaceA {
void operation1();
}
interface InterfaceB {
void operation2();
void operation3();
}
interface InterfaceC {
void operation4();
void operation5();
}
class Boy implements InterfaceA, InterfaceB {
public void operation1() {
System.out.println("Boy - InterfaceA - operation1");
}
public void operation2() {
System.out.println("Boy - InterfaceB - operation2");
}
public void operation3() {
System.out.println("Boy - InterfaceB - operation3");
}
}
class Dog implements InterfaceA, InterfaceC {
public void operation1() {
System.out.println("Dog - InterfaceA - operation1");
}
public void operation4() {
System.out.println("Dog - InterfaceC - operation4");
}
public void operation5() {
System.out.println("Dog - InterfaceC - operation5");
}
}
class Anna {
// Anna 通过 InterfaceA 依赖 Boy
private InterfaceA interfaceA = new Boy();
// Anna 通过 InterfaceB 依赖 Boy
private InterfaceB interfaceB = new Boy();
public void depend1() {
interfaceA.operation1();
}
public void depend2() {
interfaceB.operation2();
}
public void depend3() {
interfaceB.operation3();
}
}
class Candy {
// Candy 通过接口 InterfaceA 依赖 Dog 类
private InterfaceA interfaceA = new Dog();
// Candy 通过接口 InterfaceC 依赖 Dog 类
private InterfaceC interfaceC = new Dog();
public void depend1() {
interfaceA.operation1();
}
public void depend2() {
interfaceC.operation4();
}
public void depend3() {
interfaceC.operation5();
}
}



