#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;
}
一个propety pattern的例子
最新推荐文章于 2022-02-23 11:27:23 发布