软件设计的原则&101个设计模式-2011年04月25日 陈皓 101个设计模式 101 Design Patterns & Tips for Developers

软件设计的原则&101个设计模式-2011年04月25日  陈皓
101个设计模式
101 Design Patterns & Tips for Developers

以前本站向大家介绍过一些软件开发的原则,比如优质代码的十诫Unix传奇(下篇)中所以说的UNIX的设计原则。相信大家从中能够从中学了解到一些设计原理方面的知识,正如我在《再谈“我是怎么招聘程序”》中所说的,一个好的程序员通常由其操作技能、知识水平,经验层力和能力四个方面组成。在这里想和大家说说设计中的一些原则,我认为这些东西属于长期经验总结出来的知识。这些原则,每一个程序员都应该了解。但是请不要教条主义,在使用的时候还是要多多考虑实际情况。其实,下面这些原则,不单单只是软件开发,可以推广到其它生产活动中,甚至我们的生活中

目录

Don’t Repeat Yourself (DRY)

DRY 是一个最简单的法则,也是最容易被理解的。但它也可能是最难被应用的(因为要做到这样,我们需要在泛型设计上做相当的努力,这并不是一件容易的事)。它意味着,当我们在两个或多个地方的时候发现一些相似的代码的时候,我们需要把他们的共性抽象出来形一个唯一的新方法,并且改变现有的地方的代码让他们以一些合适的参数调用这个新的方法。

参考http://en.wikipedia.org/wiki/Don%27t_repeat_yourself

Keep It Simple, Stupid (KISS)

KISS原则在设计上可能最被推崇的,在家装设计,界面设计 ,操作设计上,复杂的东西越来越被众人所BS了,而简单的东西越来越被人所认可,比如这些UI的设计和我们中国网页(尤其是新浪的网页)者是负面的例子。“宜家”(IKEA)简约、效率的家居设计、生产思路;“微软”(Microsoft)“所见即所得”的理念;“谷歌”(Google)简约、直接的商业风格,无一例外的遵循了“kiss”原则,也正是“kiss”原则,成就了这些看似神奇的商业经典。而苹果公司的iPhone/iPad将这个原则实践到了极至。

把一个事情搞复杂是一件简单的事,但要把一个复杂的事变简单,这是一件复杂的事。

参考http://en.wikipedia.org/wiki/KISS_principle

Program to an interface, not an implementation

这是设计模式中最根本的哲学,注重接口,而不是实现,依赖接口,而不是实现。接口是抽象是稳定的,实现则是多种多样的。以后面我们会面向对象的SOLID原则中会提到我们的依赖倒置原则,就是这个原则的的另一种样子。还有一条原则叫 Composition over inheritance(喜欢组合而不是继承),这两条是那23个经典设计模式中的设计原则。

Command-Query Separation (CQS)  – 命令-查询分离原则

  • 查询:当一个方法返回一个值来回应一个问题的时候,它就具有查询的性质;
  • 命令:当一个方法要改变对象的状态的时候,它就具有命令的性质;

通常,一个方法可能是纯的Command模式或者是纯的Query模式,或者是两者的混合体。在设计接口时,如果可能,应该尽量使接口单一化,保证方法的行为严格的是命令或者是查询,这样查询方法不会改变对象的状态,没有副作用,而会改变对象的状态的方法不可能有返回值。也就是说:如果我们要问一个问题,那么就不应该影响到它的答案。实际应用,要视具体情况而定,语义的清晰性和使用的简单性之间需要权衡。将Command和Query功能合并入一个方法,方便了客户的使用,但是,降低了清晰性,而且,可能不便于基于断言的程序设计并且需要一个变量来保存查询结果。

在系统设计中,很多系统也是以这样原则设计的,查询的功能和命令功能的系统分离,这样有则于系统性能,也有利于系统的安全性。

参考http://en.wikipedia.org/wiki/Command-query_separation

You Ain’t Gonna Need It (YAGNI)

这个原则简而言之为——只考虑和设计必须的功能,避免过度设计。只实现目前需要的功能,在以后您需要更多功能时,可以再进行添加。

  • 如无必要,勿增复杂性。
  • 软件开发先是一场沟通博弈。

以前本站有一篇关于过度重构的文章,这个示例就是这个原则的反例。而,WebSphere的设计者就表示过他过度设计了这个产品。我们的程序员或是架构师在设计系统的时候,会考虑很多扩展性的东西,导致在架构与设计方面使用了大量折衷,最后导致项目失败。这是个令人感到讽刺的教训,因为本来希望尽可能延长项目的生命周期,结果反而缩短了生命周期。

参考http://en.wikipedia.org/wiki/You_Ain%27t_Gonna_Need_It

Law of Demeter – 迪米特法则

