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