其他分享
首页 > 其他分享> > 抽象工厂模式

抽象工厂模式

作者:互联网

标题

class Log
{
public:
	virtual void writeLog() = 0;    //纯虚函数,抽象类,
	virtual ~Log() {}
};
class database :public Log
{
public:
	database() {}
	~database() {}

	void writeLog()
	{
		cout << "Write Database Log!" << endl;
	}
};
class fileLog :public Log
{
public:
	fileLog() {}
	~fileLog() {}
	void writeLog()
	{
		cout << "write File log!" << endl;
	}
};

class LogFactory
{
public:
	virtual unique_ptr<Log> createLog() = 0;
};

class DatabaseLogFactory:public LogFactory
{
public:
	unique_ptr<Log> createLog()
	{
		unique_ptr<Log> databaseLog(new database());   //创建数据库日志对象


		return databaseLog;
	}
};

class FileLogFactory :public LogFactory
{
public:
	unique_ptr<Log> createLog()
	{
		unique_ptr<Log> log (new fileLog());
		return log;
	}
};
int main()
{
	unique_ptr<LogFactory> uni_ptr(new FileLogFactory());   //指向Log工厂的智能指针

	unique_ptr<Log> ptr(nullptr);    //指向Log对象的智能指针

	ptr = uni_ptr->createLog();

	ptr->writeLog();    //写日志

	unique_ptr<LogFactory>uni_ptr1(new DatabaseLogFactory());
	ptr = uni_ptr1->createLog();
	ptr->writeLog();
		
	system("pause");
	return 0;
}

标签:Log,createLog,writeLog,模式,ptr,抽象,工厂,unique,public
来源: https://blog.csdn.net/ALITAAAA/article/details/115332730