迪米特法则(Law of Demeter),又称“最少知识原则”(Principle of Least Knowledge),其来源于1987年荷兰大学的一个叫做Demeter的项目。Craig Larman把Law of Demeter又称作“不要和陌生人说话”。在《程序员修炼之道》中讲LoD的那一章叫作“解耦合与迪米特法则”。关于迪米特法则有一些很形象的比喻:

  • 如果你想让你的狗跑的话,你会对狗狗说还是对四条狗腿说?
  • 如果你去店里买东西,你会把钱交给店员,还是会把钱包交给店员让他自己拿?

和狗的四肢说话?让店员自己从钱包里拿钱?这听起来有点荒唐,不过在我们的代码里这几乎是见怪不怪的事情了。

对于LoD,正式的表述如下:

对于对象 ‘O’ 中一个方法’M’,M应该只能够访问以下对象中的方法:

  1. 对象O;
  2. 与O直接相关的Component Object;
  3. 由方法M创建或者实例化的对象;
  4. 作为方法M的参数的对象。

在《Clean Code》一书中,有一段Apache framework中的一段违反了LoD的代码:

final String outputDir = ctxt.getOptions().getScratchDir().getAbsolutePath();

这么长的一串对其它对象的细节,以及细节的细节,细节的细节的细节……的调用,增加了耦合,使得代码结构复杂、僵化,难以扩展和维护。

在《重构》一书中的代码的环味道中有一种叫做“Feature Envy”(依恋情结),形象的描述了一种违反了LoC的情况。Feature Envy就是说一个对象对其它对象的内容更有兴趣,也就是说老是羡慕别的对象的成员、结构或者功能,大老远的调用人家的东西。这样的结构显然是不合理的。我们的程序应该写得比较“害羞”。不能像前面例子中的那个不把自己当外人的店员一样,拿过客人的钱包自己把钱拿出来。“害羞”的程序只和自己最近的朋友交谈。这种情况下应该调整程序的结构,让那个对象自己拥有它羡慕的feature,或者使用合理的设计模式(例如Facade和Mediator)。

参考http://en.wikipedia.org/wiki/Principle_of_Least_Knowledge

面向对象的S.O.L.I.D 原则

一般来说这是面向对象的五大设计原则,但是,我觉得这些原则可适用于所有的软件开发。

Single Responsibility Principle (SRP) – 职责单一原则

关于单一职责原则,其核心的思想是:一个类,只做一件事,并把这件事做好,其只有一个引起它变化的原因。单一职责原则可以看作是低耦合、高内聚在面向对象原则上的引申,将职责定义为引起变化的原因,以提高内聚性来减少引起变化的原因。职责过多,可能引起它变化的原因就越多,这将导致职责依赖,相互之间就产生影响,从而极大的损伤其内聚性和耦合度。单一职责,通常意味着单一的功能,因此不要为一个模块实现过多的功能点,以保证实体只有一个引起它变化的原因。

  • Unix/Linux是这一原则的完美体现者。各个程序都独立负责一个单一的事。
  • Windows是这一原则的反面示例。几乎所有的程序都交织耦合在一起。

Open/Closed Principle (OCP) – 开闭原则

关于开发封闭原则,其核心的思想是:模块是可扩展的,而不可修改的。也就是说,对扩展是开放的,而对修改是封闭的

  • 对扩展开放,意味着有新的需求或变化时,可以对现有代码进行扩展,以适应新的情况。
  • 对修改封闭,意味着类一旦设计完成,就可以独立完成其工作,而不要对类进行任何修改。

对于面向对象来说,需要你依赖抽象,而不是实现,23个经典设计模式中的“策略模式”就是这个实现。对于非面向对象编程,一些API需要你传入一个你可以扩展的函数,比如我们的C 语言的qsort()允许你提供一个“比较器”,STL中的容器类的内存分配,ACE中的多线程的各种锁。对于软件方面,浏览器的各种插件属于这个原则的实践。

Liskov substitution principle (LSP) – 里氏代换原则

软件工程大师Robert C. Martin把里氏代换原则最终简化为一句话:“Subtypes must be substitutable for their base types”。也就是,子类必须能够替换成它们的基类。即:子类应该可以替换任何基类能够出现的地方,并且经过替换以后,代码还能正常工作。另外,不应该在代码中出现if/else之类对子类类型进行判断的条件。里氏替换原则LSP是使代码符合开闭原则的一个重要保证。正是由于子类型的可替换性才使得父类型的模块在无需修改的情况下就可以扩展。

