抽象工厂模式

标题

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;
}

上一篇:【死磕 Spring】----- IOC 之 属性填充


下一篇:MySQL 批量插入之 --怎样不插入重复的数据?