閱讀筆記:Design Pattern

筆記書

閱讀筆記

關注焦點

學習領域

常用資源

閱讀雜誌

影音紀錄

達人朋友

生活購物

訊息

相關網站

參考文獻

最新修改

簡體版

English

  • Design Patterns — Gamma, Helm, Johnson, Vlissides
  • 目的: provides a scheme for refining the sybsystems or components of a software system, or the relationships between them. It describes a commonly-recurring structure of communicating component that solves a general design problem within a particular context.

Creational Pattern :

  • Abstract Factory :
  • 目的 : 使程式能輕易的 Port 到另外一個系統上。
  • 使用時機:為要建立一個容易 update , 可移植性高的系統時。
class WidgetFactory { 
 virtual createScrollBar(); 
 virutal createWindow(); 
}
class MsWidgetFactory : public WidgetFactory { 
 createScrollBar(); 
 createWindow(); 
}
class XWidgetFactory : public WidgetFactory { 
 createScrollBar(); 
 createWindow(); 
}

Builder

  • 目的 : Client 不斷呼叫 Builder 建立 component 直到建立完整的 complex component 才呼叫 Builder 傳回 complex component。
  • 使用時機:在設計多目標,多樣的彈性系統時,或目標樣式尚未決定時使用之。
class RTFReader { 
 virtual construct() { 
  while (t = get the next token) 
  switch t.type 
   CHAR : builder-ConvertCharacter(t.Char) 
   FONT : builder-ConvertFontChange(t.Fount) 
   PARA : builder-ConvertParagraph() 
 } 
}
class TextBuilder { 
 virtual ConvertCharacter(); 
 virtual ConvertFontChange(Font) 
 virtual ConvertParagraph() 
}
class ASCIIBuilder : public TextBuilder { 
 ConvertCharacter(char); 
 GetASCIIText() 
}
class TeXBuilder : public TextBuilder { 
 ConvertCharacter(char) 
 ConvertFontChange(Font) 
 ConvertParagraph() 
 GetTeXText() 
}

Factory Method

  • 目的 : 當 Factory 無法預期以後會需要什麼樣的 component 時,使用 Factory Method ,將責任延遲到 concreteProcuct 身上。
  • 使用時機:在目標功能尚無法決定或可能變化時使用之。
class Figure { 
 virtual createManipulator() ; } 
}
class LineFigure : public Figure{ 
 createManipulator(); 
}
class TextFigure : public Figure { 
 createManipulator(); 
}
class Manipulator { 
 virtual DownClick(); 
 virtual Drag(); 
 virtual UpClick(); 
}
class LineManipulator : public Manipulator { 
 DownClick(); 
 Drag(); 
 UpClick(); 
}
class TextManipulator : public Manipulator { 
 DownClick(); 
 Drag(); 
 UpClick(); 
}

Prototype :

  • 目的 : 讓 object 來擔任建立與自己相同型態的 object 的任務。
  • 使用時機:在設計者無法預期以後會有何種物件,且未來維護者可能無能力或無法加入新的物件到物件管理程式之中時。
class Graphic { 
 virtual draw(Position) 
 virtual clone() 
}
class Staff : public Graphic{ 
 draw(Position) 
 clone() { return copy of self } 
}
class MusicalNote : public Graphic { 
 draw(Position) 
 clone() { return copy of self } 
}
class Tool { 
 virtual manipulate() 
}
class RotateTool : public Tool { 
 manipulate() 
}
class GraphicTool : public Tool { 
 Graphic *prototype; 
 manipulate() 
 { 
  while (user drags mouse) 
   p-draw(newPosition) 
  insert p into drawing 
 } 
}

Singleton

  • 目的 : 讓一個 class 只能有一個 instance。
class Singleton { 
 static Singleton* instance() 
 static Singleton* _instance; 
}

Singleton* Singleton::_instance = NULL;

Singleton* Singleton::_instance() { 
 if (_instance == 0) 
  _instance = new Singleton 
 return _instance 
}

Structural Patterns

Adapter

  • 目的 : 把別人的物件整合到自己的 interface 中。
  • 使用時機:when two incompatible classes should work together。