这么说来,似乎有点教条化,我非常建议大家看看这个原则个两个最经典的案例——“正方形不是长方形”和“鸵鸟不是鸟”。通过这两个案例,你会明白《墨子 小取》中说的 ——“娣,美人也,爱娣,非爱美人也….盗,人也;恶盗,非恶人也。”——妹妹虽然是美人,但喜欢妹妹并不代表喜欢美人。盗贼是人,但讨厌盗贼也并不代表就讨厌人类。这个原则让你考虑的不是语义上对象的间的关系,而是实际需求的环境

在很多情况下,在设计初期我们类之间的关系不是很明确,LSP则给了我们一个判断和设计类之间关系的基准:需不需要继承,以及怎样设计继承关系。

Interface Segregation Principle (ISP) – 接口隔离原则

接口隔离原则意思是把功能实现在接口中,而不是类中,使用多个专门的接口比使用单一的总接口要好。

举个例子,我们对电脑有不同的使用方式,比如:写作,通讯,看电影,打游戏,上网,编程,计算,数据等,如果我们把这些功能都声明在电脑的抽类里面,那么,我们的上网本,PC机,服务器,笔记本的实现类都要实现所有的这些接口,这就显得太复杂了。所以,我们可以把其这些功能接口隔离开来,比如:工作学习接口,编程开发接口,上网娱乐接口,计算和数据服务接口,这样,我们的不同功能的电脑就可以有所选择地继承这些接口。

这个原则可以提升我们“搭积木式”的软件开发。对于设计来说,Java中的各种Event Listener和Adapter,对于软件开发来说,不同的用户权限有不同的功能,不同的版本有不同的功能,都是这个原则的应用。

Dependency Inversion Principle (DIP) – 依赖倒置原则

高层模块不应该依赖于低层模块的实现,而是依赖于高层抽象。

举个例子,墙面的开关不应该依赖于电灯的开关实现,而是应该依赖于一个抽象的开关的标准接口,这样,当我们扩展程序的时候,我们的开关同样可以控制其它不同的灯,甚至不同的电器。也就是说,电灯和其它电器继承并实现我们的标准开关接口,而我们的开关产商就可不需要关于其要控制什么样的设备,只需要关心那个标准的开关标准。这就是依赖倒置原则。

这就好像浏览器并不依赖于后面的web服务器,其只依赖于HTTP协议。这个原则实在是太重要了,社会的分工化,标准化都是这个设计原则的体现。

参考http://en.wikipedia.org/wiki/Solid_(object-oriented_design)

Common Closure Principle(CCP)– 共同封闭原则

一个包中所有的类应该对同一种类型的变化关闭。一个变化影响一个包,便影响了包中所有的类。一个更简短的说法是:一起修改的类,应该组合在一起(同一个包里)。如果必须修改应用程序里的代码,我们希望所有的修改都发生在一个包里(修改关闭),而不是遍布在很多包里。CCP原则就是把因为某个同样的原因而需要修改的所有类组合进一个包里。如果2个类从物理上或者从概念上联系得非常紧密,它们通常一起发生改变,那么它们应该属于同一个包。

CCP延伸了开闭原则(OCP)的“关闭”概念,当因为某个原因需要修改时,把需要修改的范围限制在一个最小范围内的包里。

参考http://c2.com/cgi/wiki?CommonClosurePrinciple

Common Reuse Principle (CRP) – 共同重用原则

包的所有类被一起重用。如果你重用了其中的一个类,就重用全部。换个说法是,没有被一起重用的类不应该被组合在一起。CRP原则帮助我们决定哪些类应该被放到同一个包里。依赖一个包就是依赖这个包所包含的一切。当一个包发生了改变,并发布新的版本,使用这个包的所有用户都必须在新的包环境下验证他们的工作,即使被他们使用的部分没有发生任何改变。因为如果包中包含有未被使用的类,即使用户不关心该类是否改变,但用户还是不得不升级该包并对原来的功能加以重新测试。

CCP则让系统的维护者受益。CCP让包尽可能大(CCP原则加入功能相关的类),CRP则让包尽可能小(CRP原则剔除不使用的类)。它们的出发点不一样,但不相互冲突。

参考http://c2.com/cgi/wiki?CommonReusePrinciple

Hollywood Principle – 好莱坞原则

好莱坞原则就是一句话——“don’t call us, we’ll call you.”。意思是,好莱坞的经纪人们不希望你去联系他们,而是他们会在需要的时候来联系你。也就是说,所有的组件都是被动的,所有的组件初始化和调用都由容器负责。组件处在一个容器当中,由容器负责管理。

简单的来讲,就是由容器控制程序之间的关系,而非传统实现中,由程序代码直接操控。这也就是所谓“控制反转”的概念所在:

  1. 不创建对象,而是描述创建对象的方式。
  2. 在代码中,对象与服务没有直接联系,而是容器负责将这些联系在一起。

控制权由应用代码中转到了外部容器,控制权的转移,是所谓反转。

