468,484 Members | 2,076 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

polymorphism advice,

I have a set of brush classes that inherit from Brush. They override a pure
virtual paint method. When the user selects a new brush, I just have a
Brush pointer point to an instance of the particular selected type of brush.

The problem is that some brush settings don't apply to every kind of brush.
So it is difficult to set the particular brush property with the abstract
brush pointer. I see two ways to handle this:

Add a virtual "set" function that takes an AllBrushProperties structure
(some properties will be unused depending on the brush) so that each brush
can update itself correctly.

Use runtime type checking and downcast to the type to call the appropriate
set method.

I don't really like either as it sort of ruins my otherwise clean
polymorphic system.
Jul 27 '06 #1
14 1050
vsgdp wrote:
[snip]
The problem is that some brush settings don't apply to every kind of brush.
So it is difficult to set the particular brush property with the abstract
brush pointer. I see two ways to handle this:
Old problem, new form. It arises because your design has not kept
subsitution in mind. when a derived class is designed, it must be
able to fill in the hole left for a base class. Here's an example:

All birds fly.
Ostrich is a bird.
Ostrich does not fly.

You must give up one (or more) of these to make it work.

- Maybe not all birds fly. The "make bird fly" interface could define
what happens when the bird fails to fly. Or there could be
exceptions.
- Maybe Ostrich is not a bird. Could be you have two categories
that derive from proto-bird. Proto-bird does not define flight.
Flighted-bird is a proto-bird that flies. Non-flying-bird is a
proto-bird
that does not fly, and Ostrich fits under that.
- Ostrich might fly. (Call the airlines.)
Socks

Jul 27 '06 #2
vsgdp wrote:
I have a set of brush classes that inherit from Brush. They override a pure
virtual paint method. When the user selects a new brush, I just have a
Brush pointer point to an instance of the particular selected type of brush.

The problem is that some brush settings don't apply to every kind of brush.
So it is difficult to set the particular brush property with the abstract
brush pointer. I see two ways to handle this:

Add a virtual "set" function that takes an AllBrushProperties structure
(some properties will be unused depending on the brush) so that each brush
can update itself correctly.

Use runtime type checking and downcast to the type to call the appropriate
set method.

I don't really like either as it sort of ruins my otherwise clean
polymorphic system.
This is not really a C++-specific question so much as an OO question.
You might try in comp.object or similar.

Cheers! --M

Jul 27 '06 #3
"vsgdp" <he***@null.comwrote...
>I have a set of brush classes that inherit from Brush. They override a
pure virtual paint method. When the user selects a new brush, I just have
a Brush pointer point to an instance of the particular selected type of
brush.

The problem is that some brush settings don't apply to every kind of
brush.
Like which? And if the brushes are not truly *extending* the base Brush,
why are they deriving from it?
So it is difficult to set the particular brush property with the abstract
brush pointer.
What do you mean by "to set the particular brush"?
I see two ways to handle this:

Add a virtual "set" function that takes an AllBrushProperties structure
(some properties will be unused depending on the brush) so that each brush
can update itself correctly.

Use runtime type checking and downcast to the type to call the appropriate
set method.

I don't really like either as it sort of ruins my otherwise clean
polymorphic system.
"Clean"? Are you sure? You know, there's much to be said about deriving
a PhoneNumber class from a String class, for example. In my book public
inheritance is not the right thing for those. A phone number, although can
be represented by a string, is not a true *extension* of it. It seems that
you are looking at your polymorphic system through rosy glasses.

Another possibility is to have a *polymorphic* BrushPropertySetter class
and have every Brush descendant present a pointer to its own "Setter". I
don't know (yet) what properties you have that are not common, but each
of the brush-specific setters can present its own interface to the user,
and the user then sets certain properties that will be used to update the
correct brush (or something like that anyway).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 27 '06 #4
In article <fW7yg.7277$5K2.1890@fed1read03>, "vsgdp" <he***@null.com>
wrote:
I have a set of brush classes that inherit from Brush. They override a pure
virtual paint method. When the user selects a new brush, I just have a
Brush pointer point to an instance of the particular selected type of brush.

The problem is that some brush settings don't apply to every kind of brush.
So it is difficult to set the particular brush property with the abstract
brush pointer. I see two ways to handle this:

Add a virtual "set" function that takes an AllBrushProperties structure
(some properties will be unused depending on the brush) so that each brush
can update itself correctly.

Use runtime type checking and downcast to the type to call the appropriate
set method.

I don't really like either as it sort of ruins my otherwise clean
polymorphic system.
Ask about this on comp.object
Jul 27 '06 #5
>>I have a set of brush classes that inherit from Brush. They override a
>>pure virtual paint method. When the user selects a new brush, I just have
a Brush pointer point to an instance of the particular selected type of
brush.

The problem is that some brush settings don't apply to every kind of
brush.

