设计模式(二)——六大基本原则

设计模式(二)——六大基本原则

单一职责原则

SRP(Single Responsibility Principle)
there should never be more than one reason for a class to change
应该有且仅有一个原因引起类的变更

接口一定要做到单一职责

里氏替换原则

LSP(Liskov Substitution Principle)
If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T.
如果对每一个类型为S的对象o1,都有类型为T的对象o2,使得以T定义的所有程序P在所有的对象o1都代换成o2时,程序P的行为没有发生变换,那么类型S是类型T的子类型。
Functions that use pointers oor references to base classes must be able to use objects of derived classes without knowing it.
所有引用基类的地方必须能透明地使用其子类的对象。

  1. 子类的方法可以使得入参被放大。即这个方法可以输入原参数的基类,表示方法的通用性被增强
  2. 子类的方法可以返回原返回值的子类,一般原返回值的子类有更多的功能,表示方法的功能性被增强

依赖倒置原则

DIP(Dependence Inversion Principle)
High level modules should not depend upon low level modules. Both should depend upon abstractions. Abstractions should not depend upon details. Details should depend upon abstractions.

  • 高层模块不应该依赖底层模块,两者都应该依赖其抽象
  • 抽象不应该依赖细节
  • 细节应该依赖抽象

在java语言中的描述是:

  • 模块间的抽象通过抽象发生,实现类之间不发生直接的依赖关系,其依赖关系是通过接口或抽象类产生的
  • 接口或抽象类不依赖实现类
  • 实现类依赖接口或抽象

接口隔离原则

Clients should not be forced to depend upon interfaces that they don’t use.
客户端不应该依赖它不需要的接口
The dependency of one class to another one should depend on the smallest possible interface.
类间的依赖关系应该建立在最小的接口上

迪米特法则

LoD(Law of Demeter)
一个对象应该对其他对象有最少的了解。

开闭原则

Software entities like classes, modules and functions should be open for extension but closed for modifications.
对扩展开发,对修改关闭

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值