好莱坞原则就是IoC(Inversion of Control)或DI(Dependency Injection )的基础原则。这个原则很像依赖倒置原则,依赖接口,而不是实例,但是这个原则要解决的是怎么把这个实例传入调用类中?你可能把其声明成成员,你可以通过构造函数,你可以通过函数参数。但是 IoC可以让你通过配置文件,一个由Service Container 读取的配置文件来产生实际配置的类。但是程序也有可能变得不易读了,程序的性能也有可能还会下降。

参考

High Cohesion & Low/Loose coupling & – 高内聚, 低耦合

这个原则是UNIX操作系统设计的经典原则,把模块间的耦合降到最低,而努力让一个模块做到精益求精。

  • 内聚:一个模块内各个元素彼此结合的紧密程度
  • 耦合:一个软件结构内不同模块之间互连程度的度量

内聚意味着重用和独立,耦合意味着多米诺效应牵一发动全身。

参考

Convention over Configuration(CoC)– 惯例优于配置原则

简单点说,就是将一些公认的配置方式和信息作为内部缺省的规则来使用。例如,Hibernate的映射文件,如果约定字段名和类属性一致的话,基本上就可以不要这个配置文件了。你的应用只需要指定不convention的信息即可,从而减少了大量convention而又不得不花时间和精力啰里啰嗦的东东。配置文件很多时候相当的影响开发效率。

Rails 中很少有配置文件(但不是没有,数据库连接就是一个配置文件),Rails 的fans号称期开发效率是 java 开发的 10 倍,估计就是这个原因。Maven也使用了CoC原则,当你执行mvn -compile命令的时候,不需要指源文件放在什么地方,而编译以后的class文件放置在什么地方也没有指定,这就是CoC原则。

参考http://en.wikipedia.org/wiki/Convention_over_Configuration

Separation of Concerns (SoC) – 关注点分离

SoC 是计算机科学中最重要的努力目标之一。这个原则,就是在软件开发中,通过各种手段,将问题的各个关注点分开。如果一个问题能分解为独立且较小的问题,就是相对较易解决的。问题太过于复杂,要解决问题需要关注的点太多,而程序员的能力是有限的,不能同时关注于问题的各个方面。正如程序员的记忆力相对于计算机知识来说那么有限一样,程序员解决问题的能力相对于要解决的问题的复杂性也是一样的非常有限。在我们分析问题的时候,如果我们把所有的东西混在一起讨论,那么就只会有一个结果——乱。

我记得在上一家公司有一个项目,讨论就讨论了1年多,项目本来不复杂,但是没有使用SoC,全部的东西混为一谈,再加上一堆程序员注入了各种不同的观点和想法,整个项目一下子就失控了。最后,本来一个1年的项目做了3年。

实现关注点分离的方法主要有两种,一种是标准化,另一种是抽象与包装。标准化就是制定一套标准,让使用者都遵守它,将人们的行为统一起来,这样使用标准的人就不用担心别人会有很多种不同的实现,使自己的程序不能和别人的配合。Java EE就是一个标准的大集合。每个开发者只需要关注于标准本身和他所在做的事情就行了。就像是开发镙丝钉的人只专注于开发镙丝钉就行了,而不用关注镙帽是怎么生产的,反正镙帽和镙丝钉按标来就一定能合得上。不断地把程序的某些部分抽像差包装起来,也是实现关注点分离的好方法。一旦一个函数被抽像出来并实现了,那么使用函数的人就不用关心这个函数是如何实现的,同样的,一旦一个类被抽像并实现了,类的使用者也不用再关注于这个类的内部是如何实现的。诸如组件,分层,面向服务,等等这些概念都是在不同的层次上做抽像和包装,以使得使用者不用关心它的内部实现细节。

说白了还是“高内聚,低耦合”。

参考http://sulong.me/archives/99

Design by Contract (DbC) – 契约式设计

DbC的核心思想是对软件系统中的元素之间相互合作以及“责任”与“义务”的比喻。这种比喻从商业活动中“客户”与“供应商”达成“契约”而得来。例如:

  • 供应商必须提供某种产品(责任),并且他有权期望客户已经付款(权利)。
  • 客户必须付款(责任),并且有权得到产品(权利)。
  • 契约双方必须履行那些对所有契约都有效的责任,如法律和规定等。

同样的,如果在程序设计中一个模块提供了某种功能,那么它要:

  • 期望所有调用它的客户模块都保证一定的进入条件:这就是模块的先验条件(客户的义务和供应商的权利,这样它就不用去处理不满足先验条件的情况)。
  • 保证退出时给出特定的属性:这就是模块的后验条件——(供应商的义务,显然也是客户的权利)。
  • 在进入时假定,并在退出时保持一些特定的属性:不变式。

