设计模式 每日一例 Abstract Factory
来源:互联网 发布:淘宝店铺被永久封店 编辑:程序博客网 时间:2024/05/16 07:05
其实这一篇开始才真正进入设计模式的学习。
这里参照了这个Blog的例子:http://www.cppblog.com/converse/,为了直观和适应一个正在进行的项目需要做了命名上的一些修改。
/************************************************************************ * File Name : AbstractFactory.h* Description: * Author : amphisbe* Create Date: 2012/07/18* Version : 1.0* Modified :* ************************************************************************/#ifndef __ABSTRACTFACTORY_H__#define __ABSTRACTFACTORY_H__//登入消息基类class AbstractMsgLogin{public:AbstractMsgLogin(void){};virtual ~AbstractMsgLogin(void){};};//登入请求class MsgLoginReq : public AbstractMsgLogin{public:MsgLoginReq(){};virtual ~MsgLoginReq(){};};//登入响应class MsgLoginRet : public AbstractMsgLogin{public:MsgLoginRet(){};virtual ~MsgLoginRet(){};};//登出消息基类class AbstractMsgLogout{public:AbstractMsgLogout(void){};virtual ~AbstractMsgLogout(void){};};//登出请求class MsgLogoutReq : public AbstractMsgLogout{public:MsgLogoutReq(){};virtual ~MsgLogoutReq(){};};//登出响应class MsgLogoutRet : public AbstractMsgLogout{public:MsgLogoutRet(){};virtual ~MsgLogoutRet(){};};//AbstractFactory类出场,消息构造器(工厂类)基类class AbstractMsgFactory{public:AbstractMsgFactory(){}virtual ~AbstractMsgFactory(){}virtual AbstractMsgLogin* CreateMsgLogin() = 0;virtual AbstractMsgLogout* CreateMsgLogout() = 0;};//请求消息构造器class ReqMsgFactory : public AbstractMsgFactory{public:ReqMsgFactory(){};virtual ~ReqMsgFactory(){};virtual AbstractMsgLogin* CreateMsgLogin();virtual AbstractMsgLogout* CreateMsgLogout();};//响应消息构造器class RetMsgFactory: public AbstractMsgFactory{public:RetMsgFactory(){};virtual ~RetMsgFactory(){};virtual AbstractMsgLogin* CreateMsgLogin();virtual AbstractMsgLogout* CreateMsgLogout();};AbstractMsgLogin* ReqMsgFactory::CreateMsgLogin(){ return new MsgLoginReq();}AbstractMsgLogout* ReqMsgFactory::CreateMsgLogout(){ return new MsgLogoutReq();}AbstractMsgLogin* RetMsgFactory::CreateMsgLogin(){ return new MsgLoginRet();}AbstractMsgLogout* RetMsgFactory::CreateMsgLogout(){ return new MsgLogoutRet();}#endif // __ABSTRACTFACTORY_H__
以下是试用代码,这里才能清楚一些:
ReqMsgFactory* pReqMsgFactory = new ReqMsgFactory;AbstractMsgLogin* pMsgLoginReq = pReqMsgFactory->CreateMsgLogin();AbstractMsgLogout* pMsgLogoutReq = pReqMsgFactory->CreateMsgLogout();RetMsgFactory* pRetMsgFactory = new RetMsgFactory;AbstractMsgLogin* pMsgLoginRet = pReqMsgFactory->CreateMsgLogin();AbstractMsgLogout* pMsgLogoutRet = pReqMsgFactory->CreateMsgLogout();
OK,到目前为止,我有两个消息构造器,一个构造请求消息,一个构造响应消息。以此类推下去,我在构造器的工厂类中定义很多消息的构造方法类似于CreateMsgXXX()。
以下摘录《设计模式:可复用面向对象软件的基础》关于AbstractFactory的文字:
工厂(Factory)和产品(Product)是Abstract Factory (3.1) 模式的主要参与者。该模式描述了怎样在不直接实例化类的情况下创建一系列相关的产品对象。它最适用于产品对象的数目和种类不变,而具体产品系列之间存在不同的情况。我们通过实例化一个特定的具体工厂对象来选择产品系列,并且以后一直使用该工厂生产产品对象。我们也能够通过用一个不同的具体工厂实例来替换原来的工厂对象以改变整个产品系列。抽象工厂模式对产品系列的强调使它区别于其他只与一种产品对象有关的创建性模式。
- 设计模式 每日一例 Abstract Factory
- 设计模式一:abstract factory
- 设计模式(Abstract Factory)
- 设计模式-Abstract Factory
- 设计模式--Abstract Factory
- 设计模式一(Factory Method,Abstract Factory)
- 设计模式-Abstract Factory模式
- 设计模式 - Abstract Factory模式
- 设计模式--Abstract Factory模式
- 设计模式 一 抽象工厂Abstract Factory(工厂模式)
- Delphi设计模式-Abstract Factory
- [.NET 设计模式] Abstract Factory
- 设计模式之: abstract factory
- 设计模式之Abstract Factory
- 设计模式之ABSTRACT FACTORY
- [设计模式]4. Abstract Factory
- 设计模式(一) 抽象工厂(Abstract Factory)
- Python与设计模式(一)——Abstract Factory
- OpenCV统计应用-直方图比较
- php 常用函数
- XMLHTTPRequest
- Matlab C Library Call Matlab Built-In Functions
- Could not open ServletContext resource [/WEB-INF/applicationContext.xml]解决
- 设计模式 每日一例 Abstract Factory
- Multithreading & Concurrent Programming
- 2011.11.16 I2c-s3c2440.c 分析
- 【WIN08R2 Active Directory】之二 部署企业中Windows Server 2008 R2额外域控制器
- php Mcrypt
- 文件路径的获取方法
- Linux网络编程socket错误分析
- Android开发Info类总结
- 我对共享存储区的理解