473,237 Members | 1,241 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,237 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 1188
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: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.