Factory Method
Abstract Factory
Prototype
Builder數據庫
class ISplitter{ public: virtual void split()=0; virtual ~ISplitter(){} }; class BinarySplitter : public ISplitter{ }; class TxtSplitter: public ISplitter{ }; class PictureSplitter: public ISplitter{ }; class VideoSplitter: public ISplitter{ };
class MainForm : public Form { TextBox* txtFilePath; TextBox* txtFileNumber; ProgressBar* progressBar; public: void Button1_Click(){ ISplitter * splitter= new BinarySplitter();//依賴具體類 splitter->split(); } };
工廠模式設計模式
//抽象類 class ISplitter{ public: virtual void split()=0; virtual ~ISplitter(){} }; //工廠基類 class SplitterFactory{ public: virtual ISplitter* CreateSplitter()=0; virtual ~SplitterFactory(){} }; //具體類 class BinarySplitter : public ISplitter{ }; class TxtSplitter: public ISplitter{ }; class PictureSplitter: public ISplitter{ }; class VideoSplitter: public ISplitter{ }; //具體工廠 class BinarySplitterFactory: public SplitterFactory{ public: virtual ISplitter* CreateSplitter(){ return new BinarySplitter(); } }; class TxtSplitterFactory: public SplitterFactory{ public: virtual ISplitter* CreateSplitter(){ return new TxtSplitter(); } }; class PictureSplitterFactory: public SplitterFactory{ public: virtual ISplitter* CreateSplitter(){ return new PictureSplitter(); } }; class VideoSplitterFactory: public SplitterFactory{ public: virtual ISplitter* CreateSplitter(){ return new VideoSplitter(); } }; class MainForm : public Form { SplitterFactory* factory;//工廠 public: MainForm(SplitterFactory* factory){ this->factory=factory; } void Button1_Click(){ ISplitter * splitter= factory->CreateSplitter(); //多態new splitter->split(); } };
經過抽象類和抽象工廠將具體的對象建立工做延遲到子類工廠的實現中。這樣實現了在mainForm中的多態new,實現了客戶端與具體類的解耦合。ide
定義一個用於建立子類對象的接口,讓子類決定實例化哪個類。Factory Method 使得一個類的實例化延遲(目的:解耦,手段:虛函數)到子類。
————《設計模式》GoF函數
class EmployeeDAO{ public: vector<EmployeeDO> GetEmployees(){ SqlConnection* connection = new SqlConnection(); connection->ConnectionString = "..."; SqlCommand* command = new SqlCommand(); command->CommandText="..."; command->SetConnection(connection); SqlDataReader* reader = command->ExecuteReader(); while (reader->Read()){ } } }; //數據庫訪問有關的基類 class IDBConnection{ }; class IDBConnectionFactory{ public: virtual IDBConnection* CreateDBConnection()=0; }; class IDBCommand{ }; class IDBCommandFactory{ public: virtual IDBCommand* CreateDBCommand()=0; }; class IDataReader{ }; class IDataReaderFactory{ public: virtual IDataReader* CreateDataReader()=0; }; //支持SQL Server class SqlConnection: public IDBConnection{ }; class SqlConnectionFactory:public IDBConnectionFactory{ }; class SqlCommand: public IDBCommand{ }; class SqlCommandFactory:public IDBCommandFactory{ }; class SqlDataReader: public IDataReader{ }; class SqlDataReaderFactory:public IDataReaderFactory{ }; //支持Oracle class OracleConnection: public IDBConnection{ }; class OracleCommand: public IDBCommand{ }; class OracleDataReader: public IDataReader{ }; class EmployeeDAO{ IDBConnectionFactory* dbConnectionFactory; IDBCommandFactory* dbCommandFactory; IDataReaderFactory* dataReaderFactory; public: vector<EmployeeDO> GetEmployees(){ IDBConnection* connection = dbConnectionFactory->CreateDBConnection(); connection->ConnectionString("..."); IDBCommand* command = dbCommandFactory->CreateDBCommand(); command->CommandText("..."); command->SetConnection(connection); //關聯性 IDBDataReader* reader = command->ExecuteReader(); //關聯性 while (reader->Read()){ } } }; //數據庫訪問有關的基類 class IDBConnection{ }; class IDBCommand{ }; class IDataReader{ }; class IDBFactory{ public: virtual IDBConnection* CreateDBConnection()=0; virtual IDBCommand* CreateDBCommand()=0; virtual IDataReader* CreateDataReader()=0; }; //支持SQL Server class SqlConnection: public IDBConnection{ }; class SqlCommand: public IDBCommand{ }; class SqlDataReader: public IDataReader{ }; class SqlDBFactory:public IDBFactory{ public: virtual IDBConnection* CreateDBConnection()=0; virtual IDBCommand* CreateDBCommand()=0; virtual IDataReader* CreateDataReader()=0; }; //支持Oracle class OracleConnection: public IDBConnection{ }; class OracleCommand: public IDBCommand{ }; class OracleDataReader: public IDataReader{ }; class EmployeeDAO{ IDBFactory* dbFactory; public: vector<EmployeeDO> GetEmployees(){ IDBConnection* connection = dbFactory->CreateDBConnection(); connection->ConnectionString("..."); IDBCommand* command = dbFactory->CreateDBCommand(); command->CommandText("..."); command->SetConnection(connection); //關聯性 IDBDataReader* reader = command->ExecuteReader(); //關聯性 while (reader->Read()){ } } };
模式定義:
提供一個接口,讓該接口負責建立一系列相關或者相互依賴的對象,無需指定它們具體的類。
————《設計模式》GoFui
動機:
this
//抽象類 class ISplitter{ public: virtual void split()=0; virtual ISplitter* clone()=0; //經過克隆本身來建立對象 virtual ~ISplitter(){} }; //具體類 class BinarySplitter : public ISplitter{ public: virtual ISplitter* clone(){ return new BinarySplitter(*this); } }; class TxtSplitter: public ISplitter{ public: virtual ISplitter* clone(){ return new TxtSplitter(*this); } }; class PictureSplitter: public ISplitter{ public: virtual ISplitter* clone(){ return new PictureSplitter(*this); } }; class VideoSplitter: public ISplitter{ public: virtual ISplitter* clone(){ return new VideoSplitter(*this); } }; class MainForm : public Form { ISplitter* prototype;//原型對象 public: MainForm(ISplitter* prototype){ this->prototype=prototype; } void Button1_Click(){ ISplitter * splitter= prototype->clone(); //克隆原型 splitter->split(); } };
使用原型實例指定建立對象的種類,而後經過拷貝這些原型來建立新的對象。
————《設計模式》GoFspa
class House{ //.... }; class HouseBuilder { public: House* GetResult(){ return pHouse; } virtual ~HouseBuilder(){} protected: House* pHouse; virtual void BuildPart1()=0; virtual void BuildPart2()=0; virtual void BuildPart3()=0; virtual void BuildPart4()=0; virtual void BuildPart5()=0; }; class StoneHouse: public House{ }; class StoneHouseBuilder: public HouseBuilder{ protected: virtual void BuildPart1(){ //pHouse->Part1 = ...; } virtual void BuildPart2(){ } virtual void BuildPart3(){ } virtual void BuildPart4(){ } virtual void BuildPart5(){ } }; class HouseDirector{ public: HouseBuilder* pHouseBuilder; HouseDirector(HouseBuilder* pHouseBuilder){ this->pHouseBuilder=pHouseBuilder; } House* Construct(){ pHouseBuilder->BuildPart1(); for (int i = 0; i < 4; i++){ pHouseBuilder->BuildPart2(); } bool flag=pHouseBuilder->BuildPart3(); if(flag){ pHouseBuilder->BuildPart4(); } pHouseBuilder->BuildPart5(); return pHouseBuilder->GetResult(); } };
講一個複雜對象的構建與其表示相分離,使得一樣的構建過程(穩定)能夠建立不一樣的表示(變化)
————《設計模式》GoFprototype
典型模式
Facade
Proxy
Adapter
Mediator設計
爲子系統中的一組接口提供一個一致的界面,Facade,模式定義了一個高層接口,這個接口使得這一子系統更加容易使用(複用)
————《設計模式》GoF代理
爲其餘對象提供一種代理以控制(隔離,使用窗口)對這個對象的訪問。
————《設計模式》GoF
class ISubject{ public: virtual void process(); }; //Proxy的設計 class SubjectProxy: public ISubject{ public: virtual void process(){ //對RealSubject的一種間接訪問 //.... } }; class ClientApp{ ISubject* subject; public: ClientApp(){ subject=new SubjectProxy(); } void DoTask(){ //... subject->process(); //.... } }; class ISubject{ public: virtual void process(); }; class RealSubject: public ISubject{ public: virtual void process(){ //.... } }; class ClientApp{ ISubject* subject; public: ClientApp(){ subject=new RealSubject(); } void DoTask(){ //... subject->process(); //.... } };
//目標接口(新接口) class ITarget{ public: virtual void process()=0; }; //遺留接口(老接口) class IAdaptee{ public: virtual void foo(int data)=0; virtual int bar()=0; }; //遺留類型 class OldClass: public IAdaptee{ //.... }; //對象適配器 class Adapter: public ITarget{ //繼承 protected: IAdaptee* pAdaptee;//組合 public: Adapter(IAdaptee* pAdaptee){ this->pAdaptee=pAdaptee; } virtual void process(){ int data=pAdaptee->bar(); pAdaptee->foo(data); } }; //類適配器 class Adapter: public ITarget, protected OldClass{ //多繼承 } int main(){ IAdaptee* pAdaptee=new OldClass(); ITarget* pTarget=new Adapter(pAdaptee); pTarget->process(); } class stack{ deqeue container; }; class queue{ deqeue container; };
將一個類的接口轉換成客戶但願的另外一個接口,Adapter模式使得本來因爲接口不兼容而不能在一塊兒工做的那些類能夠一塊兒工做。
————《設計模式》GoF
用一箇中介對象來封裝(封裝變化)一系列的對象交互。中介者使各對象不須要顯示的互相引用(編譯時依賴-》運行時依賴),從而使其耦合鬆散(管理變化),並且能夠獨立地改變它們之間的交互。
————《設計模式》GoF