zoukankan      html  css  js  c++  java
  • 设计模式学习(一) 设计原则

      

     6大设计原则:

      单一职责原则、里氏替换原则、依赖倒置原则、接口隔离原则、迪米特法则、开闭原则

    #单一职责原则:Single Responsibility Principle,简称SRP。

      There should never be more than one reason for a class to change.

      一个类,应当只有一个引起它变化的原因;即一个类应该只有一个职责。

      当一个庞大的对象有过多的职责、当用户调用某一个职责时、难免会产生冗余的代码。

      &因“职责”是不可量的、项目而异、环境而异、需按实际情况和个人经验来定。

      优点:复杂度低、可读性高、便于维护。

    #里氏替换原则:Liskov Substitution Principle,简称LSP。

      子类可以扩展父类的功能,但不能改变父类原有的功能。

      &在编译期,Java语言编译器会检查一个程序是否符合里氏替换原则。

      一.If for each object o1 of type S there is an object o2 of type T such that for all programsP defined in terms of S,the behavior of P is unchanged when o1 is substituted for o2then T is a subtype of S.

      二.Functions that use pointers or references to base classes must be able to use objects ofderived classes without knowing it.

    #依赖倒置原则:Dependence Inversion Principle,简称DIP。

      High level modules should not depend upon low level modules. Both should dependupon abstractions. Abstractions should not depend upon details. Details should dependupon abstractions.

      高层模块不应该依赖低层模块,两者都应该依赖其抽象;抽象不应该依赖细节,细节应该依赖抽象。

      &精确的定义就是“面向接口编程”

    #接口隔离原则:Interface Segregation Principle,简称ISP.

       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 possibleinterface 类间的依赖关系应该建立在最小的接口上

      不依赖不需要的接口。接口尽量小,且要有限度。

    #迪米特法则:Law of Demeter,简称LoD。

      只与你直接的朋友们通信(Only talk to your immediate friends);
      不要跟“陌生人”说话(Don't talk to strangers);

    #开闭原则:Open-Closed Principle,简称OCP。

      Software entities should be open for extension,but closed for modification.一个软件实体应当对扩展开放,对修改关闭。

    @

    -------博客内容仅用于个人学习总结-------
  • 相关阅读:
    马虎的算式
    jquery中的toggle与slideToggle的区别
    Eclipse下配置C/C++开发环境
    A ResourcePool could not acquire a resource from its primary factory or source
    hadoop mapreduce数据排序
    排序算法之选择排序
    Poj1816(Trie+DFS)
    [置顶] Android4.0 Launcher源码研究
    java第十五天_Map集合,
    VirtualBox下安装ubuntu server 16.04
  • 原文地址:https://www.cnblogs.com/DarGi2019/p/12834745.html
Copyright © 2011-2022 走看看