473,386 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 1195
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
18
by: Ken | last post by:
Hi. Can anyone refer me to any articles about the compatibility between c++ polymorphism and real-time programming? I'm currently on a real-time c++ project, and we're having a discussion...
3
by: E. Robert Tisdale | last post by:
polymorph just means "many form(s)". The definition in plain English http://www.bartleby.com/61/66/P0426600.html and narrower definitions in the context of computer programming ...
11
by: richard pickworth | last post by:
Can anyone explain polymorphism?(very simply). thanks richard
2
by: Josh Mcfarlane | last post by:
I've create a base class for Packages that I am sending via TCP/IP, and then deriving the different transport classes from this base class. Now I can recreate the classes on the other end with...
4
by: LP | last post by:
Hi, I understand the concept/definition of polymorphism. But what does the term "runtime polymorphism" mean? I was asked to define it during a technical interview. I gave a guy vanilla definition...
13
by: Krivenok Dmitry | last post by:
Hello all! Perhaps the most important feature of dynamic polymorphism is ability to handle heterogeneous collections of objects. ("C++ Templates: The Complete Guide" by David Vandevoorde and...
2
by: sarathy | last post by:
Hi all, I need a small clarification reg. Templates and Polymorphism. I believe templates is really a good feature, which can be used to implement generic functions and classes. But i doubt...
11
by: chsalvia | last post by:
I've been programming in C++ for a little over 2 years, and I still find myself wondering when I should use polymorphism. Some people claim that polymorphism is such an integral part of C++,...
17
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.