Like which? And if the brushes are not truly *extending* the base Brush,
why are they deriving from it?
Basically my brushes paint different kinds of shapes/patterns. So it seemed
to make sense to have them derive from Brush and implement the paint method.
To give you an example, some brushes require a frequency member because they
paint sinusoidal patterns. For some brushes, the strength of the brush
weakens over distance, but not for all.
>So it is difficult to set the particular brush property with the abstract
brush pointer.

What do you mean by "to set the particular brush"?
I mean, if I need to set the frequency of a brush (only certain brushes have
frequency) this is difficult to do with a pointer to the base class.

In retrospect, I think I found a solution. I store an instance of each
brush type in the program, and only the "currently selected" brush is a
pointer to the base class. Therefore, when the user changes the property of
a brush, I can just apply it to the concrete instance with no problem.


Jul 27 '06 #6
"vsgdp" <he***@null.comwrote...
>>>I have a set of brush classes that inherit from Brush. They override a
pure virtual paint method. When the user selects a new brush, I just
have a Brush pointer point to an instance of the particular selected type
of brush.

The problem is that some brush settings don't apply to every kind of
brush.

Like which? And if the brushes are not truly *extending* the base Brush,
why are they deriving from it?

Basically my brushes paint different kinds of shapes/patterns. So it
seemed to make sense to have them derive from Brush and implement the
paint method. To give you an example, some brushes require a frequency
member because they paint sinusoidal patterns. For some brushes, the
strength of the brush weakens over distance, but not for all.
But that's exactly what makes deriving from a common base class pointless.
You can't expect to have such different interfaces in those derived classes
and still derive them from only one base class.
>>So it is difficult to set the particular brush property with the
abstract brush pointer.

What do you mean by "to set the particular brush"?

I mean, if I need to set the frequency of a brush (only certain brushes
have frequency) this is difficult to do with a pointer to the base class.
Of course! That's what makes them break the design (and if the design can
be so easily broken, it's just bad design).
In retrospect, I think I found a solution. I store an instance of each
brush type in the program, and only the "currently selected" brush is a
pointer to the base class. Therefore, when the user changes the property
of a brush, I can just apply it to the concrete instance with no problem.
Sounds good to me.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 27 '06 #7
Victor Bazarov wrote:
"vsgdp" <he***@null.comwrote...
>>Basically my brushes paint different kinds of shapes/patterns. So it
seemed to make sense to have them derive from Brush and implement the
paint method. To give you an example, some brushes require a frequency
member because they paint sinusoidal patterns. For some brushes, the
strength of the brush weakens over distance, but not for all.


But that's exactly what makes deriving from a common base class pointless.
You can't expect to have such different interfaces in those derived classes
and still derive them from only one base class.
Are you saying that every derived class should have the same interface?
Imagine a class Fruit with derived classes Strawberry and Orange

The Oranges class has a method peel();

Is this bad design because Strawberry has not a peel() method?

Or does it become bad design only if you want to use a Fruit pointer and
in some cases only apply the peel() method?

Phil
Jul 28 '06 #8
Philipp wrote:
Victor Bazarov wrote:
>"vsgdp" <he***@null.comwrote...
>>Basically my brushes paint different kinds of shapes/patterns. So
it seemed to make sense to have them derive from Brush and
implement the paint method. To give you an example, some brushes
require a frequency member because they paint sinusoidal patterns. For
some brushes, the strength of the brush weakens over distance,
but not for all.


But that's exactly what makes deriving from a common base class
pointless. You can't expect to have such different interfaces in
those derived classes and still derive them from only one base class.

Are you saying that every derived class should have the same
interface? Imagine a class Fruit with derived classes Strawberry and
Orange
The Oranges class has a method peel();

Is this bad design because Strawberry has not a peel() method?
It is bad design if your Fruit class doesn't have the "cleanme" member
implemented differently in Orange, Strawberry, Cherry, etc., but instead
tries to use "peel" for Orange and "skin" for Peach and "pit" for Cherry.
The system should rely on proper _implemenatation_ and _polymorphic_
behaviour, instead of trying to adapt to whatever derived classes provide.

We're not talking about the situation when your system is designed to deal
with Oranges and it knows how to "peel" them. We're talking of a system
that is supposedly geared towards generic interaction with its pieces.
Or does it become bad design only if you want to use a Fruit pointer
and in some cases only apply the peel() method?
Exactly.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 28 '06 #9
Victor Bazarov posted:
>Are you saying that every derived class should have the same
interface? Imagine a class Fruit with derived classes Strawberry and
Orange
The Oranges class has a method peel();

Is this bad design because Strawberry has not a peel() method?

