c++ 23种设计模式之抽象工厂模式

#include <string> 
#include <iostream> 
#include <vector> 
using namespace std; 

//c++ 23种设计模式之抽象工厂模式
// 用户抽象接口 
class IUser 
{ 
public : 
	virtual void GetUser()=0; 
	virtual void InsertUser()=0; 
}; 

// 部门抽象接口 
class IDepartment 
{ 
public: 
	virtual void GetDepartment()=0; 
	virtual void InsertDepartment()=0; 
}; 

//ACCESS 用户 
class CAccessUser : public IUser 
{ 
public: 
	virtual void GetUser() 
	{ 
		cout<<"Access GetUser"<<endl; 
	} 
	virtual void InsertUser() 
	{ 
		cout<<"Access InsertUser"<<endl; 
	} 
}; 

//ACCESS 部门 
class CAccessDepartment : public IDepartment 
{ 
public: 
	virtual void GetDepartment() 
	{ 
		cout<<"Access GetDepartment"<<endl; 
	} 
	virtual void InsertDepartment() 
	{ 
		cout<<"Access InsertDepartment"<<endl; 
	} 
}; 

//SQL 用户 
class CSqlUser : public IUser 
{ 
public: 
	virtual void GetUser() 
	{ 
		cout<<"Sql User"<<endl; 
	} 
	virtual void InsertUser() 
	{ 
		cout<<"Sql User"<<endl; 
	} 
}; 

//SQL 部门类 
class CSqlDepartment: public IDepartment 
{ 
public: 
	virtual void GetDepartment() 
	{ 
		cout<<"sql getDepartment"<<endl; 
	} 
	virtual void InsertDepartment() 
	{ 
		cout<<"sql insertdepartment"<<endl; 
	} 
}; 

// 抽象工厂 
class IFactory 
{ 
public: 
	virtual IUser* CreateUser()=0; 
	virtual IDepartment* CreateDepartment()=0; 
}; 

//ACCESS 工厂 
class AccessFactory : public IFactory 
{ 
public: 
	virtual IUser* CreateUser() 
	{ 
		return new  CAccessUser(); 
	} 
	virtual IDepartment* CreateDepartment() 
	{ 
		return new CAccessDepartment(); 
	} 
}; 

//SQL 工厂 
class SqlFactory : public IFactory 
{ 
public: 
	virtual IUser* CreateUser() 
	{ 
		return new  CSqlUser(); 
	} 
	
	virtual IDepartment* CreateDepartment() 
	{ 
		return new CSqlDepartment(); 
	} 
}; 

//客户端: 
int main() 
{ 
	IFactory* factory= new SqlFactory(); 
	IUser* user=factory->CreateUser(); 
	IDepartment* depart = factory->CreateDepartment(); 
	user->GetUser(); 
	depart->GetDepartment(); 
	return 0; 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值