软件设计模式

维基百科,自由的百科全书

跳转到: 导航, 搜索

設計模式英語design pattern)這個術語是在1990年代,由Erich Gamma等人,從建築設計領域引入到計算機科學裡去的。是對軟體設計中普遍存在(反覆出現)的各種問題,所提出的解決方案。設計模式並不直接用來完成程式碼的編寫,而是描述在各種不同情況下,要怎麼解決問題的一種方案。物件導向設計模式通常以類別物件來描述其中的關係和相互作用,但不涉及用來完成應用程式的特定類別或物件。演算法不能算是一種設計模式,因為演算法主要是用來解決計算上的問題,而非設計上的問題。設計模式主要是使不穩定的依賴於相對穩定、具體依賴於相對抽象,避免會引起麻煩的緊耦合,以增強軟體設計面對並適應變化的能力。

並非所有的軟體模式都是設計模式,設計模式特指軟體設計層次上的問題。還有其它非設計模式的模式,如架構模式

随着软件开发社群对设计模式的兴趣日益增长,已经出版了一些相关的专著,定期召开相应的研讨会,而且Ward Cunningham为此发明了WikiWiki用来交流设计模式的经验。

目录

[编辑] 發展歷史

建筑师克里斯托佛·亚历山大在1977/79年编制了一本汇集设计模式的书。但是这种设计模式的思想在建筑设计领域里的影响远没有后来在软件开发领域里传播的广泛。

肯特·贝克沃德·坎宁安在1987年,利用克里斯托佛·亚历山大在建筑设计领域里的思想开发了设计模式并把此思想应用在Smalltalk中的图形用户接口的生成中。一年后Erich Gamma在他的苏黎世大学博士毕业论文中开始尝试把这种思想改写为适用于软件开发。于此同时James Coplien 在1989年至1991 年也在利用相同的思想致力于C++的开发,而后于1991年发表了他的著作Advanced C++ Idioms。就在这一年Erich Gamma 得到了博士学位,然后去了美国,在那与Richard Helm, Ralph Johnson ,John Vlissides 合作出版了Design Patterns - Elements of Reusable Object-Oriented Software 一书,在此书中共收录了23个设计模式

这四位作者在软件开发领域里也以他们的匿名著称Gang of Four(四人帮,简称GoF),并且是他们在此书中的协作导致了软件设计模式的突破。有时这个匿名GoF也会用于指代前面提到的那本书。

[编辑] 表述格式

表述一个软件设计模式的格式根据作者的不同,划分和名称等都会有所不同。常用的GoF描述模式的格式大致分为以下这些部分:

  • 模式名:每一个模式都有自己的名字,模式的名字使得我们可以讨论我们的设计。
  • 问题:在面向对象的系统设计过程中反复出现的特定场合,它导致我们采用某个模式。
  • 解决方案:上述问题的解决方案,其内容给出了设计的各个组成部分,它们之间的关系、职责划分和协作方式。
  • 别名:一個模式可以有超過一個以上的名稱。這些名稱應該要在這一節註明。
  • 动机:該模式應該利用在哪種情況下是本節提供的方案(包括問題與來龍去脈)的責任。
  • 適用性:模式適用於哪些情況、模式的背景等等。
  • 结构:這部分常用類圖與互動圖闡述此模式。
  • 参与者:這部分提供一份本模式用到的類與物件清單,與它們在設計下扮演的腳色。
  • 合作:描述在此模式下,類與物件間的互動。
  • 影響:采用该模式对软件系统其他部分的影响,比如对系统的扩充性、可移植性的影响。影响也包括负面的影响。這部分應描述使用本模式後的結果、副作用、與权衡(trade-off)
  • 實作:這部分應描述實現該模式、該模式的部分方案、實現該模式的可能技術、或者建議實現模式的方法。
  • 示例:簡略描繪出如何以程式語言來使用模式。
  • 已知应用:業界已知的實作範例。
  • 相关模式:這部分包括其他相關模式,以及與其他類似模式的不同。

[编辑] 分类

<<Design Patterns>>一书原先把设计模式分为创建型模式,结构型模式,行为型模式.把它们通过授权,聚合,诊断的概念来描述.若想更进一步了解关于面向对象设计的背景,参考接口模式,内聚.若想更进一步了解关于面向对象编程的背景,参考继承,接口,多态.

模式名称 描述 是否在 《设计模式》 是否在 《Code Complete》[1]
创建型模式
抽象工厂模式 为一个产品族提供了统一的创建接口。当需要这个产品族的某一系列的时候,可以从抽象工厂中选出相应的系列创建一个具体的工厂类。
工厂方法模式 Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
生成器模式 Separate the construction of a complex object from its representation so that the same construction process can create different representations.
懒惰初始化模式 推迟对象的创建、数据的计算等需要耗费较多资源的操作,只有在第一次访问的时候才执行。
对象池模式 通过回收利用对象避免获取和释放资源所需的昂贵成本。
原型模式 Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
单例模式 确保一个类只有一个实例,并提供对该实例的全局访问。
结构型模式
适配器模式 将某个类的接口转换成客户端期望的另一个接口表示。适配器模式可以消除由于接口不匹配所造成的类兼容性问题。

interfaces.

桥接模式 Decouple an abstraction from its implementation so that the two can vary independently.
组合模式 Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
修饰模式 向某个对象动态地添加更多的功能。修饰模式是除类继承外另一种扩展功能的方法。
外观模式 Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
享元模式 通过共享以便有效的支持大量小颗粒对象。
代理模式 Provide a surrogate or placeholder for another object to control access to it.
行为模式
责任链模式 Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass

the request along the chain until an object handles it.

命令模式 Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
解释器模式 Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.
迭代器模式 Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
Mediator Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
备忘录模式 Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.
观察者模式 Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
状态模式 Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
策略模式 Define a family of algorithms, encapsulate each one, and make them interchangable. Strategy lets the algorithm vary independently from clients that use it.
Specification Recombinable Business logic in a boolean fashion
模板方法模式 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 algorithm's structure.

访问者模式 Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the

elements on which it operates.

Single-serving visitor Optimize the implementation of a visitor that is allocated, used only once, and then deleted
Hierarchical visitor Provide a way to visit every node in a hierarchical data structure such as a tree.
并发模式
Active Object
Balking
Double checked locking
Guarded
Leaders/followers
Monitor object
Read write lock
Scheduler
线程池模式
Thread-specific storage
Reactor

[编辑] 參閱

[编辑] 参考资料

[编辑] 外部連結

个人工具