一个propety pattern的例子

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <vector>
using std::vector;

#include <cassert>

#include <conio.h>

enum imageType
{
	LSAT, SPOT
};

class Image
{
protected:
	Image();
	virtual ~Image();
	Image( const Image& rhs);
	Image& operator = (const Image & rhs);
public:
	virtual void PrintInfo() const =0;
	static Image *findAndClone(imageType);
public:
	virtual imageType returnType() const = 0;
	virtual Image *clone() const = 0 ;
	// As each subclass of Image is declared, it registers its prototype
	static void addPrototype(Image *image)
	{
		assert(image);
		_prototypes.push_back(image);
	}
private:
	// addPrototype() saves each registered prototype here
	static vector<Image*>_prototypes;
	static int _nextSlot;
};



vector<Image*> Image::_prototypes;
int Image::_nextSlot;

Image::Image()
{

}
Image::~Image()
{

}

Image::Image(const Image& rhs)
{

}


// Client calls this public static member function when it needs an instance
// of an Image subclass
Image *Image::findAndClone(imageType type)
{
	for(int i = 0; i <_prototypes.size(); i++)
	{
		if(_prototypes[i]->returnType() == type )
		{
			return _prototypes[i]->clone();
		}
	}
}



class LandSatImage: public Image
{
public:
	LandSatImage();
	~LandSatImage();
protected:
	LandSatImage( int nDumy);
	LandSatImage(const LandSatImage& rhs);
	LandSatImage& operator = (const LandSatImage& rhs);
public:
	virtual Image* clone() const;
private:
	static int id;
	static LandSatImage _landSatImage;
public:
	imageType returnType() const
	{
		return LSAT;
	}
	void PrintInfo() const
	{
		_tprintf(_T("LandSatImate: %d\r\n"), id);
	}
};

int LandSatImage::id =0;
LandSatImage LandSatImage::_landSatImage(1);

LandSatImage::LandSatImage()
{
}

LandSatImage::~LandSatImage()
{

}

LandSatImage::LandSatImage(int nDumy)
{
	addPrototype(this);
}

LandSatImage::LandSatImage(const LandSatImage& rhs)
{
	id++;
}

Image* LandSatImage::clone() const
{
	return new LandSatImage(*this);
}


class SpotImage:public Image
{
public:
	SpotImage();
	~SpotImage();
protected:
	SpotImage(int nDumy);
	SpotImage(const SpotImage& rhs);
	SpotImage& operator = (const SpotImage& rhs);
public:
	virtual Image* clone() const;
private:
	static int id;
	static SpotImage _spotImage;
public:
	imageType returnType() const
	{
		return SPOT;
	}
	void PrintInfo() const
	{
		_tprintf(_T("SpotImage: %d\r\n"), id);
	}
};

int  SpotImage::id = 0;
SpotImage SpotImage::_spotImage(1);

SpotImage::SpotImage()
{
}

SpotImage::~SpotImage()
{
}

SpotImage::SpotImage(int nDumy)
{
	addPrototype(this);
}

SpotImage::SpotImage(const SpotImage& rhs)
{
	id++;
}

Image* SpotImage::clone() const
{
	return new SpotImage(*this);
}


// Simulated stream of creation requests
static const int NUM_IMAGES = 8;
imageType input[NUM_IMAGES] = 
{
	LSAT, LSAT, LSAT, SPOT, LSAT, SPOT, SPOT, LSAT
};

int main()
{
	Image* imageArray[NUM_IMAGES]={NULL};
	//clone
	int i;
	for(i = 0; i< NUM_IMAGES; i++)
	{
		imageArray[i] = Image::findAndClone(input[i]);
		if(imageArray[i])
		{
			imageArray[i]->PrintInfo();
		}
	}
	getch();
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值