在运行时动态选择类 [英] Selecting a class dynamically at runtime

查看:204
本文介绍了在运行时动态选择类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图想出一个解决方案,根据消息类型在运行时选择处理消息的类。
我知道我可以使用这样的东西

Im trying to come up with a solution where the class that processes a "message" is selected at runtime depending on the message type. I know that i can use something like this

if msg_type = "A"
  MsgAProcessor.execute(message);
else if msg_type = "B"
  MsgBProcessoror = execute(message);
....
....
....

我不想使用上面的方法,因为我不希望代码知道我可以处理的消息类型的任何信息。我希望将来能够为新的消息类型添加新的消息处理器。我现在想的解决方案如下:

I dont want to use the above approach as i dont want the code to know anything about the message types that i could be processing. I want to be able to in the future add a new message processor for a new message type. The solution i am thinking of at the moment is as follows

目前有3个消息处理器

MsgAProcessor
MsgBProcessor
MsgBProcessor

全部三个类有一个名为execute的方法,它将以自己的方式处理消息。我创建了一个名为MsgProcessor的接口,并在接口中添加了execute()方法。

All three classes have a method called execute which will process the message in its own way. I have created an interface called MsgProcessor and added the execute() method in the interface.

现在我很难知道调用者应该调用哪个消息处理器而不必检查消息类型。例如,我不能这样做

Now i am having difficulty in knowing which message processor the caller should call without having to check the message type. For example, i cant do this

MsgProcessor proc = new MsgAprocessor()
proc.execute()

MsgProcessor proc = new MsgAprocessor() proc.execute()

上述内容将被要求在if语句中,因为它需要在找到消息类型后立即调用。我还想避免使用实现类类型进行实例化。

The above will stil be required to be in an if statement as it needs to be called just after finding out the message type. I would also like to avoid instantiating using the implementation class type.

有没有更好的方法来实现同样的目标?

Is there a better way of achieving the same?

我希望能够从接口调用MsgProcessor.execute,并让运行时环境知道要根据消息类型调用哪个实现类。

I want to be able to just call MsgProcessor.execute from the interface and have the runtime environment know which implementation class to call based on the message type.

推荐答案


  1. 有一个接口处理器,它有一个方法execute() 。所有三个MsgProcessors都实现了这一点。

  2. 有一个单独的类 ProcessorControl ,它有一个提交(字符串消息)方法。每当有新消息出现时,您只需执行 ProcessorControl.submit(消息)

  3. 现在,ProcessorControl有一个方法addProcessor(Proccesor proc,String type),它添加了处理器到类型为键的哈希表。因此,现在为每个处理器分配了一个类型。

  4. 在提交方法中,只需获取 hashtable.get(type).execute(proc)

  1. Have an interface Processor that has a method execute(). All your three MsgProcessors implement this.
  2. Have a separate class ProcessorControl that has a submit(String message) method. Whenever a new message comes, you simply do ProcessorControl.submit(message)
  3. Now ProcessorControl has a method addProcessor(Proccesor proc, String type) which adds the processors to a hashtable with type as the key. Hence each processor is now assigned with a type.
  4. In the submit method, just get hashtable.get(type).execute(proc)

这是一个简单的命令模式。

This is a simple command pattern.

这篇关于在运行时动态选择类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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