委托相关设计

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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值