设计模式原则(上)

简介: 设计模式原则

1、设计模式七大原则

1.1 设计模式的目的

编写软件过程中,程序员面临着来自 耦合性,内聚性以及可维护性,可扩展性,重用性,灵活性 等多方面的挑战,设计模式是为了让程序(软件),具有更好

  1. 代码重用性 (即:相同功能的代码,不用多次编写)
  2. 可读性 (即:编程规范性, 便于其他程序员的阅读和理解)
  3. 可扩展性 (即:当需要增加新的功能时,非常的方便,称为可维护)
  4. 可靠性 (即:当我们增加新的功能后,对原来的功能没有影响)
  5. 使程序呈现高内聚,低耦合的特性

1.2 设计模式常用的七大原则

设计模式原则,其实就是程序员在编程时,应当遵守的原则,也是各种设计模式的基础(即:设计模式为什么这样设计的依据)

  1. 单一职责原则
  2. 接口隔离原则
  3. 依赖倒转(倒置)原则
  4. 里氏替换原则
  5. 开闭原则
  6. 迪米特法则
  7. 合成复用原则

2、单一职责原则

2.1 基本介绍

对类来说的,即一个类应该只负责一项职责。如类 A 负责两个不同职责:职责 1,职责 2。当职责 1 需求变更而改变 A 时,可能造成职责 2 执行错误,所以需要将类 A 的粒度分解为 A1,A2

2.2 例子

2.2.1 存在问题

若是用同一个方法,显然无法处理这3种交通工具,违反了单一职责,想法是把类拆分或者把方法拆分。

package com.atguigu.principle.singleresponsibility;
public class SingleResponsibility1 {
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    Vehicle vehicle = new Vehicle();
    vehicle.run("摩托车");
    vehicle.run("汽车");
    vehicle.run("飞机");
  }
}
// 交通工具类
// 方式1
// 1. 在方式1 的run方法中,违反了单一职责原则
// 2. 解决的方案非常的简单,根据交通工具运行方法不同,分解成不同类即可
class Vehicle {
  public void run(String vehicle) {
    System.out.println(vehicle + " 在公路上运行....");
  }
}

2.2.2 把类拆分

这里我们拆分成3个类,分别处理对应的交通工具

package com.atguigu.principle.singleresponsibility;
public class SingleResponsibility2 {
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    RoadVehicle roadVehicle = new RoadVehicle();
    roadVehicle.run("摩托车");
    roadVehicle.run("汽车");
    AirVehicle airVehicle = new AirVehicle();
    airVehicle.run("飞机");
  }
}
//方案2的分析
//1. 遵守单一职责原则
//2. 但是这样做的改动很大,即将类分解,同时修改客户端
//3. 改进:直接修改Vehicle 类,改动的代码会比较少=>方案3
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类

class RoadVehicle {
  public void run(String vehicle) {
    System.out.println(vehicle + "公路运行");
  }
}

airvehicle类

class AirVehicle {
  public void run(String vehicle) {
    System.out.println(vehicle + "天空运行");
  }
}

watervehicle类

class WaterVehicle {
  public void run(String vehicle) {
    System.out.println(vehicle + "水中运行");
  }
}

2.2.3 把方法拆分

package com.atguigu.principle.singleresponsibility;
public class SingleResponsibility3 {
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    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.3 单一职责原则注意事项和细节

  1. 降低类的复杂度,一个类只负责一项职责。
  2. 提高类的可读性,可维护性
  3. 降低变更引起的风险
  4. 通常情况下,我们应当遵守单一职责原则,只有逻辑足够简单,才可以在代码级违反单一职责原则;只有类中方法数量足够少,可以在方法级别保持单一职责原则

3、接口隔离原则

3.1 基本介绍

  1. 客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口上

  1. 类 A 通过接口 Interface1 依赖类 B,类 C 通过接口 Interface1 依赖类 D,如果接口 Interface1 对于类 A 和类 C来说不是最小接口,那么类 B 和类 D 必须去实现他们不需要的方法。
  2. 按隔离原则应当这样处理:将接口 Interface1 拆分为独立的几个接口(这里我们拆分成 3 个接口),类 A 和类 C 分别与他们需要的接口建立 依赖关系。也就是采用接口隔离原则
package com.atguigu.principle.segregation;
public class Segregation1 {
  public static void main(String[] args) {
    // TODO Auto-generated method stub
  }
}
//接口
interface Interface1 {
  void operation1();
  void operation2();
  void operation3();
  void operation4();
  void operation5();
}
class B implements Interface1 {
  public void operation1() {
    System.out.println("B 实现了 operation1");
  }
  public void operation2() {
    System.out.println("B 实现了 operation2");
  }
  public void operation3() {
    System.out.println("B 实现了 operation3");
  }
  public void operation4() {
    System.out.println("B 实现了 operation4");
  }
  public void operation5() {
    System.out.println("B 实现了 operation5");
  }
}
class D implements Interface1 {
  public void operation1() {
    System.out.println("D 实现了 operation1");
  }
  public void operation2() {
    System.out.println("D 实现了 operation2");
  }
  public void operation3() {
    System.out.println("D 实现了 operation3");
  }
  public void operation4() {
    System.out.println("D 实现了 operation4");
  }
  public void operation5() {
    System.out.println("D 实现了 operation5");
  }
}
class A { //A 类通过接口Interface1 依赖(使用) B类,但是只会用到1,2,3方法
  public void depend1(Interface1 i) {
    i.operation1();
  }
  public void depend2(Interface1 i) {
    i.operation2();
  }
  public void depend3(Interface1 i) {
    i.operation3();
  }
}
class C { //C 类通过接口Interface1 依赖(使用) D类,但是只会用到1,4,5方法
  public void depend1(Interface1 i) {
    i.operation1();
  }
  public void depend4(Interface1 i) {
    i.operation4();
  }
  public void depend5(Interface1 i) {
    i.operation5();
  }
}

3.2 接口隔离设计及实现