契约就是这些权利和义务的正式形式。我们可以用“三个问题”来总结DbC,并且作为设计者要经常问:

  • 它期望的是什么?
  • 它要保证的是什么?
  • 它要保持的是什么?

根据Bertrand Meyer氏提出的DBC概念的描述,对于类的一个方法,都有一个前提条件以及一个后续条件,前提条件说明方法接受什么样的参数数据等,只有前提条件得到满足时,这个方法才能被调用;同时后续条件用来说明这个方法完成时的状态,如果一个方法的执行会导致这个方法的后续条件不成立,那么这个方法也不应该正常返回。

现在把前提条件以及后续条件应用到继承子类中,子类方法应该满足:

  1. 前提条件不强于基类.
  2. 后续条件不弱于基类.

换句话说,通过基类的接口调用一个对象时,用户只知道基类前提条件以及后续条件。因此继承类不得要求用户提供比基类方法要求的更强的前提条件,亦即,继承类方法必须接受任何基类方法能接受的任何条件(参数)。同样,继承类必须顺从基类的所有后续条件,亦即,继承类方法的行为和输出不得违反由基类建立起来的任何约束,不能让用户对继承类方法的输出感到困惑。

这样,我们就有了基于契约的LSP,基于契约的LSP是LSP的一种强化。

参考http://en.wikipedia.org/wiki/Design_by_contract

Acyclic Dependencies Principle (ADP) – 无环依赖原则

包之间的依赖结构必须是一个直接的无环图形,也就是说,在依赖结构中不允许出现环(循环依赖)。如果包的依赖形成了环状结构,怎么样打破这种循环依赖呢?有2种方法可以打破这种循环依赖关系:第一种方法是创建新的包,如果A、B、C形成环路依赖,那么把这些共同类抽出来放在一个新的包D里。这样就把C依赖A变成了C依赖D以及A依赖D,从而打破了循环依赖关系。第二种方法是使用DIP(依赖倒置原则)和ISP(接口分隔原则)设计原则。

无环依赖原则(ADP)为我们解决包之间的关系耦合问题。在设计模块时,不能有循环依赖。

参考http://c2.com/cgi/wiki?AcyclicDependenciesPrinciple

————————————————————————————

上面这些原则可能有些学院派,也可能太为理论,我在这里说的也比较模糊和简单,这里只是给大家一个概貌,如果想要了解更多的东西,大家可以多google一下。

不过这些原则看上去都不难,但是要用好却并不那么容易。要能把这些原则用得好用得精,而不教条,我的经验如下:(我以为这是一个理论到应用的过程)

  1. 你可以先粗浅或是表面地知道这些原则。
  2. 但不要急着马上就使用。
  3. 在工作学习中观察和总结别人或自己的设计。
  4. 再回过头来了回顾一下这些原则,相信你会有一些自己的心得。
  5. 有适度地去实践一下。
  6. Goto第 3步。

