这种情况是否适用于多态函数 [英] Is this scenario a good use for polymorphic functions

查看:66
本文介绍了这种情况是否适用于多态函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个服务器,它接收一系列不同的

消息。有大约12种不同的消息类型,所以我认为

一个好主意是为每种消息类型设计一个类。

然后在我的基类中我例如有纯虚函数

称为例如PerformAction。然后在每个消息类中我实现

PerformAction。


基类计算出消息的类型然后调用

正确的继承类函数。例如,如果该函数是一个blah

消息,它只是用消息Blah响应。然后基础

类会调用Blah类的PerformAction函数,这将会产生一个消息响应。


这个吗在这种情况下看起来像是一个合理的设计?

I am developing a server which receives a range of different
messages. There are about 12 different message types so I thought
that a good idea would be to devise a class for each message type.
Then in my base class I for example have a pure virtual function
called eg PerformAction. Then in each message class I implement
PerformAction.

The base class works out what the type of message is and then calls
the correct inherited class function. Eg if the function were a blah
message which simply responds with the message "Blah" then the base
class would call the Blah class PerformAction function which would
maybe create a blah message response.

Does this seem like a reasonable design in this scenario?

推荐答案

Angus写道:
Angus wrote:

我正在开发一个服务器,它接收一系列不同的

消息。有大约12种不同的消息类型,所以我认为

一个好主意是为每种消息类型设计一个类。

然后在我的基类中我例如有纯虚函数

称为例如PerformAction。然后在每个消息类中我实现

PerformAction。


基类计算出消息的类型然后调用

正确的继承类函数。例如,如果该函数是一个blah

消息,它只是用消息Blah响应。然后基础

类会调用Blah类的PerformAction函数,这将会产生一个消息响应。


这个吗在这种情况下看起来像一个合理的设计?
I am developing a server which receives a range of different
messages. There are about 12 different message types so I thought
that a good idea would be to devise a class for each message type.
Then in my base class I for example have a pure virtual function
called eg PerformAction. Then in each message class I implement
PerformAction.

The base class works out what the type of message is and then calls
the correct inherited class function. Eg if the function were a blah
message which simply responds with the message "Blah" then the base
class would call the Blah class PerformAction function which would
maybe create a blah message response.

Does this seem like a reasonable design in this scenario?



No.


如果基类知道所有消息类型并且可以知道什么是

函数为每个人调用,你根本不需要派生类型

。只需让单个消息类完成它的工作。


这是你需要派生类和多态的方案

行为:


您的处理器 class对类型一无所知,但它可以在消息调度程序注册表中注册
。当注册

时,处理器说它处理什么类型的消息。然后,

调度程序为每种类型创建一个实例映射:


map< message_type,processor_base * registry;


和任何时候消息到来调度程序获取类型并调用

处理器使用它通过在注册表中查找消息

类型获得的指针。 />

这里的区别是(a)处理器在

时间之前是未知的,它们来自一些可能存在或可能不存在的插件,并且

(b)消息类型的数量不一定是静态的,它可以从运行变为
,当新的处理器被开发用于服务时

。新的消息类型。


V

-

请在回复e-时删除资金'A'邮件

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

No.

If the base class is aware of all message types and can know what
function to call for each of them, you don''t need the derived types
at all. Just let the single message class do its job.

Here is the scenario where you''d need derived classes and polymorphic
behaviour:

Your "processor" class knows nothing about the types, but it can
register itself in the message dispatcher registry. When registering
the "processor" says what type of message it processes. The
dispatcher then creates a map of instances for each type:

map<message_type, processor_base*registry;

and any time a message comes the dispatcher gets the type and calls
the processor using the pointer it obtains by looking up the message
type in the registry.

The difference here is (a) the processors are not known ahead of
time, they come from some plug-ins that may or may not exist, and
(b) the number of message types is not necessarily static, it can
change from run to run, when new processors are developed to serve
new message types.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


9月19日凌晨1点11分,安格斯< anguscom ... @ gmail.comwrote:
On Sep 19, 1:11 am, Angus <anguscom...@gmail.comwrote:

我正在开发一个服务器,它接收一系列不同的

消息。有大约12种不同的消息类型,所以我认为

一个好主意是为每种消息类型设计一个类。

然后在我的基类中我例如有纯虚函数

称为例如PerformAction。然后在每个消息类中我实现

PerformAction。


基类计算出消息的类型然后调用

正确的继承类函数。例如,如果该函数是一个blah

消息,它只是用消息Blah响应。然后基础

