设计模式七大原则

设计模式七大原则

单一职责原则

对一个类来说,一个类应该只负责一项职责。例如dao中的UserDao,OrderDao等等。

接口隔离原则

一个类对于另一个类的依赖应该建立在最小接口上,即 将具有多个功能的接口拆分为多个接口。

依赖倒转原则

高层模块不应该依赖于低层模块,二者都应该依赖其抽象类。
依赖倒转的中心思想是面向接口编程。使用接口或抽象类定制号规范,而不涉及具体的操作,将具体的操作交给其实现类去实现。

依赖关系的三种传递方式:

  • 接口传递

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    public class DependencyPass1 {
    public static void main(String[] args) {
    InternetTV internetTV = new InternetTV();
    OpenAndClose openAndClose = new OpenAndCloseImpl();
    openAndClose.open(internetTV);
    }
    }

    // 实体接口
    interface TV {
    public void play();
    }

    // 功能接口
    interface OpenAndClose {
    public void open(TV tv);// 接收接口
    }

    // 实现实体接口
    class InternetTV implements TV {
    @Override
    public void play() {
    System.out.println("InternetTV中的play()被调用");
    }
    }

    // 实现功能接口
    class OpenAndCloseImpl implements OpenAndClose {
    @Override
    public void open(TV tv) {
    tv.play();
    }
    }
  • 构造器传递

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    public class DependencyPass2 {
    public static void main(String[] args) {
    InternetTV internetTV = new InternetTV();
    OpenAndClose openAndClose = new OpenAndCloseImpl(internetTV);
    openAndClose.open();
    }
    }
    // 实体接口
    interface TV {
    public void play();
    }

    // 功能接口
    interface OpenAndClose {
    public void open();
    }

    // 实现实体接口
    class InternetTV implements TV {
    @Override
    public void play() {
    System.out.println("InternetTV中的play()被调用");
    }
    }

    // 实现功能接口
    // 通过构造方法接收接口
    class OpenAndCloseImpl implements OpenAndClose {
    private TV tv;

    public OpenAndCloseImpl(TV tv) {
    this.tv = tv;
    }

    @Override
    public void open() {
    tv.play();
    }
    }
  • setter方式传递

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    public class DependencyPass3 {
    public static void main(String[] args) {
    InternetTV internetTV = new InternetTV();
    OpenAndCloseImpl openAndClose = new OpenAndCloseImpl();
    openAndClose.setTv(internetTV);
    openAndClose.open();
    }
    }

    // 实体接口
    interface TV {
    public void play();
    }

    // 功能接口
    interface OpenAndClose {
    public void open();
    }

    // 实现实体接口
    class InternetTV implements TV {
    @Override
    public void play() {
    System.out.println("InternetTV中的play()被调用");
    }
    }

    // 实现功能接口
    // 通过setter方法接收接口
    class OpenAndCloseImpl implements OpenAndClose {
    private TV tv;

    public void setTv(TV tv) {
    this.tv = tv;
    }

    @Override
    public void open() {
    tv.play();
    }
    }

    里氏替换原则

让原来的子类和父类都继承一个通用的基类,原有的继承关系去掉,采用依赖,聚合,组合等关系代替。

开闭原则

对扩展开放,修改关闭。
使用抽象类提供模板,在进行拓展时,继承抽象类实现具体的方法即可。

迪米特法则

一个对象应该对其他对象保持最少的了解,用来降低程序之间的耦合度。

合成复用原则

尽量使用合成或聚合关系,少使用继承关系。

类的关系

依赖关系 Dependency

只要是在类中使用到了对方,他们之间就存在依赖关系。

A依赖于B,A作为箭头的起点,即箭头从主语开始,指向宾语。所有的关系都是这样子的!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class PersonServiceBean{
private PersonDao personDao;
public void save(Person person){}
public IDCard getIDCard(Integer personId){}
public void modify(){
Department department = new Department();
}
}

public class PersonDao{}

public class Person{}

public class IDcard{}

public class Department{}

泛化关系 Generalization

就是继承关系,它是依赖关系的特例。

实现关系 Realization

就是A类实现B类,也是依赖关系的特例

关联关系 Association

类与类之间的联系,也是依赖关系的特例。
关联具有导航性,即单向或双向。
关联具有多重性,一对一或一对多

单向一对一:

双向一对一:

聚合关系 Aggregation

表示整体和局部之间的关系,整体和局部可以分开,是关联关系的特例。

Moniter、Mouse聚合于Computer

组合关系 Composition

如果聚合关系中整体和局部是不可分开的,就升级为组合关系。