所以设计模式,实是是一种方法,一种为了解决某种或某类物定问题所使用的设计模型。据说,在编程语言方面有100多种设计模式,而在现实生活中,传说有上成千上万个模式,比如写书有写书的设计模式,写武侠的一种,言情的另一种,连官方的新闻稿件也有。

 言归正传,这个站点(http://sourcemaking.com/design-patterns-and-tips)是向大家着力推荐的讲解编程方面设计模式的网站,除了GoF那经典的23个三大类的设计模式,还有N多的其它种类的设计模式。一共101个,最重要的是,它的这101个设计模式的写作模式如下:

  1. 模式的意图
  2. 要解决什么样的问题
  3. 模式的讨论
  4. 模式的结构
  5. 模式的业务示例
  6. 实现模式的Checklist
  7. 模式的规则
  8. 代码示例(包括各种语言,如:Java, C++, PHP, Delphi…)

101 Design Patterns & Tips for Developers

 
Creational Patterns
1.
Abstract Factory
The purpose of the Abstract Factory is to provide an interface for creating families of related objects, without specifying concrete classes (more...)
2.
Builder
The Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations (more...)
软件设计的原则&101个设计模式-2011年04月25日  陈皓
101个设计模式
101 Design Patterns & Tips for Developers
3.
Factory Method
The Factory Method defines an interface for creating objects, but lets subclasses decide which classes to instantiate (more...)
4.
Object Pool
Object pools are used to manage the object caching and it can offer a significant performance boost (more...)
5.
Prototype
The Prototype pattern specifies the kind of objects to create using a prototypical instance. Prototypes of new products are often built prior to full production, but in this example, the prototype is passive and does not participate in copying itself (more...)
6.
Singleton
The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance (more...)
Structural Patterns
7.
Adapter
The Adapter pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients (more...)
软件设计的原则&101个设计模式-2011年04月25日  陈皓
101个设计模式
101 Design Patterns & Tips for Developers
8.
Bridge
The Bridge pattern decouples an abstraction from its implementation, so that the two can vary independently (more...)
9.
Composite
The Composite composes objects into tree structures and lets clients treat individual objects and compositions uniformly (more...)
10.
Decorator
The Decorator attaches additional responsibilities to an object dynamically. Decorator pattern suggests giving the client the ability to specify whatever combination of "features" is desired (more...)
11.
Facade
The Facade defines a unified, higher level interface to a subsystem that makes it easier to use (more...)
软件设计的原则&101个设计模式-2011年04月25日  陈皓
101个设计模式
101 Design Patterns & Tips for Developers
12.
Flyweight
The Flyweight pattern describes how to share objects to allow their use at fine granularities without prohibitive cost (more...)
13.
Private Class Data
The private class data design pattern seeks to reduce exposure of attributes by limiting their visibility (more...)
14.
Proxy
The Proxy provides a surrogate or place holder to provide access to an object (more...)
Behavioral Patterns
15.
Chain of Responsibility
Chain of Responsibility chains the receiving objects together, and then passes any request messages from object to object until it reaches an object capable of handling the message (more...)
16.
Command
The Command pattern allows requests to be encapsulated as objects, thereby allowing clients to be parameterized with different requests (more...)
软件设计的原则&101个设计模式-2011年04月25日  陈皓
101个设计模式
101 Design Patterns & Tips for Developers
17.
Interpreter
The Intepreter pattern defines a grammatical representation for a language and an interpreter to interpret the grammar (more...)
18.
Iterator
The Iterator provides ways to access elements of an aggregate object sequentially without exposing the underlying structure of the object. (more...)
19.
Mediator
The Mediator defines an object that controls how a set of objects interact. Loose coupling between colleague objects is achieved by having colleagues communicate with the Mediator, rather than with each other (more...)
20.
Memento
The Memento captures and externalizes an object's internal state so that the object can later be restored to that state (more...)
21.
Null Object
The Null Object encapsulates the absence of an object by providing a substitutable alternative that offers suitable default "do nothing" behavior (more...)
22.
Observer
The Observer defines a one-to-many relationship so that when one object changes state, the others are notified and updated automatically (more...)
软件设计的原则&101个设计模式-2011年04月25日  陈皓
101个设计模式
101 Design Patterns & Tips for Developers
23.
State
The State pattern allows an object to change its behavior when its internal state changes (more...)
24.
Strategy
The Strategy lets the algorithm vary independently from the clients that use it. It defines a family of algorithms, encapsulates each one, and makes them interchangeable (more...)
25.
Template Method
The Template Method defines a skeleton of an algorithm in an operation, and defers some steps to subclasses (more...)
26.
Visitor
The Visitor pattern represents an operation to be performed on the elements of an object structure without changing the classes on which it operates (more...)
Composing Methods of Refactoring
27.
Extract Method
If you have a code fragment that can be grouped together, turn the fragment into a method whose name explains the purpose of the method (more...)
28.
Inline Method
If a method's body is just as clear as its name, put the method's body into the body of its callers and remove the method (more...)
29.
Inline Temp
If you have a temp that is assigned to once with a simple expression, and the temp is getting in the way of other refactorings, replace all references to that temp with the expression (more...)
软件设计的原则&101个设计模式-2011年04月25日  陈皓
101个设计模式
101 Design Patterns & Tips for Developers
30.
Introduce Explaining Variable
If you have a complicated expression, put the result of the expression, or parts of the expression, in a temporary variable with a name that explains the purpose (more...)
31.
Remove Assignments to Parameters
If the code assigns to a parameter, use a temporary variable instead (more...)
32.
Replace Method with Method Object
If uou have a long method that uses local variables in such a way that you cannot apply Extract Method, turn the method into its own object so that all the local variables become fields on that object (more...)
33.
Replace Temp with Query
If you are using a temporary variable to hold the result of an expression, extract the expression into a method. Replace all references to the temp with the expression. The new method can then be used in other methods (more...)
34.
Split Temporary Variable
If you have a temporary variable assigned to more than once, but is not a loop variable nor a collecting temporary variable, make a separate temporary variable for each assignment (more...)
34.
Substitute Algorithm
If you want to replace an algorithm with one that is clearer, replace the body of the method with the new algorithm (more...)
Moving Features Between Objects
35.
Extract Class
If you have one class doing work that should be done by two, create a new class and move the relevant fields and methods from the old class into the new class (more...)
36.
Hide Delegate
If a client is calling a delegate class of an object, create methods on the server to hide the delegate (more...)
软件设计的原则&101个设计模式-2011年04月25日  陈皓
101个设计模式
101 Design Patterns & Tips for Developers
37.
Inline Class
If a class isn't doing very much, move all its features into another class and delete it (more...)
38.
Introduce Foreign Method
If a server class you are using needs an additional method, but you can't modify the class, create a method in the client class with an instance of the server class as its first argument (more...)
39.
Introduce Local Extension
If a server class you are using needs several additional methods, but you can't modify the class, create a new class that contains these extra methods. Make this extension class a subclass or a wrapper of the original (more...)
40.
Move Field
If a field is, or will be, used by another class more than the class on which it is defined, create a new field in the target class, and change all its users (more...)
41.
Move Method
If a method is, or will be, using or used by more features of another class than the class on which it is defined, create a new method with a similar body in the class it uses most. Either turn the old method into a simple delegation, or remove it altogether (more...)
42.
Remove Middle Man
If a class is doing too much simple delegation, get the client to call the delegate directly (more...)
Organizing Data
43.
Change Bidirectional Association to Unidirectional
If you have a two-way association but one class no longer needs features from the other, drop the unneeded end of the association (more...)
44.
Change Reference to Value
If you have a reference object that is small, immutable, and awkward to manage, turn it into a value object (more...)
软件设计的原则&101个设计模式-2011年04月25日  陈皓
101个设计模式
101 Design Patterns & Tips for Developers
45.
Change Unidirectional Association to Bidirectional
If you have two classes that need to use each other's features, but there is only a one-way link, add back pointers, and change modifiers to update both sets (more...)
46.
Change Value to Reference
If you have a class with many equal instances that you want to replace with a single object, turn the object into a reference object (more...)
47.
Duplicate Observed Data
If you have domain data available only in a GUI control, and domain methods need access, copy the data to a domain object. Set up an observer to synchronize the two pieces of data (more...)
48.
Encapsulate Collection
If a method returns a collection, make it return a read-only view and provide add/remove methods (more...)
49.
Encapsulate Field
If there is a public field, make it private and provide accessors (more...)
50.
Replace Array with Object
If you have an array in which certain elements mean different things, replace the array with an object that has a field for each element (more...)
软件设计的原则&101个设计模式-2011年04月25日  陈皓
101个设计模式
101 Design Patterns & Tips for Developers
51.
Replace Data Value with Object
If you have a data item that needs additional data or behavior, turn the data item into an object (more...)
52.
Replace Magic Number with Symbolic Constant
If you have a literal number with a particular meaning, create a constant, name it after the meaning, and replace the number with it (more...)
53.
Replace Record with Data Class
If you need to interface with a record structure in a traditional programming environment, make a dumb data object for the record (more...)
54.
Replace Subclass with Fields
If you have subclasses that vary only in methods that return constant data, change the methods to superclass fields and eliminate the subclasses (more...)
55.
Replace Type Code with Class
If a class has a numeric type code that does not affect its behavior, replace the number with a new class (more...)
56.
Replace Type Code with State/Strategy
If you have a type code that affects the behavior of a class, but you cannot use subclassing, replace the type code with a state object (more...)
57.
Replace Type Code with Subclasses
If you have an immutable type code that affects the behavior of a class, replace the type code with subclasses (more...)
58.
Introduce Explaining Variable
If you have a complicated expression, put the result of the expression, or parts of the expression, in a temporary variable with a name that explains the purpose (more...)
59.
Remove Assignments to Parameters
If the code assigns to a parameter, use a temporary variable instead (more...)
60.
Self Encapsulate Field
If you are accessing a field directly, but the coupling to the field is becoming awkward, create getting and setting methods for the field and use only those to access the field (more...)
Simplifying Conditional Expressions
61.
Consolidate Conditional Expression
If you have a sequence of conditional tests with the same result, combine them into a single conditional expression and extract it (more...)
62.
Consolidate Duplicate Conditional Fragments
If the same fragment of code is in all branches of a conditional expression, move it outside of the expression (more...)
软件设计的原则&101个设计模式-2011年04月25日  陈皓
101个设计模式
101 Design Patterns & Tips for Developers
63.
Decompose Conditional
If you have a complicated conditional (if-then-else) statement, extract methods from the condition, then part, and else parts (more...)
64.
Introduce Assertion
If a section of code assumes something about the state of the program, make the assumption explicit with an assertion (more...)
65.
Introduce Null Object
If you have repeated checks for a null value, replace the null value with a null object (more...)
66.
Remove Control Flag
If you have a variable that is acting as a control flag for a series of boolean expressions, use a break or return instead (more...)
67.
Replace Conditional with Polymorphism
If you have a conditional that chooses different behavior depending on the type of an object, move each leg of the conditional to an overriding method in a subclass. Make the original method abstract (more...)
68.
Replace Nested Conditional with Guard Clauses
If a method has conditional behavior that does not make clear the normal path of execution, use guard clauses for all the special cases (more...)
Making Method Calls Simpler
69.
Add Parameter
If a method needs more information from its caller, add a parameter for an object that can pass on this information (more...)
70.
Encapsulate Downcast
If a method returns an object that needs to be downcasted by its callers, move the downcast to within the method (more...)
71.
Hide Method
If a method is not used by any other class, make the method private (more...)
72.
Introduce Parameter Object
If you have a group of parameters that naturally go together, replace them with an object (more...)
73.
Hide Delegate
If a client is calling a delegate class of an object, create methods on the server to hide the delegate (more...)
74.
Parameterize Method
If several methods do similar things but with different values contained in the method body, create one method that uses a parameter for the different values (more...)
75.
Preserve Whole Object
If you are getting several values from an object and passing these values as parameters in a method call, send the whole object instead (more...)
76.
Remove Parameter
If a parameter is no longer used by the method body, remove it (more...)
77.
Remove Setting Method
A field should be set at creation time and never altered. Remove any setting method for that field (more...)
78.
Rename Method
If the name of a method does not reveal its purpose, change the name of the method (more...)
79.
Replace Constructor with Factory Method
If you want to do more than simple construction when you create an object, replace the constructor with a factory method (more...)
软件设计的原则&101个设计模式-2011年04月25日  陈皓
101个设计模式
101 Design Patterns & Tips for Developers
80.
Replace Error Code with Exception
If a method returns a special code to indicate an error, throw an exception instead (more...)
81.
Replace Exception with Test
If you are throwing a checked exception on a condition the caller could have checked first, change the caller to make the test first (more...)
82.
Replace Parameter with Explicit Methods
If you have a method that runs different code depending on the values of an enumerated parameter, create a separate method for each value of the parameter (more...)
83.
Replace Parameter with Method
If an object invokes a method, then passes the result as a parameter for a method. The receiver can also invoke this method, remove the parameter and let the receiver invoke the method (more...)
84.
Separate Query from Modifier
If you have a method that returns a value but also changes the state of an object, create two methods, one for the query and one for the modification (more...)
Dealing with Generalization
85.
Collapse Hierarchy
If a superclass and subclass are not very different, merge them together (more...)
86.
Extract Interface
If several clients use the same subset of a class's interface, or two classes have part of their interfaces in common, extract the subset into an interface (more...)
87.
Extract Subclass
If a class has features that are used only in some instances, create a subclass for that subset of features (more...)
软件设计的原则&101个设计模式-2011年04月25日  陈皓
101个设计模式
101 Design Patterns & Tips for Developers
88.
Replace Array with Object
If you have an array in which certain elements mean different things, replace the array with an object that has a field for each element (more...)
89.
Replace Data Value with Object
If you have a data item that needs additional data or behavior, turn the data item into an object (more...)
90.
Extract Superclass
If you have two classes with similar features, create a superclass and move the common features to the superclass (more...)
91.
Form Template Method
If you have two methods in subclasses that perform similar steps in the same order, yet the steps are different, get the steps into methods with the same signature, so that the original methods become the same. Then you can pull them up (more...)
92.
Pull Up Constructor Body
If you have constructors on subclasses with mostly identical bodies, create a superclass constructor; call this from the subclass methods (more...)
93.
Pull Up Field
If two subclasses have the same field, move the field to the superclass (more...)
94.
Pull Up Method
If you have methods with identical results on subclasses, move them to the superclass (more...)
95.
Push Down Field
If a field is used only by some subclasses, move the field to those subclasses (more...)
96.
Push Down Method
If behavior on a superclass is relevant only for some of its subclasses, move it to those subclasses (more...)
97.
Replace Delegation with Inheritance
If you're using delegation and are often writing many simple delegations for the entire interface, make the delegating class a subclass of the delegate (more...)
软件设计的原则&101个设计模式-2011年04月25日  陈皓
101个设计模式
101 Design Patterns & Tips for Developers
98.
Replace Inheritance with Delegation
If a subclass uses only part of a superclasses interface or does not want to inherit data, create a field for the superclass, adjust methods to delegate to the superclass, and remove the subclassing (more...)
Big Refactorings
99.
Convert Procedural Design to Objects
If you have code written in a procedural style, turn the data records into objects, break up the behavior, and move the behavior to the objects (more...)
100.
Extract Hierarchy
If you have a class that is doing too much work, at least in part through many conditional statements, create a hierarchy of classes in which each subclass represents a special case (more...)
101.
Tease Apart Inheritance
If you have an inheritance hierarchy that is doing two jobs at once, create two hierarchies and use delegation to invoke one from the other (more...)