设计模式小记

来源:互联网 发布:老张博客源码 编辑:程序博客网 时间:2024/06/09 18:27

第一:设计原则:

     1)单一责任原则:应该有且仅有一个原因引起类的变更。

     2)里氏替换原则:所有引用基类的地方必须能够透明的使用其子类的对象

     3)依赖倒置原则:High level modules should not depend upon low level modules .Both should depend upon abstraction . Abstractions should not depend upon details ,details should depend upon abstractions (面向接口编程)

     4)接口隔离原则:Clients should  not  be forced to depend upon interfaces that they not use;

     5)迪米特法则(最少知识原则):一个类应该对自己要耦合或调用的类知道的最少

     6)开闭原则:Software entities like classes ,modules and functions should be open  for extension,but closed for modifications(一个软件实体如类,模块和函数应该对扩展开放,对修改关闭)

     7)the release reuse equivalency principle (REP) : the granule of reuse is the granule of release.重用的粒度是发布版本的粒度.since packages are the unit of release ,they are also the unit of reuse. therefore architechts would do well to group reuseable classes together into packages.

     8)the common closure principle(CCP) : classes that change together,belong together.把容易变化的类放在同一个包内

     9)the common reuse principle(CRP):classes that are not reused together should not be grouped together.不一起重用的类不应该放在一起

    the Package Coupling Principles:

    10)the acyclic dependencies principle (ADP):the dependencies between packages must not form cycles.

    11)the stable dependencies principle(SDP):depend in the direction of stability.

    12)

第二:设计模式:

    1)模版模式:Define the skeleton of an algorithm in an operation , deferring some steps to subclasses, Template method lets subclasses redefine certain steps of an algorithm without changing the algoritm's structure.(定义一个操作中的算法的框架,而将一些步骤延迟到子类中,使得子类可以在不改变一个算法的结构既可以重定义该算法的某些特定步骤)

    2)建造者模式(生成器模式):Separate the construction of a complex object from its representation so that the same construction process can create different representations(将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示)

    3)命令模式:命令模式是一个高内聚的模式,其定义为:Encapsulate a request as an object , thereby letting you parameter clients with different requests , queue or log requests , and support undoable operations (将一个请求封装成一个对象,从而让你使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能)

   4)责任链模式:avoid coupling the sender of a request to its receiver by giving more than one object a change to handle the request . Chain the receiving objects and pass the request along the chain until an object handles it(使多个对象都有机会处理请求,从而避免了请求的发送者和接受者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递该请求,直到有对象处理它为止)

   5)观察者模式(发布订阅模式):Define a  one- to-many dependency between objects so that when one object changes state , all its dependents are notified and updated automatically (定义对象间一种一对多的依赖关系,使得每当一个对象改变状态,则所有依赖于它的对象都会得到通知并被自动更新)

   6)备忘录模式(Memento Pattern ) without violating encapsulation,capture and externalize an object's internal state so that the object can be restored to this state later(在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后可以将该对象恢复到原先保存的状态)

0 0