大話設計模式C++實現-第15章-抽象原廠模式

來源:互聯網
上載者:User

標籤:hit   delete   nbsp   模式   技術   執行個體   細節   角色   cto   

一、UML圖



二、概念

抽象方法模式(Abstract Factory):提供一個建立一系列相關或互相依賴對象的介面,而無需指定他們詳細的類。


三、包括的角色

(1)抽象工廠

(2)詳細工廠:包含詳細工廠1和詳細工廠2。詳細工廠1用於生產詳細產品A1和詳細產品B1,詳細工廠2用於生產詳細產品A2和詳細產品B2;

(3)抽象產品:包含抽象產品A和抽象產品B。

(4)詳細產品:包含抽象產品A所相應的詳細產品A1和A2。以及抽象產品B所相應的詳細產品B1和B2.

說明:在《大話設計模式》中,上述的1和2分別代表Sqlserver資料庫和Access資料庫。上述的A和B分別代表資料庫中的User表和Department表。


四、優勢

(1)抽象原廠模式是對Factory 方法模式的改進。

用於處理產品不僅僅有一類的情況(Factory 方法模式下。產品僅僅有User這一類,而抽象原廠模式下。產品包含User和Department兩類)。

(2)在下面情況下應當考慮使用抽象原廠模式:

  •  一個系統不應當依賴於產品類執行個體怎樣被建立、組合和表達的細節,這對於全部形態的原廠模式都是重要的。
  • 這個系統有多於一個的產品族。而系統僅僅消費當中某一產品族。
  • 同屬於同一個產品族的產品是在一起使用的,這一約束必須在系統的設計中體現出來。

  • 系統提供一個產品類的庫,全部的產品以相同的介面出現,從而使client不依賴於實現。

(3)解讀:

在上例中。產品族包括兩個:1和2,也就是Sqlserver資料庫和Access資料庫。

每一個產品族裡面又包括兩類產品:A和B,也就是User表和Department表。而每一個產品族中的產品要一起使用,就是說產品族1中的兩類產品A和B要一起使用,也就是說在SqlServer資料庫中SqlServerUser表和SqlServerDepartment表要一起使用,Access資料庫同理。

五、C++實現

(1)代碼

#include <iostream>#include <cstdlib>using namespace std;//資料庫表項:Userclass User{private:int id;string name;public:int getID(){return id;}string getName(){return name;}void setID(int ID){this->id=ID;}void setName(string NAME){this->name=NAME;}};//資料庫表項:Departmentclass Department{private:int id;string name;public:int getID(){return id;}string getName(){return name;}void setID(int ID){this->id=ID;}void setName(string NAME){this->name=NAME;}};//抽象產品A:IUserclass IUser{public:virtual void Insert(User user)=0;virtual User* GetUser(int id)=0;};//詳細產品A1:SqlserverUserclass SqlserverUser:public IUser{public:void Insert(User user){cout<<"在SQL Server中給User表添加了一條記錄"<<endl;}User* GetUser(int id){cout<<"在SQL Server中依據ID得到User表一條記錄"<<endl;return NULL;}};//詳細產品A2:AccessUserclass AccessUser:public IUser{public:void Insert(User user){cout<<"在Access中給User表添加了一條記錄"<<endl;}User* GetUser(int id){cout<<"在Access中依據ID得到User表一條記錄"<<endl;return NULL;}};//抽象產品B:IDepartmentclass IDepartment{public:virtual void Insert(Department department)=0;virtual Department* GetDepartment(int id)=0;};//詳細產品B1:SqlserverDepartmentclass SqlserverDepartment:public IDepartment{public:void Insert(Department department){cout<<"在Sql Server中給Department表加入了一條記錄"<<endl;}Department* GetDepartment(int id){cout<<"在SQL Server中依據ID得到Department表的一條記錄"<<endl;return NULL;}};//詳細產品B2:AccessDepartmentclass AccessDepartment:public IDepartment{public:void Insert(Department department){cout<<"在Access中給Department表加入了一條記錄"<<endl;}Department* GetDepartment(int id){cout<<"在Access中依據ID得到Department表的一條記錄"<<endl;return NULL;}};//抽象工廠:IFactoryclass IFactory{public:virtual IUser* CreateUser()=0;virtual IDepartment* CreateDepartment()=0;};//詳細工廠1:SqlServerFactoryclass SqlserverFactory:public IFactory{public:IUser* CreateUser(){return new SqlserverUser;}IDepartment* CreateDepartment(){return new SqlserverDepartment;}};//詳細工廠2:AccessFactoryclass AccessFactory:public IFactory{public:IUser* CreateUser(){return new AccessUser;}IDepartment* CreateDepartment(){return new AccessDepartment;}};//clientvoid  main(){User user;Department department;//ConcreteFactory1IFactory* factory=NULL;factory=new SqlserverFactory;//ProductA1IUser* iu=NULL;iu=factory->CreateUser();iu->Insert(user);iu->GetUser(1);//ProductB1IDepartment* id=NULL;id=factory->CreateDepartment();id->Insert(department);id->GetDepartment(1);if(factory!=NULL){delete factory;factory=NULL;}if(iu!=NULL){delete iu;iu=NULL;}if(id!=NULL){delete id;id=NULL;}system("pause");}

(2)執行


大話設計模式C++實現-第15章-抽象原廠模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.