Cocos2d-x 简单工厂模式

1. CCWindow 继承自CCLayer
2. 用户创建如LoginWindow继承自CCWindow
3.CCWindowFactory 中:
enum WindowID
{
   LoginWindow,
   RegistWindow,
}
RegistWindow(enum WindowID,Window Class)
{
   //保存在Map中
}

CreateWindow()
{
 //Map迭代器,顺序创建CCWindow子类
}


网上找的简单工厂模式的例子,有爱的Nokia

/* Nokia抽象产品 */
#pragma once
#include <string>
#include <iostream>
class CNokia
{
public:
	CNokia(void);
public:
	virtual ~CNokia(void);
public:
	virtual bool MakeCall(const std::string & number) const = 0;
};
#include "Nokia.h"

CNokia::CNokia(void)
{
}

CNokia::~CNokia(void)
{
}

#pragma once
#include "nokia.h"

class CN96 : public CNokia
{
public:
	CN96(void);
public:
	virtual ~CN96(void);
public:
	virtual bool MakeCall(const std::string & number) const;
};

/* N96型号的Nokia手机产品类 */
#include "N96.h"

CN96::CN96(void)
{
	std::cout << "生产一部N96" << std::endl;
}

CN96::~CN96(void)
{
}

bool CN96::MakeCall(const std::string & number) const
{
	std::cout << "我在用N96,拨打号码是:" <<  number.c_str() << std::endl;
	return false;
}

#pragma once
#include "nokia.h"

class CN95 :public CNokia
{
public:
	CN95(void);
public:
	virtual ~CN95(void);
public:
	virtual bool MakeCall(const std::string & number) const;
};

/* N95型号的Nokia手机产品类 */
#include "N95.h"

CN95::CN95(void)
{
	std::cout << "生产一部N95" << std::endl;
}

CN95::~CN95(void)
{
}

bool CN95::MakeCall(const std::string & number) const
{
	std::cout << "我在用N95,拨打号码是:" <<  number.c_str() << std::endl;
	return false;
}

/*N85, N81, ...*/

/* 工厂类 */
#pragma once
#include "N96.h"
#include "N95.h"
#include "N85.h"
#include "N81.h"
#include <cassert>
class CNokiaSimpleFactory
{
public:
	CNokiaSimpleFactory(void);
public:
	~CNokiaSimpleFactory(void);
public:
         /* 静态工厂方法 */
	static CNokia * CreateNokia(const std::string & model);
};

#include "NokiaSimpleFactory.h"

CNokiaSimpleFactory::CNokiaSimpleFactory(void)
{
}

CNokiaSimpleFactory::~CNokiaSimpleFactory(void)
{
}

CNokia * CNokiaSimpleFactory::CreateNokia(const std::string & model)
{
	if (model == "N96")
	{
		return new CN96();
	}
	else if (model == "N95")
	{
		return new CN95();
	}
	else if (model == "N85")
	{
		return new CN85();
	}
	else if (model == "N81")
	{
		return new CN81();
	}
	else
	{
		assert(false);
	}
	return NULL;
}


/* main方法,可以看做客户端 */
#include "stdafx.h"
#include "NokiaSimpleFactory.h"

int _tmain(int argc, _TCHAR* argv[])
{
	CNokia * nokia = NULL;
        /* 
          modeName 可以从外部XML文件中读取,运行过程中动态的
            决定该创建哪一种型号的手机
         */
	std::string modeName = "N96";
	nokia = CNokiaSimpleFactory::CreateNokia(modeName);
	nokia->MakeCall("123456789");
        delete nokia;
	nokia = NULL;
	return 0;
}



你可能感兴趣的:(C++,简单工厂模式,cocos2d-x)