博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设计模式
阅读量:6163 次
发布时间:2019-06-21

本文共 4414 字,大约阅读时间需要 14 分钟。

#ifndef __FACTORY__

#define __FACTORY__
#if 1
#include <map>
#include <string>
using namespace std;
template<typename Product, typename ConcreteProduct>
class ConcreteFactory
{
public:
static Product* CreateProduct()
{
return new ConcreteProduct();
}
};

template<typename Product>

class Creator
{
public:
static Creator& Instance()
{
static Creator<Product> m_instance;
return m_instance;
}
public:
typedef Product* (*CreateProductDelegate)();
typedef map<std::string,CreateProductDelegate> MapRegisterCreatorItem;
template<typename ConcreteProduct>
void registerCreator(const std::string &str)
{
m_RegisterConcreteItem[str] = ConcreteFactory<Product,ConcreteProduct>::CreateProduct;
}
void unregisterAllProduct()
{
MapRegisterCreatorItem::iterator itr = m_RegisterConcreteItem.begin();
m_RegisterConcreteItem.clear();
}
Product * createProduct(const std::string &str)
{
MapRegisterCreatorItem::iterator itr = m_RegisterConcreteItem.find(str);
if (m_RegisterConcreteItem.end() != itr)
{
CreateProductDelegate create = itr->second;
if (NULL != create)
{
return create();
}
}
return 0;
}
private:
Creator(){}
Creator(const Creator&){}
~Creator(){}
private:
MapRegisterCreatorItem m_RegisterConcreteItem;
};

#endif

#endif

 

#ifndef __PRODUCT__

#define __PRODUCT__
#include "Common.h"
#include "TObserver.h"

class IProduct:public IObserver

{
public:
virtual void Process() = 0;
virtual ~IProduct()
{
cout<<"Destroy IProduct"<<endl;
}
};

class CProductA: public IProduct

{
public:
virtual int Notify(char *message)
{
if (NULL != message)
{
cout<<"Product A is Notify:"<<message<<endl;
}
return 0;
}
virtual void Process()
{
cout<<"Product A is Process!"<<endl;
}
virtual ~CProductA()
{
cout<<"Destroy ProductA"<<endl;
}
};
class CProductB: public IProduct
{
public:
virtual int Notify(char *message)
{
if (NULL != message)
{
cout<<"Product B is Notify:"<<message<<endl;
}
return 0;
}
virtual void Process()
{
cout<<"Product B is Process!"<<endl;
}
virtual ~CProductB()
{
cout<<"Destroy ProductB"<<endl;
}
};
class CProductC: public IProduct
{
public:
virtual int Notify(char *message)
{
if (NULL != message)
{
cout<<"Product C is Notify:"<<message<<endl;
}
return 0;
}
virtual void Process()
{
cout<<"Product C is Process!"<<endl;
}
virtual ~CProductC()
{
cout<<"Destroy ProductC"<<endl;
}
};
class CProductD: public IProduct
{
public:
virtual int Notify(char *message)
{
if (NULL != message)
{
cout<<"Product D is Notify:"<<message<<endl;
}
return 0;
}
virtual void Process()
{
cout<<"Product D is Process!"<<endl;
}
virtual ~CProductD()
{
cout<<"Destroy ProductD"<<endl;
}
};
#endif

 

#pragma once

#include "Common.h"
//观察者
class IObserver
{
public:
IObserver(){}
virtual ~IObserver(){}
public:
virtual int Notify(char *message) = 0;
protected:
private:
};
//目标
class ISubject
{
public:
ISubject(){}
virtual ~ISubject(){}
public:
virtual int Notify(char *message)
{
list<IObserver*>::iterator itr = m_observer.begin();
for (; itr != m_observer.end(); itr++)
{
(*itr)->Notify(message);
}
return 0;
}
int Attach(IObserver *obj)
{
m_observer.push_back(obj);
return 0;
}
int Detach(IObserver *obj)
{
m_observer.remove(obj);
return 0;
}
private:
list<IObserver*> m_observer;
};

// Protocol.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include "Product.h"
#include "TSingletons.h"
#include "TFactory.h"
#include "Common.h"
int _tmain(int argc, _TCHAR* argv[])
{
printf("argc:%d\n",argc);
for (int i =0; i < argc; i++)
{
wprintf(_T("argv:%s\n"),argv[i]);
}
//模板工厂模式
typedef Creator<IProduct> ProductCreateor;
ProductCreateor &factory = ProductCreateor::Instance();
factory.registerCreator<CProductA>("ProductA");
factory.registerCreator<CProductB>("ProductB");
factory.registerCreator<CProductC>("ProductC");
factory.registerCreator<CProductD>("ProductD");
IProduct *productA = factory.createProduct("ProductA");
productA->Process();
IProduct *productB = factory.createProduct("ProductB");
productB->Process();
IProduct *productC = factory.createProduct("ProductC");
productC->Process();
factory.unregisterAllProduct();
//观察者模式
static ISubject s = TSingletons<ISubject>::Instance();
s.Attach((IObserver*)productA);
s.Attach((IObserver*)productB);
s.Attach((IObserver*)productC);
s.Notify("haha");
s.Detach((IObserver*)productA);
s.Detach((IObserver*)productB);
s.Detach((IObserver*)productC);
delete productA;
delete productB;
delete productC;
system("Pause");
return 0;
}

 

转载于:https://www.cnblogs.com/chl602774238/p/6279784.html

你可能感兴趣的文章
[转]React 教程
查看>>
拓扑排序介绍
查看>>
eclipse打开工作空间(workspace)没有任务反应
查看>>
使用Sybmol模块来构建神经网络
查看>>
字符串去分割符号
查看>>
WPF中,多key值绑定问题,一个key绑定一个界面上的对象
查看>>
UML类图简明教程
查看>>
java反编译工具(Java Decompiler)
查看>>
Android开发之自定义对话框
查看>>
微信Access Token 缓存方法
查看>>
Eclipsed的SVN插件不能识别之前工作空间的项目
查看>>
Linux 查看iptables状态-重启
查看>>
amazeui学习笔记一(开始使用2)--布局示例layouts
查看>>
c#中lock的使用(用于预约超出限额的流程)
查看>>
ODI基于源表时间戳字段获取增量数据
查看>>
并发容器之CopyOnWriteArrayList(转载)
查看>>
什么是AAC音频格式 AAC-LC 和 AAC-HE的区别是什么
查看>>
原创:goldengate从11.2升级到12.1.2
查看>>
Quartz
查看>>
正则表达式的语法规则
查看>>