类会调用Blah类的PerformAction函数,这将会产生一个消息响应。


这个吗在这种情况下看起来像一个合理的设计?
I am developing a server which receives a range of different
messages. There are about 12 different message types so I thought
that a good idea would be to devise a class for each message type.
Then in my base class I for example have a pure virtual function
called eg PerformAction. Then in each message class I implement
PerformAction.

The base class works out what the type of message is and then calls
the correct inherited class function. Eg if the function were a blah
message which simply responds with the message "Blah" then the base
class would call the Blah class PerformAction function which would
maybe create a blah message response.

Does this seem like a reasonable design in this scenario?



Angus


我想你已经采取了合理的方法。我正在理解

关于你的开发是系统处理许多不同的
类型的消息。这种方式可以让你在系统支持更多不同类型的消息时扩展系统。

因为我还没有完全明白你到底是什么做,我相信

不同类型之间的任务和行动将是重要的。如果

这是正确的,当然,你应该为

所有的消息类做一个抽象类。


干杯,

Hi, Angus

I think you''ve made a reasonable approach. What I''m understanding
about your development is the system deals with a number of different
types of messages. That way would allow you to extend the system when
more different types of messages should be supported by the system.
Since I haven''t fully understand what you are really doing, I believe
the tasks and actions between different types would be significant. If
this is right then, of course, you should make an abstract class for
all the message classes.

Cheers,


9月18日上午11:38,Victor Bazarov < v.Abaza ... @ comAcast.netwrote:
On Sep 18, 11:38 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

Angus写道:
Angus wrote:

我是开发一个接收一系列不同的
消息的服务器。有大约12种不同的消息类型,所以我认为

一个好主意是为每种消息类型设计一个类。

然后在我的基类中我例如有纯虚函数

称为例如PerformAction。然后在每个消息类中我实现

PerformAction。
I am developing a server which receives a range of different
messages. There are about 12 different message types so I thought
that a good idea would be to devise a class for each message type.
Then in my base class I for example have a pure virtual function
called eg PerformAction. Then in each message class I implement
PerformAction.


基类计算出消息的类型,然后调用

正确的继承类函数。例如,如果该函数是一个blah

消息,它只是用消息Blah响应。然后基础

类会调用Blah类的PerformAction函数,这可能会产生一个消息响应。
The base class works out what the type of message is and then calls
the correct inherited class function. Eg if the function were a blah
message which simply responds with the message "Blah" then the base
class would call the Blah class PerformAction function which would
maybe create a blah message response.


在这种情况下,这看起来像是一个合理的设计吗?
Does this seem like a reasonable design in this scenario?



No.


如果基类知道所有消息类型并且可以知道什么是

函数为每个人调用,你根本不需要派生类型

。只需让单个消息类完成它的工作。


这是你需要派生类和多态的方案

行为:


您的处理器 class对类型一无所知,但它可以在消息调度程序注册表中注册
。当注册

时,处理器说它处理什么类型的消息。然后,

调度程序为每种类型创建一个实例映射:


map< message_type,processor_base * registry;


和任何时候消息到来调度程序获取类型并调用

处理器使用它通过在注册表中查找消息

类型获得的指针。 />

这里的区别是(a)处理器在

时间之前是未知的,它们来自一些可能存在或可能不存在的插件,并且

(b)消息类型的数量不一定是静态的,它可以从运行变为
,当新的处理器被开发用于服务时

。新的消息类型。


V

-

请在回复e-时删除资金'A'邮件

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


No.

If the base class is aware of all message types and can know what
function to call for each of them, you don''t need the derived types
at all. Just let the single message class do its job.

Here is the scenario where you''d need derived classes and polymorphic
behaviour:

Your "processor" class knows nothing about the types, but it can
register itself in the message dispatcher registry. When registering
the "processor" says what type of message it processes. The
dispatcher then creates a map of instances for each type:

map<message_type, processor_base*registry;

and any time a message comes the dispatcher gets the type and calls
the processor using the pointer it obtains by looking up the message
type in the registry.

The difference here is (a) the processors are not known ahead of
time, they come from some plug-ins that may or may not exist, and
(b) the number of message types is not necessarily static, it can
change from run to run, when new processors are developed to serve
new message types.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask



这与我们如何做的消息非常相似在我们的服务器中处理。

模式本质上是一个工厂。我想到了现代C ++设计书中注册处理器的想法



This is very similar to how we do message processing in our servers.
The "pattern" is essentially a factory. I came across the idea of
registering the processors from the Modern C++ Design book.


这篇关于这种情况是否适用于多态函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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