标签:
abstract class LoggerFactory{ public void writeLog(){ Logger logger=this.createLogger(); logger.writeLog(); } public abstract LoggerFactory createLogger(); }
#include <iostream> using namespace std; //日记记录器接口:抽象产品 class Logger{ public: virtual void writeLog()=0; }; //数据库日志记录器 class DatabaseLogger:public Logger{ public: void writeLog(){ cout<<"数据库日志记录器"<<endl; } }; //文件日志记录器:具体产品 class FileLogger:public Logger{ public: void writeLog(){ cout<<"文件日志记录器"<<endl; } }; //日志记录器工厂接口:抽象工厂 class LoggerFactory{ public: virtual Logger* createLogger()=0; }; //数据库日志记录器工厂类:具体工厂 class DatabaseLoggerFactory:public LoggerFactory{ public: Logger* createLogger(){ return new DatabaseLogger(); } }; //文件日志记录器工厂类:具体工厂 class FileLoggerFactory:public LoggerFactory{ public: Logger* createLogger(){ return new FileLogger(); } }; //客户端测试代码 int main(){ LoggerFactory *loggerFactory=new DatabaseLoggerFactory(); Logger *logger=loggerFactory->createLogger(); logger->writeLog(); return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/sxhlovehmm/article/details/46864821