frame 1:
#include <iostream>
using namespace std;
class User_less11{
private:
int _id;
string _name;
public:
int ID(){ return _id;}
void setID(int id){_id = id;}
string Name(){return _name;}
void setName(string name){_name = name;}
};
class IUser_less11{
public:
virtual ~IUser_less11(){};
virtual void Insert(User_less11 * user){};
virtual User_less11 * GetUser(int id){return nullptr;};
};
class SqlserverUser_less11 : public IUser_less11{
public:
void Insert(User_less11 * user) {
cout<<"insert a new line into sql"<<endl;
}
User_less11 * GetUser(int id){
cout<<"get a user from sql"<<endl;
return nullptr;
}
};
class AccessUser_less11:public IUser_less11 {
public:
void Insert(User_less11 * user) {
cout<<"insert a new line into access"<<endl;
}
User_less11 * GetUser(int id){
cout<<"get a user from access"<<endl;
return nullptr;
}
};
class Department_less11{
private:
int _id;
string _name;
public:
int ID(){ return _id;}
void setID(int id){_id = id;}
string Name(){return _name;}
void setName(string name){_name = name;}
};
class IDepartment_le11 {
public:
~IDepartment_le11(){};
virtual void Insert(Department_less11 * d){};
virtual Department_less11 * GetDepartment(int id){return nullptr;};
};
class SqlserverDepartment_less11 : public IDepartment_le11{
public:
void Insert(Department_less11 * department) {
cout<<"insert a new department into sql"<<endl;
}
Department_less11 * GetDepartment(int id){
cout<<"get a department from sql"<<endl;
return nullptr;
}
};
class AccessDepartment_less11:public IDepartment_le11 {
public:
void Insert(Department_less11 * department) {
cout<<"insert a new department into access"<<endl;
}
Department_less11 * GetDepartment(int id){
cout<<"get a department from access"<<endl;
return nullptr;
}
};
class IFactory_less11 {
public:
~IFactory_less11(){};
virtual IUser_less11 * CreateUser(){return nullptr;};
virtual IDepartment_le11 * CreateDepartment(){return nullptr;};
};
class SqlServerFactory_less11:public IFactory_less11
{
public:
IUser_less11 * CreateUser(){
return new SqlserverUser_less11();
}
IDepartment_le11 * CreateDepartment(){return new SqlserverDepartment_less11();}
};
class AccessFactory_less11:IFactory_less11 {
public:
IUser_less11 * CreateUser(){
return new AccessUser_less11();
}
IDepartment_le11 * CreateDepartment(){return new AccessDepartment_less11();}
};
用工厂模式的时候,我们就要在修改需求的时候改变很多的工厂声明的地方,不是很方便。
#include <iostream>
using namespace std;
class User_less11{
private:
int _id;
string _name;
public:
int ID(){ return _id;}
void setID(int id){_id = id;}
string Name(){return _name;}
void setName(string name){_name = name;}
};
class IUser_less11{
public:
virtual ~IUser_less11(){};
virtual void Insert(User_less11 * user){};
virtual User_less11 * GetUser(int id){return nullptr;};
};
class SqlserverUser_less11 : public IUser_less11{
public:
void Insert(User_less11 * user) {
cout<<"insert a new line into sql"<<endl;
}
User_less11 * GetUser(int id){
cout<<"get a user from sql"<<endl;
return nullptr;
}
};
class AccessUser_less11:public IUser_less11 {
public:
void Insert(User_less11 * user) {
cout<<"insert a new line into access"<<endl;
}
User_less11 * GetUser(int id){
cout<<"get a user from access"<<endl;
return nullptr;
}
};
class Department_less11{
private:
int _id;
string _name;
public:
int ID(){ return _id;}
void setID(int id){_id = id;}
string Name(){return _name;}
void setName(string name){_name = name;}
};
class IDepartment_le11 {
public:
~IDepartment_le11(){};
virtual void Insert(Department_less11 * d){};
virtual Department_less11 * GetDepartment(int id){return nullptr;};
};
class SqlserverDepartment_less11 : public IDepartment_le11{
public:
void Insert(Department_less11 * department) {
cout<<"insert a new department into sql"<<endl;
}
Department_less11 * GetDepartment(int id){
cout<<"get a department from sql"<<endl;
return nullptr;
}
};
class AccessDepartment_less11:public IDepartment_le11 {
public:
void Insert(Department_less11 * department) {
cout<<"insert a new department into access"<<endl;
}
Department_less11 * GetDepartment(int id){
cout<<"get a department from access"<<endl;
return nullptr;
}
};
class DataAccess {
protected:
static const string db;
public:
static IUser_less11 * CreateUser(){
IUser_less11 * result = nullptr;
if (db == "sqlserver") {
result = new SqlserverUser_less11();
} else if (db == "access") {
result = new AccessUser_less11();
} else {
}
return result;
}
static IDepartment_le11 * CreateDepartment(){
IDepartment_le11 * result = nullptr;
if (db == "sqlserver") {
result = new SqlserverDepartment_less11();
} else if (db == "access") {
result = new AccessDepartment_less11();
}
return result;
}
};
const string DataAccess::db = "sqlserver";
fame3
#include <iostream>
using namespace std;
class User_less11{
private:
int _id;
string _name;
public:
int ID(){ return _id;}
void setID(int id){_id = id;}
string Name(){return _name;}
void setName(string name){_name = name;}
};
class IUser_less11{
public:
virtual ~IUser_less11(){};
virtual void Insert(User_less11 * user){};
virtual User_less11 * GetUser(int id){return nullptr;};
};
class SqlserverUser_less11 : public IUser_less11{
public:
void Insert(User_less11 * user) {
cout<<"insert a new line into sql"<<endl;
}
User_less11 * GetUser(int id){
cout<<"get a user from sql"<<endl;
return nullptr;
}
};
class AccessUser_less11:public IUser_less11 {
public:
void Insert(User_less11 * user) {
cout<<"insert a new line into access"<<endl;
}
User_less11 * GetUser(int id){
cout<<"get a user from access"<<endl;
return nullptr;
}
};
class Department_less11{
private:
int _id;
string _name;
public:
int ID(){ return _id;}
void setID(int id){_id = id;}
string Name(){return _name;}
void setName(string name){_name = name;}
};
class IDepartment_le11 {
public:
~IDepartment_le11(){};
virtual void Insert(Department_less11 * d){};
virtual Department_less11 * GetDepartment(int id){return nullptr;};
};
class SqlserverDepartment_less11 : public IDepartment_le11{
public:
void Insert(Department_less11 * department) {
cout<<"insert a new department into sql"<<endl;
}
Department_less11 * GetDepartment(int id){
cout<<"get a department from sql"<<endl;
return nullptr;
}
};
class AccessDepartment_less11:public IDepartment_le11 {
public:
void Insert(Department_less11 * department) {
cout<<"insert a new department into access"<<endl;
}
Department_less11 * GetDepartment(int id){
cout<<"get a department from access"<<endl;
return nullptr;
}
};
class DataAccess {
protected:
static const string db;
public:
static IUser_less11 * CreateUser(){
IUser_less11 * result = nullptr;
if (db == "sqlserver") {
result = new SqlserverUser_less11();
} else if (db == "access") {
result = new AccessUser_less11();
} else {
}
return result;
}
static IDepartment_le11 * CreateDepartment(){
IDepartment_le11 * result = nullptr;
if (db == "sqlserver") {
result = new SqlserverDepartment_less11();
} else if (db == "access") {
result = new AccessDepartment_less11();
}
return result;
}
};
const string DataAccess::db = "sqlserver";
抽象工厂的好处是易于交换产品,由于具体工厂类在一个应用中只需要初始化的时候出现一次,这使得改变一个应用的具体工厂变的十分容易,它只需要改变具体工厂即可使用不同的产品配置。
第二大好处是,它让具体创建实例过程与客户端分离,客户端是通过它们的抽象接口操作实例,产品的具体类名也被具体的工厂实现分离,不会出现在客户代码中。