C++ 中的工厂模式和抽象工厂模式都是为了实现对象的创建和实例化,但它们在设计和使用方式上有一些区别。
工厂模式(Factory Pattern):
工厂模式通过一个工厂类来封装对象的创建逻辑,并将具体对象的创建细节隐藏起来。客户端只需通过工厂类调用相应的方法来获取所需的对象实例。 工厂模式适用于需要创建一组相关对象的场景,工厂类可以根据条件返回不同的具体对象。 工厂模式强调的是创建过程的封装,使得客户端与具体对象之间解耦。 抽象工厂模式(Abstract Factory Pattern):
抽象工厂模式通过提供一个抽象的工厂接口,定义了一系列相关或依赖对象的创建方法。每个具体工厂类负责实现这个接口,从而创建属于自己产品族的具体对象。 抽象工厂模式适用于需要创建多个相关对象族的场景,每个具体工厂类可以负责创建一整套产品。 抽象工厂模式强调的是创建一系列相关对象的能力,它提供了一种更高级别的抽象,允许工厂类在产品族的维度上进行扩展。 关键区别:
工厂模式创建一种类型的对象,而抽象工厂模式创建一组相关对象。 工厂模式使用单个工厂类,而抽象工厂模式使用多个具体工厂类。 工厂模式强调的是对象创建过程的封装,而抽象工厂模式强调的是产品族的创建能力。 选择使用哪种模式取决于具体的需求。如果只需要创建一种类型的对象,可以使用工厂模式。如果需要创建多个相关对象族,并且希望能够轻松扩展和替换这些对象族,可以使用抽象工厂模式。
抽象工厂模式2个示例如下:
#include <iostream>
namespace AbsfactModel{
//数据库访问相关操作
class DataConnection
{
public:
DataConnection(){};
virtual ~DataConnection(){};
};
class DataCommand
{
public:
DataCommand(){};
virtual ~DataCommand(){};
};
class DataReader
{
public:
DataReader(){};
virtual ~DataReader(){};
};
class DataFactory
{
public:
virtual DataConnection* createDataConnection()=0;
virtual DataCommand* createDataCommand()=0;
virtual DataReader* createDataDataReader()=0;
};
//MySQL
class MySqlConnection:public DataConnection
{
public:
MySqlConnection()
{
std::cout << "MySqlConnection " << std::endl;
}
~MySqlConnection()
{
std::cout << "~MySqlConnection " << std::endl;
}
};
class MySqlCommand:public DataCommand
{
public:
MySqlCommand()
{
std::cout << "MySqlCommand " << std::endl;
}
~MySqlCommand()
{
std::cout << "~MySqlCommand " << std::endl;
}
};
class MySqlDataReader:public DataReader
{
public:
MySqlDataReader()
{
std::cout << "MySqlDataReader " << std::endl;
}
~MySqlDataReader()
{
std::cout << "~MySqlDataReader " << std::endl;
}
};
class MySqlFactory:public DataFactory
{
public:
virtual DataConnection* createDataConnection()override
{
return new MySqlConnection();
};
virtual DataCommand* createDataCommand()override
{
return new MySqlCommand();
};
virtual DataReader* createDataDataReader()override
{
return new MySqlDataReader();
};
};
//MongoDb
class MongoDbConnection:public DataConnection
{
public:
MongoDbConnection()
{
std::cout << "MongoDbConnection " << std::endl;
}
~MongoDbConnection()
{
std::cout << "~MongoDbConnection " << std::endl;
}
};
class MongoDbCommand:public DataCommand
{
public:
MongoDbCommand()
{
std::cout << "MongoDbCommand " << std::endl;
}
~MongoDbCommand()
{
std::cout << "~MongoDbCommand " << std::endl;
}
};
class MongoDbDataReader:public DataReader
{
public:
MongoDbDataReader()
{
std::cout << "MongoDbDataReader " << std::endl;
}
~MongoDbDataReader()
{
std::cout << "~MongoDbDataReader " << std::endl;
}
};
class MongoDbFactory:public DataFactory
{
public:
virtual DataConnection* createDataConnection()override
{
return new MongoDbConnection();
};
virtual DataCommand* createDataCommand()override
{
return new MongoDbCommand();
};
virtual DataReader* createDataDataReader()override
{
return new MongoDbDataReader();
};
};
}
class UseData
{
public:
UseData(AbsfactModel::DataFactory* cnFt):
pFact(cnFt)
{};
AbsfactModel::DataFactory* pFact;
void ReadrData()
{
AbsfactModel::DataConnection* data1 = pFact->createDataConnection();
AbsfactModel::DataCommand* data2 = pFact->createDataCommand(); //关联性
AbsfactModel::DataReader* data3 = pFact->createDataDataReader();
delete data1;
data1 = nullptr;
delete data2;
data2 = nullptr;
delete data3;
data3 = nullptr;
}
};
int main()
{
AbsfactModel::DataFactory* cFact = new AbsfactModel::MySqlFactory();
UseData obj(cFact);
obj.ReadrData();
return 0;
}
#include <iostream>
using namespace std;
/*
抽象工厂模式
1. 抽象产品类
2. 抽象产品族
3. 具体产品类
4. 抽象工厂类
5. 具体工厂类
*/
//----生产键盘和鼠标----
//抽象产品类: 声明了产品具有的业务方法
class AbstractProduct
{
public:
virtual ~AbstractProduct() {}
virtual void MakeProduct() = 0;
};
//1. 键盘
//抽象产品族
class KeyBoard :public AbstractProduct {};
//具体产品类:实现产品的具体方法
class LogiKeyBoard :public KeyBoard
{
public:
~LogiKeyBoard() {}
void MakeProduct()override
{
cout << "罗技键盘" << endl;
}
};
class RazerKeyBoard :public KeyBoard
{
public:
~RazerKeyBoard() {}
void MakeProduct()override
{
cout << "雷蛇键盘" << endl;
}
};
//2. 鼠标
//抽象产品族
class Mouse :public AbstractProduct {};
//具体产品类
class LogiMouse :public Mouse
{
public:
~LogiMouse() {}
void MakeProduct()override
{
cout << "罗技鼠标" << endl;
}
};
class RazerMouse :public Mouse
{
public:
~RazerMouse() {}
void MakeProduct()override
{
cout << "雷蛇鼠标" << endl;
}
};
/*
//TODO:在此增加新的产品族与具体产品类...
//
//*/
//抽象工厂类: 声明提供创建每一族产品的方法
class AbstractFactory
{
public:
virtual ~AbstractFactory() {}
virtual KeyBoard* CreateKeyBoard() = 0;
virtual Mouse* CreateMouse() = 0;
//TODO: 提供每一个产品族的创建方法...
};
//具体工厂类:实现创建产品的方法
//1. 罗技工厂
class LogiFactory:public AbstractFactory
{
public:
~LogiFactory() {}
KeyBoard* CreateKeyBoard()override
{
return new LogiKeyBoard;
}
Mouse* CreateMouse()override
{
return new LogiMouse;
}
};
//2. 雷蛇工厂
class RazerFactory :public AbstractFactory
{
public:
~RazerFactory() {}
KeyBoard* CreateKeyBoard()override
{
return new RazerKeyBoard;
}
Mouse* CreateMouse()override
{
return new RazerMouse;
}
};
int main()
{
AbstractFactory* Factory = new RazerFactory;
KeyBoard* keyBoard = Factory->CreateKeyBoard();
Mouse* mouse = Factory->CreateMouse();
keyBoard->MakeProduct();
mouse->MakeProduct();
delete Factory;
delete keyBoard;
delete mouse;
Factory = new LogiFactory;
keyBoard = Factory->CreateKeyBoard();
mouse = Factory->CreateMouse();
keyBoard->MakeProduct();
mouse->MakeProduct();
delete Factory;
delete keyBoard;
delete mouse;
Factory = nullptr;
keyBoard = nullptr;
mouse = nullptr;
return 0;
}
工厂模式示例如下:
#include <iostream>
using namespace std;
//造飞机造火箭
/*----------------
1. 抽象产品类
----------------*/
class AbstractProduct
{
public:
virtual void MakeProduct() = 0;
};
/*----------------
2. 抽象工厂类
----------------*/
class AbstractFactory
{
public:
virtual AbstractProduct* CreateProduct() = 0;
};
/*----------------
3. 具体产品类: 飞机
----------------*/
class PlaneProduct:public AbstractProduct
{
public:
void MakeProduct()override
{
cout << "飞机产品" << endl;
}
};
/*----------------
4. 具体工厂类:造飞机
----------------*/
class PlaneFactory :public AbstractFactory
{
public:
AbstractProduct* CreateProduct()override
{
return new PlaneProduct;
}
};
/*----------------
3. 具体产品类: 火箭
----------------*/
class RocketProduct :public AbstractProduct
{
public:
void MakeProduct()override
{
cout << "火箭产品" << endl;
}
};
/*----------------
4. 具体工厂类:造火箭
----------------*/
class RocketFactory :public AbstractFactory
{
public:
AbstractProduct* CreateProduct()override
{
return new RocketProduct;
}
};
int main()
{
//1. 开设工厂(飞机,火箭)
AbstractFactory* factory = new PlaneFactory;
//2. 制造产品
AbstractProduct* product = factory->CreateProduct();
//3. 使用产品
product->MakeProduct();
factory = new RocketFactory;
product = factory->CreateProduct();
product->MakeProduct();
return 0;
}
标签:MakeProduct,c++,class,抽象,模式,工厂,public
From: https://blog.51cto.com/danielllf/7850430