1

this is my first question here :)

I know that I should not check for object type but instead use dynamic_cast, but that would not solve my problem.

I have class called Extension and interfaces called IExtendable and IInitializable, IUpdatable, ILoadable, IDrawable (the last four are basicly the same). If Extension implements IExtendable interface, it can extend itself with different Extension objects.

The problem is that I want to allow the Extension which implements IExtendable to extend only with Extension that implements the same interfaces as the original Extension.

You probably don't unerstand that mess so I try to explain it with code:

class IExtendable
{
public:
 IExtendable(void);

 void AddExtension(Extension*);
 void RemoveExtensionByID(unsigned int);

 vector<Extension*>* GetExtensionPtr(){return &extensions;};
private:
 vector<Extension*> extensions;
};

class IUpdatable
{
public:
 IUpdatable(void);
 ~IUpdatable(void);

 virtual void Update();
};

class Extension
{
public:
 Extension(void);
 virtual ~Extension(void);

 void Enable(){enabled=true;};
 void Disable(){enabled=false;};

 unsigned int GetIndex(){return ID;};

private:
 bool enabled;
 unsigned int ID;

 static unsigned int _indexID;
};

Now imagine the case that I create Extension like this:

class MyExtension : public Extension, public IExtendable, public IUpdatable, public IDrawable
{
public:
    MyExtension(void);
    virtual ~MyExtension(void);

    virtual void AddExtension(Extension*);
    virtual void Update();
    virtual void Draw();
};

And I want to allow this class to extend itself only with Extensions that implements the same interfaces (or less). For example I want it to be able to take Extension which implements IUpdatable; or both IUpdatable and IDrawable; but e.g. not Extension which implements ILoadable. I want to do this because when e.g. Update() will be called on some Extension which implements IExtendable and IUpdateable, it will be also called on these Extensions which extends this Extension.

So when I'm adding some Extension to Extension which implements IExtendable and some of the IUpdatable, ILoadable... I'm forced to check if Extension that is going to be add implements these interfaces too. So In the IExtendable::AddExtension(Extension*) I would need to do something like this:

void IExtendable::AddExtension(Extension* pEx)
{
bool ok = true;
// check wheather this extension can take pEx
// do this with every interface
if ((*pEx is IUpdatable) && (*this is_not IUpdatable))
ok = false;

if (ok) this->extensions.push_back(pEx);

}

But how? Any ideas what would be the best solution? I don't want to use dynamic_cast and see if it returns null... thanks

4

3 回答 3

6

You should rethink your design. If you need this behavior, perhaps introduce IUpdateExtendable and IDrawExtendable.

The reason the current approach is bad is that it violates the Liskov Substitution Principle. You're basically saying "this object acts as an IExtendable, but it really doesn't, unless ...." If a class implements an interface, it really should be usable via that interface without any conditions. If it's not, it is a maintenance nightmare waiting to happen.

于 2010-03-27T18:32:58.070 回答
0

Just an intuition, but depending on what your extensions are doing, the visitor-pattern might help you. You may also want to read up on the decorator pattern.

于 2010-03-27T18:38:47.227 回答
0

I could merge all these interfaces in to one class so I would get something like this:

class Extension
{
public:
Extension(void);
virtual ~Extension(void);

void AddExtension(Extension* pEx){extensions.push_back(pEx);};

virtual void Initialize()
{
for each (Extension* pEx in extensions) pEx->Initialize();
};

virtual void Load()
{
for each (Extension* pEx in extensions) pEx->Load();
};

virtual void Update()
{
for each (Extension* pEx in extensions) pEx->Update();
};

virtual void Draw()
{
for each (Extension* pEx in extensions) pEx->Draw();
};

private:
vector<Extension*> extensions;
};

But this would mean that all of the methods would be called on every added extension. E.g. empty methods in some added extension that doesn't load any content and doesn't draw anything would be called.

于 2010-03-27T21:38:37.490 回答