dwhitney67
April 25th, 2009, 08:01 PM
I was assigned to peer review some code (and presumably the design as well) the other day at work.
One of the CSCs that I came across appeared to be implementing the Abstract Design Pattern, which although I understand it conceptually, I was unable to accept what I saw in the design/code.
Below is some code I threw together that mimics what I came across:
#include <vector>
#include <iostream>
#include <algorithm>
class Base
{
};
class Interface
{
protected:
virtual ~Interface() {}
virtual void doSomething() = 0;
};
class MyClass : public Base, virtual private Interface
{
public:
virtual ~MyClass() {}
virtual void doSomething() { std::cout << "doing something." << std::endl; }
};
// This part below I made up to demonstrate how the Abstract Design Pattern is being
// used.
//
void myClassDoer(Base* b);
int main()
{
typedef std::vector<Base*> BaseVec;
BaseVec bv;
bv.push_back(new MyClass);
std::for_each(bv.begin(), bv.end(), myClassDoer);
}
void myClassDoer(Base* b)
{
MyClass* mc = static_cast<MyClass*>(b);
mc->doSomething();
}
Now, is it just me, or is this overkill? The Base class has no virtual methods. Thus it is useless should one wish to treat the subclass polymorphically. A hard-cast must take place before the object can be used, thus in essence requiring the application developer to be aware of the context of the class being operated on.
Any thoughts on whether this is a standard practice, or am I just being over-zealous in my desire to nit-pick at this design?
One of the CSCs that I came across appeared to be implementing the Abstract Design Pattern, which although I understand it conceptually, I was unable to accept what I saw in the design/code.
Below is some code I threw together that mimics what I came across:
#include <vector>
#include <iostream>
#include <algorithm>
class Base
{
};
class Interface
{
protected:
virtual ~Interface() {}
virtual void doSomething() = 0;
};
class MyClass : public Base, virtual private Interface
{
public:
virtual ~MyClass() {}
virtual void doSomething() { std::cout << "doing something." << std::endl; }
};
// This part below I made up to demonstrate how the Abstract Design Pattern is being
// used.
//
void myClassDoer(Base* b);
int main()
{
typedef std::vector<Base*> BaseVec;
BaseVec bv;
bv.push_back(new MyClass);
std::for_each(bv.begin(), bv.end(), myClassDoer);
}
void myClassDoer(Base* b)
{
MyClass* mc = static_cast<MyClass*>(b);
mc->doSomething();
}
Now, is it just me, or is this overkill? The Base class has no virtual methods. Thus it is useless should one wish to treat the subclass polymorphically. A hard-cast must take place before the object can be used, thus in essence requiring the application developer to be aware of the context of the class being operated on.
Any thoughts on whether this is a standard practice, or am I just being over-zealous in my desire to nit-pick at this design?