Design Patterns Quick Memo

[Creational Patterns]

Abstract factory: use generic interface, client doesn't know which concrete class is being instantiated

Builder: step by step build something, example: building a house, making subway sandwich

Factory:

prototype: 从现有的几个代表形clone新的, example: map editor

singleton:仅一个instance。。static元素的继承问题??

 

[Structural Patterns]

adapter: 把不同的接口适应化

bridge:分离主体和具体的implemenation, example:问卷调查template+具体问卷问题

composite: 文件和文件的相互包含和层级关系, example: 文件系统

decorator:不同的窗口的美化,scroller/frame,添加一个水平线在subclass

facade:加一个统一的中间接口,隔离外界和子系统,e.g. compiler system

flight weight, 游戏引擎中对sprite的复用, reuse state,event

proxy: e.g.编辑器/浏览器里,读取图像文件失败时显示的红叉叉

 

[Behavioral Patterns]

chain of responsibility: one of the item in the open list is gonna handle the problem, which is opaque to the outside

 

command: queue/log request, a list/buffer..

 

interpreter: regex

 

iterator: traverse stuff, C++ STL, Java

 

mediator:租房,求房,(同辈之间)透过中介的关系; 按钮被另一个组建dictates

 

memento: save/screen shot a particular state of the subject

 

observer(subscription): flash, eventlistener thing; C++, delegation.

 

state: multiply states act according to same interface(e.g. TCPState, GameState, AI, animation tree)

 

strategy: a family of algorithm to solve a certain problem, encapsulated each one.  (e.g. textEditor, linebreaker??)

http://en.wikipedia.org/wiki/Strategy_pattern

 

template method: the most basic/common use of inheritance

 

Visitor: allow outside functionalities to be executed

class Element {
  Accept(Visitor visitor)  {
    visitor.Visit(this);
  }
}

class Visitor {
  Visit(Element element) {
    //Do something
    ...     } }

(NOTICE: while virtual functions are dispatched dynamically in C++, function overloading is done statically.)

 

posted on 2013-04-10 01:56  安泰  阅读(194)  评论(0编辑  收藏  举报

导航