  1. 类 A 通过接口 Interface1 依赖类 B,类 C 通过接口 Interface1 依赖类 D,如果接口 Interface1 对于类 A 和类 C
    来说不是最小接口,那么类 B 和类 D 必须去实现他们不需要的方法
  2. 将接口 Interface1 拆分为独立的几个接口,类 A 和类 C 分别与他们需要的接口建立依赖关系。也就是采用接口
    隔离原则
  3. 接口 Interface1 中出现的方法,根据实际情况拆分为三个接口
package com.atguigu.principle.segregation.improve;
public class Segregation1 {
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    // 使用一把
    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();
  }
}

4、依赖倒置原则

4.1 基本介绍

依赖倒转原则(Dependence Inversion Principle)是指:

  1. 高层模块不应该依赖低层模块,二者都应该依赖其抽象
  2. 抽象不应该依赖细节,细节应该依赖抽象
  3. 依赖倒转(倒置)的中心思想是面向接口编程
  4. 依赖倒转原则是基于这样的设计理念:相对于细节的多变性,抽象的东西要稳定的多。以抽象为基础搭建的架构比以细节为基础的架构要稳定的多。在 java 中,抽象指的是接口或抽象类,细节就是具体的实现类
  5. 使用接口或抽象类的目的是制定好规范,而不涉及任何具体的操作,把展现细节的任务交给他们的实现类去完成
package com.atguigu.principle.inversion;
public class DependecyInversion {
  public static void main(String[] args) {
    Person person = new Person();
    person.receive(new Email());
  }
}
class Email {
  public String getInfo() {
    return "电子邮件信息: hello,world";
  }
}
//完成Person接收消息的功能
//方式1分析
//1. 简单,比较容易想到
//2. 如果我们获取的对象是 微信,短信等等,则新增类,同时Perons也要增加相应的接收方法
//3. 解决思路:引入一个抽象的接口IReceiver, 表示接收者, 这样Person类与接口IReceiver发生依赖
//   因为Email, WeiXin 等等属于接收的范围,他们各自实现IReceiver 接口就ok, 这样我们就符号依赖倒转原则
class Person {
  public void receive(Email email ) {
    System.out.println(email.getInfo());
  }
}

4.2 改进

package com.atguigu.principle.inversion.improve;
public class DependecyInversion {
  public static void main(String[] args) {
    //客户端无需改变
    Person person = new Person();
    person.receive(new Email());
    person.receive(new WeiXin());
  }
}
//定义接口
interface IReceiver {
  public String getInfo();
}
class Email implements IReceiver {
  public String getInfo() {
    return "电子邮件信息: hello,world";
  }
}
//增加微信
class WeiXin implements IReceiver {
  public String getInfo() {
    return "微信信息: hello,ok";
  }
}
//方式2
class Person {
  //这里我们是对接口的依赖
  public void receive(IReceiver receiver ) {
    System.out.println(receiver.getInfo());
  }
}


相关文章
|
6月前
|
设计模式 Java 测试技术
Java设计模式七大原则-接口隔离原则
Java设计模式七大原则-接口隔离原则
36 0
|
6月前
|
设计模式 Java 数据库
Java设计模式七大原则-依赖倒转原则
Java设计模式七大原则-依赖倒转原则
59 0
|
6月前
|
设计模式 Java 关系型数据库
Java设计模式七大原则-开放-关闭原则
Java设计模式七大原则-开放-关闭原则
37 0
|
1月前
|
设计模式 关系型数据库
设计模式的六大原则:理解设计模式的关键思想和应用
设计模式的六大原则:理解设计模式的关键思想和应用
18 2
|
1月前
|
设计模式
【设计模式】1、设计模式七大原则
【设计模式】1、设计模式七大原则
16 0
|
3月前
|
设计模式 存储 NoSQL
【设计模式】软件设计原则-单一职责原则
【1月更文挑战第12天】【设计模式】软件设计原则-单一职责原则
|
3月前
|
设计模式 关系型数据库
【设计模式】软件设置原则-开闭原则
【1月更文挑战第12天】【设计模式】软件设置原则-开闭原则
|
4月前
|
设计模式 Java
Java设计模式七大原则之依赖倒置原则
Java设计模式七大原则之依赖倒置原则
29 0
|
4月前
|
设计模式 Java 开发者
Java设计模式七大原则之里氏替换原则
Java设计模式七大原则之里氏替换原则
27 0
|
4月前
|
设计模式 安全 Java
Java设计模式七大原则之开闭原则
Java设计模式七大原则之开闭原则
34 0