多态性建议, [英] polymorphism advice,

查看:74
本文介绍了多态性建议,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组继承自Brush的画笔类。它们覆盖了纯粹的

虚拟绘画方法。当用户选择一个新画笔时,我只有一个

画笔指针指向特定选定类型画笔的实例。


问题在于一些画笔设置不适用于各种画笔。

因此很难用抽象的

画笔指针设置特定画笔属性。我认为有两种方法可以解决这个问题:


添加一个虚拟的集合采用AllBrushProperties结构的函数

(根据画笔不会使用某些属性),这样每个画笔

都可以正确更新。


使用运行时类型检查和向下转换类型来调用相应的

设置方法。


我不是很喜欢它那种废墟我原本干净了

多态系统。

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.

推荐答案

vsgdp写道:

[ snip]
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:



旧问题,新形式。之所以出现是因为你的设计并未考虑到b $ b。当设计一个派生类时,它必须是

能够填补基类留下的洞。以下是一个例子:


所有的鸟都飞。

鸵鸟是一只鸟。

鸵鸟不会飞。


你必须放弃一个(或多个)才能使它工作。


- 也许不是所有的鸟都飞。 制鸟飞界面可以定义当鸟不能飞行时会发生什么。或者可能有

例外。

- 也许鸵鸟不是鸟。可能你有两个类别

来自proto-bird。 Proto-bird没有定义飞行。

Flighted-bird是一种飞行的原型鸟。非飞鸟是一只不会飞的原始鸟,而是鸵鸟,而鸵鸟也不适合。

- 鸵鸟可能会飞。 (打电话给航空公司。)

袜子

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


vsgdp写道:
vsgdp wrote:

我有一组继承自Brush的画笔类。它们覆盖了纯粹的

虚拟绘画方法。当用户选择一个新画笔时,我只有一个

画笔指针指向特定选定类型画笔的实例。


问题在于一些画笔设置不适用于各种画笔。

因此很难用抽象的

画笔指针设置特定画笔属性。我认为有两种方法可以解决这个问题:


添加一个虚拟的集合采用AllBrushProperties结构的函数

(根据画笔不会使用某些属性),这样每个画笔

都可以正确更新。


使用运行时类型检查和向下转换类型来调用相应的

设置方法。


我不是很喜欢它那种废墟我的其他清洁

多态系统。
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.



这不是一个特定于C ++的问题,而是一个OO问题。

您可以尝试comp.object或类似的。


干杯! --M

This is not really a C++-specific question so much as an OO question.
You might try in comp.object or similar.

Cheers! --M


" vsgdp" < he *** @ null.comwrote ...
"vsgdp" <he***@null.comwrote...

>我有一组继承自Brush的画笔类。它们覆盖了纯粹的虚拟绘画方法。当用户选择一个新画笔时,我只需要一个画笔指针指向特定选定类型刷子的实例。


问题在于一些画笔设置不适用于各种类型的刷子。
>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"?


我看到两种方式处理这个:


添加一个虚拟的集合采用AllBrushProperties结构的函数

(根据画笔不会使用某些属性),这样每个画笔

都可以正确更新。


使用运行时类型检查和向下转换类型来调用相应的

设置方法。


我不是很喜欢它那种废墟我的其他清洁

多态系统。
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"?你确定吗?你知道,例如,从String类派生一个PhoneNumber类可以说有很多东西。在我的书中,公共

继承对那些人来说不是正确的。电话号码虽然可以用字符串表示,但不是真正的*扩展名*。看来,你通过玫瑰色眼镜看你的多态系统。


另一种可能性是拥有*多态* BrushPropertySetter类

并让每个Brush后代都呈现指向其自己的Setter的指针。我不知道(还)你有哪些不常见的属性,但每个刷子专用的setter都可以向用户展示自己的界面,

然后用户设置某些属性,这些属性将用于更新

正确的刷子(或者类似的东西)。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复热门回复,请不要问

"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


这篇关于多态性建议,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