Re: Design problem with inheritance
- From: "Alvin Ryder" <alvin321@xxxxxxxxxxx>
- Date: 13 Jul 2006 14:31:51 -0700
srinivasarao_moturu@xxxxxxxxx wrote:
class ABC
{
public :
virtual void operation1();
virtual void operation2();
virtual int GetValue();
virtual char GetValue();
virtual void SetValue(int);
virtual void SetValue(char);
}
class intABC : public ABC
{
public :
virtual void operation1();
virtual void operation2();
void SetValue(int);
int GetValue();
private:
int val;
}
class charABC : public ABC
{
public :
virtual void operation1();
virtual void operation2();
void SetValue(char);
char GetValue();
private :
char val;
}
class Controler
{
public :
//constructors and destructors
void somefunc() //this function handles container member
private :
vector<ABC*> val;
}
client Controler class manipulates ABC derived classes polymorphically
I feel, In the above design surely it is not following Lispov
substitution principle.
here ABC is a fatty interface since intABC does not require char
version of get/set membersand charABC does not require int version of
get/set members.
If I remove Get/Set members from ABC class and put int versions in
intABC and char versions in charABC then I have to use downcasting to
call specific versions.
so is there a good design to remove fatty interface from the ABC and at
the same time i should not use downcasting and another constraint is I
should use ABC polymorphically?
With Regards,
Sri.
I would not use inheritence in this way.
1. When you need to vary types use generics or templates if your
language supports them.
2. Otherwise, if there is an object root, like Java's "Object", you can
cast to Integer and Char from it. I've heard it said "avoid casts" but
that's how Sun did it. C++ libs may or may not have such a root though.
3. Failing that, back in the C days, we had to use "void *" with casts
to whatever type was required, it worked well.
Cheers.
.
- Follow-Ups:
- Re: Design problem with inheritance
- From: Dmitry A. Kazakov
- Re: Design problem with inheritance
- References:
- Design problem with inheritance
- From: srinivasarao_moturu
- Design problem with inheritance
- Prev by Date: Re: Poly Couples
- Next by Date: Re: Observer pattern limitations
- Previous by thread: Re: Design problem with inheritance
- Next by thread: Re: Design problem with inheritance
- Index(es):
Relevant Pages
|