Factory Method
是一個
Class Creational
范式。
原文
Intent
部分
:
?????
“
This creates a dilemma: The framework must instantiate classes, but it only knows about abstract classes, which it cannot instantiate.
”
1、
Application
使用
Factory Method
對
Framework
隱藏了
Document
的實現細節;
class Document
{
?????? virtual void open() = 0;
?????? virtual void close() = 0;
};
?
class Factory
{
?????? public:
??????
?????? //
一個虛函數,被子類實現以便決定真實的
Document
類型;
??????
?????? virtual Document createDocument() { return 0;};
};
?
class Application : public Factory
{
?????? …
};
?
class Framework
{
?????? private:
??????
?????? Application _application;
??????
?????? Document _document;
?????? …
};
2、
提供
hook
Hook
是用來掛東西的。這個被掛的東西要求與
Hook
的定義具有相同的原型。在
C
中,它被一個函數指針定義;在
C++
中,由于
overriding
的提供,它可以被虛函數以及純虛函數定義;
posted on 2006-06-27 14:58
靜靜的流水 閱讀(587)
評論(2) 編輯 收藏 引用 所屬分類:
Design Patterns