开发者

Container containing Objects and Containers

I'm trying to create a structure that will will be iterated across and the objects stored in it will be drawn in order from left to right. But there will be containers that are stored alongside of the drawable objects. These containers I consider them as drawing stacks where drawable objects are drawn on top of each other. I have some rough psuedo code written up but I can't come up with a common interface to have both my container and drawable object inherit from. My goal is a common interface between the containers and the drawable objects. I figured I could do it by just inserting containers even if they only have one drawable object inside them, I feel this just takes up more memory and decreases the speed. I also don't would like to avoid casting objects to get the correct calls. Could someone suggest on what I should do, I'm not the most advanced programmer but I do strive for speed and code reduction. Here is what I have so far (I know some of the interface calls don't match the dependents i just don't know what to):

class IDrawable {
  protected:
    signal_t sig; // this is an enumeratio开发者_Python百科n
  public:
    void paint(QPainter &painter); // This functionality will be defined here since it is painting to a pixmap

    virtual void reset() = 0;
    virtual void init() = 0; // Setup code will go here
    virtual void update(float) = 0;
};

class Signal : public virtual IDrawable {
  private:
    bool state; // By default this will be false
  public:
    Signal();
    virtual ~Signal();
    // Parameters for update is 0 or 1, since the graphic is just toggled on or off
    virtual void update(float) = 0; // Drawing happens here. Will draw to pixmap
    bool state() {return state;}
};

class Gage : public virtual IDrawable {
  private:
    float cur_val;
  public:
    Gage();
    virtual ~Gage();
    virtual void init() = 0; // Setup code will go here
    virtual void update(float) = 0; // Drawing happens here. Will draw to pixmap
};

class TextGage : public virtual Gage {
  public:
    TextGage();
    virtual ~TextGage();
    void update(float); // Define update functionality here
};

// a stack can coexist with a Gage object in a container
class DrawableStack : public virtual IDrawable {
  private:
    QMap<signal_t, IDrawable*> *Stack;
  public:
    DrawableStack();
    virtual ~DrawableStack();

    void init() {
        Stack = new QMap<signal_t, IDrawable*>();
    }

    void update(signal_t,float); // Drawing happens here. Will draw to pixmap

    void setStack(QMap<IDrawable*> *gageStack) {
        Stack = gageStack;
    }

    void push(IDrawable* node) {
        Stack.insert(node->sigType, node);
    }
};


This is the classical Composite design pattern you need to apply:

http://en.wikipedia.org/wiki/Composite_pattern


I think, what you are looking for is the Composite Pattern...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