It is bad design if your Fruit class doesn't have the "cleanme" member
implemented differently in Orange, Strawberry, Cherry, etc., but instead
tries to use "peel" for Orange and "skin" for Peach and "pit" for Cherry.
The system should rely on proper _implemenatation_ and _polymorphic_
behaviour, instead of trying to adapt to whatever derived classes
provide.
Indeed, perhaps something like:

#define override virtual

class Fruit {
public:

virtual PrepareForConsumption() {}
};

class Orange : public Fruit {
public:
override PrepareForConsumption()
{
/* Peel */
}
};

class Cherry : public Fruit {
public:
override PrepareForConsumption()
{
/* Pit */
}
};

--

Frederick Gotham
Jul 28 '06 #10
Frederick Gotham wrote:
Victor Bazarov posted:
>>Are you saying that every derived class should have the same
interface? Imagine a class Fruit with derived classes Strawberry and
Orange
The Oranges class has a method peel();

Is this bad design because Strawberry has not a peel() method?

It is bad design if your Fruit class doesn't have the "cleanme"
member implemented differently in Orange, Strawberry, Cherry, etc.,
but instead tries to use "peel" for Orange and "skin" for Peach and
"pit" for Cherry. The system should rely on proper _implemenatation_
and _polymorphic_ behaviour, instead of trying to adapt to whatever
derived classes provide.


Indeed, perhaps something like:

#define override virtual

class Fruit {
public:

virtual PrepareForConsumption() {}
RVT is missing..

virtual bool PrepareForConsumption() = 0;

I'd keep it pure.
};

class Orange : public Fruit {
public:
override PrepareForConsumption()
override bool PrepareForConsumption()
{
/* Peel */
}
};

class Cherry : public Fruit {
public:
override PrepareForConsumption()

override bool PrepareForConsumption()
{
/* Pit */
}
};
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 28 '06 #11

Victor Bazarov wrote:
Frederick Gotham wrote:
Victor Bazarov posted:
>Are you saying that every derived class should have the same
interface? Imagine a class Fruit with derived classes Strawberry and
Orange
The Oranges class has a method peel();

Is this bad design because Strawberry has not a peel() method?

It is bad design if your Fruit class doesn't have the "cleanme"
member implemented differently in Orange, Strawberry, Cherry, etc.,
but instead tries to use "peel" for Orange and "skin" for Peach and
"pit" for Cherry. The system should rely on proper _implemenatation_
and _polymorphic_ behaviour, instead of trying to adapt to whatever
derived classes provide.

Indeed, perhaps something like:

#define override virtual

class Fruit {
public:

virtual PrepareForConsumption() {}

RVT is missing..

virtual bool PrepareForConsumption() = 0;

I'd keep it pure.
};

class Orange : public Fruit {
public:
override PrepareForConsumption()

override bool PrepareForConsumption()
{
/* Peel */
}
};

class Cherry : public Fruit {
public:
override PrepareForConsumption()


override bool PrepareForConsumption()
{
/* Pit */
}
};
Suppose you had:
Class PeelableFruit : public Fruit

Class Pitable Fruit : public Fruit

Then derive lemon, orange, and banana from PeelableFruit, no? This
could be a way to have several (but not all) subclasses of Fruit that
need peeling, and several others than need pitting.

Jul 28 '06 #12

shadowman...@comcast.net wrote:
Suppose you had:
Class PeelableFruit : public Fruit

Class PitableFruit : public Fruit
sorry should be:

class PeelableFruit : public Fruit
class PitableFruit : public Fruit

Jul 28 '06 #13
sh**********@comcast.net wrote:
[..]
Suppose you had:
Class PeelableFruit : public Fruit

Class Pitable Fruit : public Fruit

Then derive lemon, orange, and banana from PeelableFruit, no? This
could be a way to have several (but not all) subclasses of Fruit that
need peeling, and several others than need pitting.
That is a good solution if your system is able to recognize those two
additional abstractions.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 28 '06 #14
Victor Bazarov posted:
virtual bool PrepareForConsumption() = 0;

I'd keep it pure.

The reason I opted to provide an empty function (as opposed making it
"pure") is so that it would be very simple to implement a fruit which
required no preparation for consumption, such as a strawberry:

class Fruit {
public:

virtual void PrepareForConsumption() {}
};

class Strawberry : public Fruit {};

int main()
{
Strawberry obj;
}
Let's not drag out this example to far... people tend to lose interest when
the example isn't realisitic.

--

Frederick Gotham
Jul 28 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by E. Robert Tisdale | last post: by
11 posts views Thread by richard pickworth | last post: by
2 posts views Thread by Josh Mcfarlane | last post: by
4 posts views Thread by LP | last post: by
13 posts views Thread by Krivenok Dmitry | last post: by
2 posts views Thread by sarathy | last post: by
11 posts views Thread by chsalvia | last post: by
17 posts views Thread by Bart Friederichs | last post: by
reply views Thread by NPC403 | last post: by
2 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.