设计模式之6大基本原则

设计模式有6大基本原则:

总原则:开闭原则(Open Close Principle)
  开闭原则设计模式6大原则的核心,其具体含义为:对扩展开发,对修改关闭,正如英文文档说明的那样.当有新功能增加的时候多用继承,多写新类,少修改已有的代码,这样可以使程序的扩展性好,易于维护和升级.
相关英文文档如下:

Motivation

A clever application design and the code writing part should take care of the frequent changes that are done during the development and the maintaining phase of an application. Usually, many changes are involved when a new functionality is added to an application. Those changes in the existing code should be minimized, since it's assumed that the existing code is already unit tested and changes in already written code might affect the existing functionality in an unwanted manner.
  The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged.

Intent
  Software entities like classes, modules and functions should be open for extension but closed for modifications.
  个人理解:多用继承,和接口,扩展功能,而不修改原有功能.

参考文献:https://www.oodesign.com/open-close-principle.html


1.单一职责原则
  不要存在多于一个导致类变更的原因,也就是说每个类应该实现单一的职责,如若不然,就应该把类拆分。
  个人理解:这个是类的另外一种说法,把类当成一个个性鲜明的个体,易于分类.从另一个角度说.设计模式分类器把各种物体分成不同的类,既然可以分,肯定是具不同之处.

2.里氏替换原则(Liskov Substitution Principle)
  里氏代换原则(Liskov Substitution Principle LSP)面向对象设计的基本原则之一。 主要思想是衍生类可以完全替代基类,但是不需要修改基类的基本功能.
个人理解:用户可以继承基类,但最好不要复写重写基类的实现,因为这样就不能是使用基类的实现了.
相关英文文档介绍的比较清楚, 如下所示:

Motivation

All the time we design a program module and we create some class hierarchies. Then we extend some classes creating some derived classes. We must make sure that the new derived classes just extend without replacing the functionality of old classes. Otherwise the new classes can produce undesired effects when they are used in existing program modules.

Likov's Substitution Principle states that if a program module is using a Base class, then the reference to the Base class can be replaced with a Derived class without affecting the functionality of the program module.

Intent

Derived types must be completely substitutable for their base types.

3、依赖倒转原则(Dependence Inversion Principle)
  这个是开闭原则的基础,具体内容:面向接口编程,依赖于抽象而不依赖于具体。写代码时用到具体类时,不与具体类交互,而与具体类的上层接口交互。
  个人理解:这个是减少代码间耦合行的一个神器.面向抽象(接口)编程,而不面向对象编程,这在监听器模式中体现得非常明显.
 
4、接口隔离原则(Interface Segregation Principle)
  这个原则的意思是:每个接口中不存在子类用不到却必须实现的方法,如果不然,就要将接口拆分。使用多个隔离的接口,比使用单个接口(多个接口方法集合到一个的接口)要好。
  个人理解:使用接口,也需要分类,总结.一个接口就是一类具有某种能力的集合,实现了它就意味着拥有了它描述的全部能力,从这个角度看,它和基类是一模一样的.

5、迪米特法则(最少知道原则)(Demeter Principle)
就是说:一个类对自己依赖的类知道的越少越好。也就是说无论被依赖的类多么复杂,都应该将逻辑封装在方法的内部,通过public方法提供给外部。这样当被依赖的类变化时,才能最小的影响该类。
最少知道原则的另一个表达方式是:只与直接的朋友通信。类之间只要有耦合关系,就叫朋友关系。耦合分为依赖、关联、聚合、组合等。我们称出现为成员 变量、方法参数、方法返回值中的类为直接朋友。局部变量、临时变量则不是直接的朋友。我们要求陌生的类不要作为局部变量出现在类中。
个人理解:比如监听器模式,当用户需要监听器的时候,传"号码"过去就好了,而不需要把自己整个类给传过去.

6、合成复用原则(Composite Reuse Principle)
原则是尽量首先使用合成/聚合的方式,而不是使用继承。
个人理解:这就是isA 和Has A的问题,如果是前者,就用继承,如果是后者就用合成或者聚合.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值