面向对象设计原则

来源:互联网 发布:控制网络与现场总线 编辑:程序博客网 时间:2024/05/17 22:26

学了设计模式有一段时间了,忘记的差不多了。特写几篇博客,作为知识点回顾。不会有特别详细的内容,我目前也没那个能力。
想要系统学习设计模式,推荐刘伟老师的博客:http://blog.csdn.net/lovelion/article/details/17517213,代码是用java写的,但道理是一样的。我也是从他的课程学起的。教材的话也是他主编的一本,《设计模式 刘伟主编 清华大学出版社》。
七个面向对象设计原则,只介绍定义,用于回顾有哪些知识点。所以,不适合初学者
(英文不求能背下来,至少只给英文能认出来,搞IT免不了跟英文打交道)

  1. 单一职责原则(Single Responsibility Principle, SRP)
    定义1:一个对象应该只包含单一的职责,并且该职责被完整地封装在一个类中。(Every object should hava a single responsibility, and that responsibility shoule be entirely encapsulated by the class.)
    定义2:就一个类而言,应该仅有一个引起它变化的原因。(There should never be more than one reason for a class to change)

    类的职责包括:数据职责和行为职责。(成员和方法)

    单一职责原则是实现高内聚、低耦合的指导方针。

    反面示例。(想的起来说明你记住了,想不起来去看书或者上面的博客吧。包括下面的六个原则也是一样,最好看到名字就能想起一个反面示例,并且知道如何修改使之满足面向对象设计原则)

  2. 开闭原则(Open-Closed Principle, OCP)
    定义:一个软件实体应当对扩展开放,对修改关闭。(Software entities shoule be open for extension, but closed for modification.)

    软件实体可以指一个软件模块、一个由多个类组成的局部结构或一个独立的类。

    抽象化是开闭原则的关键。

  3. 里氏代换原则(Liskov Substitution Principle, LSP)
    定义1:如果对每一个类型为S的对象o1,都有类型为T的对象o2,使得以T定义的所有程序P在所有的对象o1都代换成o2时,程序P的行为没有变化,那么类型S是类型T的子类型。(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.)

    定义2:所有引用基类(父类)的地方必须能透明地使用其子类的对象。(Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it.)

    里氏代换原则是实现开闭原则的重要方式之一,由于使用基类对象的地方都可以使用子类对象,因此在程序中尽量使用基类类型来对对象进行定义,而在运行时再确定其子类类型,用子类对象来替换父类对象。

  4. 依赖倒置原则(Dependence Inversion Principle, DIP)
    定义1:高层模块不应该依赖底层模块,它们都应该依赖抽象。抽象不应该依赖细节,细节应该依赖于抽象。(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.)
    定义2:要针对接口编程,不要针对实现编程。(Program to an interface, not an implementation.)

    里氏代换原则是依赖倒置原则的基础。
    依赖注入:
    (1)构造注入 Constructor Injection
    (2)设值注入 Setter Injection
    (3)接口注入 Interface Injection

  5. 接口隔离原则(Interface Segregation Principle, ISP)(我认为可以理解为接口的单一职责原则)
    定义1:客户端不应该依赖那些它不需要的接口。(Clients should not be forced to depend upon interfaces that they do not use.)
    定义2:一旦一个接口太大,则需要将它分割成一些更细小的接口,使用该接口的客户端仅需知道与之相关的方法即可。(Once an interface has gotten too ‘fat’ it needs to be split into smaller and more specific interfaces so that any clients of the interface will only know about the methods that pertain to them.)

    使用多个专门的接口,而不使用单一的总接口。

  6. 合成复用原则(Composite Reuse Principle, CRP)
    定义:尽量使用对象组合,而不是继承来达到复用的目的。(Favor composition of objects over inheritance as a reuse mechanism.)

  7. 迪米特法则(Low of Demeter, LoD)
    又称为最少知识原则(Least Knowledge Principle, LKP)。
    定义1:不要和“陌生人”说话。(Don’t talk to strangers.)
    定义2:只与你的直接朋友通信。(Talk only to your immediate friends.)
    定义3:每一个软件单位对其他的单位都只有最少的知识,而且局限于那些与本单位密切相关的软件单位。(Each unit should have only limited knowledge about other units: only units ‘closely’ related to the current unit.)
    简单来说:一个软件实体应当尽可能少的与其他实体发生相互作用。

0 0