class Shape { // Target 
 virtual BoundingBox(); 
 virtual CreateManipulator(); 
}
class Line : public Shape { 
 BoundingBox(); 
 CreateManipulator(); 
}
class TextShape : public Shape { // adapter 
 TextView text; // 連到別人的物件。 
 BoundingBox() { return text-GetExtent(); } 
 CreateManipulator { return new TextManipulator(); } 
}
class TextView { // adaptee 
 GetExtent(); 
}

Bridge

  • 目的 : 把 interface 與 implementation 分開使其互不影響。
  • 使用時機:在設計前用來隔開 user , user interface, object interface 與 implementation。
class Window { 
 WindowImp* _imp; 
}
class IconWindow : public Window { 
}
class TransientWindow : public Window { 
}
class WindowImp { 
}
class XWindowImp : public WindowImp { 
}
class MSWindowImp : public WindowImp { 
}

Composite

  • 目的 : 遞回定義的物件
  • 使用時機: 對於結構複雜的物件,但介面一致的物件使用之。
class Graphic { 
 virtual draw(); 
 virtual add(Graphic); 
 virtual remove(Graphic); 
 virtual GetChild(int); 
}
class Line : public Graphic { 
 draw(); 
}
class Text : public Graphic { 
 draw(); 
}
class CompositeGraph : public Graphic { 
 Graphic item[]; 
 draw() { for (i= 1; i< toatlItem; i++) item[i].draw(); } 
 add(Graphic g); 
 remove(Graphic g); 
 GetChild(int) { return item[i]; } 
}

Decorator

  • 目的 : 使附屬功能能以簡單的繼承方式一層層的套疊上去。
  • 使用時機:要附加功能又不用破壞原來的架構的情況下。
class VisualComponent { 
 virtual draw(); 
} 
class TextView : public VisualComponent { 
 draw(); 
} 
class Decorator : public VisualComponent { 
 virtual draw(); 
} 
class ScrollDecorator : public Decorator { 
 position scrollPosition; 
 draw(); 
 scrollTo(); 
} 
class BorderDecorator : public Decorator { 
 int borderWidth; 
 draw(); { Decorator::draw(); drawBorder(); } 
 drawBorder(); 
}

Facade

  • 目的:提供一整組物件對外的介面,以統一對外界面並達到 loose couple 的功能。
  • 使用時機:為在系統太複雜的時候,作為整合之用。
class Compiler { 
 Scanner _scanner; 
 Parser _parser; 
 CodeGenerator _generator; 
  
 Compile(); 
}

Flyweight :

  • 目的:利用物件共享的方式以節省實體空間。
  • 使用時機:許多物件都會包含同一物件許多次的時候使用之。
class Glyph { 
 virtual draw(Context) 
 virtual intersect(Point, Context); 
}

class Character : public Glyph { 
 char c; 
  
 draw(Context); 
 intersect(Point, Context); 
}

class Row : public Glyph { 
 Glyph children[]; 
  
 draw(Context); 
 intersect(Point, Context); 
}

class Column : public Glyph { 
 Glyph children[]; 
  
 draw(Context); 
 intersect(Point, Context); 
}

Proxy

  • 目的:作為其它物件的存取門戶。
  • 使用時機:物件因為存取時間太長,或重複存取次數太多,或有可能根本不會存取到時使用。
class Graphic { 
 virtual draw(); 
 virtual getExtent(); 
 virtual store(); 
 virtual load(); 
}

class ImageProxy : public Graphic { 
 Image *_image; 
 char *fileName; 
 extent // 影像的屬性。
 draw() 
 { 
   if (image == 0) 
    loadImage(fileName); 
  _image-draw(); 
 }
 getExtent() 
 { 
   if (image == 0) 
    return extent; 
  else 
    return image-GetExtent(); 
 }
 store(); 
 load(); 
}

class Image : public Graphic { 
 imageImp; 
 extent // 影像的屬性。 
  
 draw(); 
 getExtent(); 
 store(); 
 load(); 
}

Behavorial patterns

Chain of Responsibility :

 

  • Command (Command Processor) :

 

  • Interpreter :

 

  • Iterator :

 

  • Mediator :

 

  • Memento :

 

  • Observer :

 

  • State :

 

  • Strategy :

 

Template Method :

 

  • Visitor :

 

Facebook

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License