Composite设计模式
委托+继承
primitive(单体,非组合物)类实现:
class Component
{
int value;
public:
Component(int val){value=val;}
virtual void add(Component*){}
};
composite(组合物)内部存储的是指针,这样可以保证大小相同,实现如下:
class Composite:public Component
{
vector<Component*> c; //容器
public:
Composite(int val):Component(val){}
void add(Component* elem)
{
c.push_back(elem); //向容器内添加元素
}
...
};
Component实现如下,注意add不能是纯虚函数,因为primitive中不支持重写,所以add是无动作虚函数
class Primitive:public Component
{
public:
Primitive(int val):Component(val);//构造函数
};
Prototype设计模式
用于创建未来才会派生的子类
子类自己创建静态的自己,调用私有构造函数,构造函数中的addPrototype会将指针放入容器
原型被登记到父类,通过原型调用子类clone函数:new自己,制造副本,调用私有构造函数
new自己引发的构造函数:不能放在public,private或protected均可
需要有两个构造函数,并加以区分(参数不同)
#include <iostream>
enum imageType
{
LAST, SPOT
};
class image
{
public:
virtual void draw() = 0;
static Image* findAndClone(imageType);
protected:
virtual imageType returnType() = 0;
virtual Image* clone() = 0;
static void addPrototype(Image *image)
{
_prototypes[_nextSlot++] = image;
}
private:
static Image* _prototypes[10];
static int _nextSlot;
};
// 对静态数据成员进行定义
Image* Image::_prototypes[];
int Image::_nextSlot;
Image* Image::findAndClone(imageType type)
{
for(int i=0;i<_nextSlot;i++)
{
if(_prototypes[i]->returnType()==type)
return _prototypes[i]->clone();
}
}
class LandSatImage: public Image
{
public:
imageType returnType(){return LSAT;}
void draw(){cout<<"LandSatImage::draw"<<_id<<endl;}
Image* clone(){return new LandSatImage(1);} //调用protected构造函数
protected:
LandSatImage(int dummy){_id=_count++;} //哑参数,只是为了区分
private:
static LandSatImage landSatImage; //调用默认构造函数
LandSatImage(){addPrototype(this);}
int _id;
static int _count;
};
LandSatImage LandSatImage::_landSatImage; //注册子类原型
int LandSatImage::_count=1; //定义静态数据成员
class SpotImage::public Image
{
public:
imageType returnType(){return SPOT;}
void draw(){cout<<"SpotImage::draw"<<_id<<endl;}
Image* clone(){return new SpotImage(1);}
protected:
SpotImage(int dummy){_id=_count++;}
private:
SpotImage(){addPrototype(this);}
static SpotImage _spotImage;
int _id;
static int _count;
};
SpotImage SpotImage::_spotImage;
int SpotImage::_count = 1;
//调用实例
const int NUM_IMAGES=8;
imageType input[NUM_IMAGES]={LSAT, LSAT, LSAT, SPOT, LSAT, SPOT, SPOT, LSAT};
int main()
{
Image* images[NUM_IMAGES];
//给定一种图像类型,找到正确的原型,并返回一个clone
for(int i=0;i<NUM_IMAGES;i++)
images[i] = Image::findAndClone(input[i]);//静态成员函数的调用
//证明clone了正确的图像对象
for(ing i=0;i<NUM_IMAGES;i++)
images[i]->draw();
//释放动态内存
for(int i=0;i<NUM_IMAGES;i++)
delete images[i];
}