沟通方式 [英] Communication pattern

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

问题描述



我必须创建一个通信协议实现.
目前,该协议基于RS232,但将来我们希望添加该协议的TCP/IP实现.

现在,我正在寻找实现此目标的最佳方法,因此可以轻松集成新的通信媒体.

有一些有用的设计模式吗?我已经看过Adapter模式,并认为这是一个选择,但是,也许您有更好的主意或文章可供参考.

问候,
Jerry

Hi,

I have to create a communication protocol implementation.
For now, this protocol is based on RS232, but in the future we want to add a TCP/IP implementation of this protocol.

Now I''m looking for the best way of implementing this so adding new communication media is easy to integrate.

Are there some design patterns that are usefull? I have looked to the Adapter pattern and think this is an option but, maybe you have an better idea or article to refer to.

Regards,
Jerry

推荐答案

我不确定我是否了解您,但是您在这里混淆了很多,您正在谈论的是TCP/IP或UDP之类的通信协议,同时您想使用设计模式来实现它!我认为您的担心应该大于此.
I am not sure if I understood you well, but you mixed things up here, you are talking about a communication protocol like TCP/IP or UDP and in the same time you want to implement it with a design pattern !?!! I think your worries should be bigger than that.


我将使用方法SendMessage和事件MessageReceived将更高级别的函数封装在接口中.然后,您可以为所需的任何通信层编写具体的实现.

然后,您可以使用特定于协议的方法(如GetWidgetCount)编写一个使用该接口的类.这就像国家模式.要使用特定的具体实现,只需更改以下行即可:

I would encapsulate the higher level functions in an interface with a method SendMessage and an event MessageReceived. You can then write concrete implementations for whatever communication layers you need.

You can then write a class with methods specific to your protocol, like GetWidgetCount, that use the interface. This is like the State pattern. To use a particular concrete implementation, you would just have to change the line:

ICommunicate communicator = new TcpIpCommunicator();



尼克



Nick


不,我们有一个通讯协议可以与我们的机器进行通讯.现在它正在通过RS232线路工作.

但是,我们必须重构该应用程序,并希望有可能添加另一种通信介质,例如TCP/IP.但是,当然,我们不想在我们的应用程序中具有有关串行线或TCP/IP命令的知识.

添加其他通信媒体时,只有通信层"才有效.

所以这就是为什么我想找到最好的方法来做到这一点.好的设计必须有可能:-)
No, we have an communication protocol to talk with our machines. It''s working over a RS232 line right now.

But, we have to refactor this application and want to have the posibilitie to add another communication medium eg TCP/IP. But ofcource, we do not want to have the knowledge about Serial line or TCP/IP commands in our application.

Only the "Communication Layer" must be infacted when we add another communication media.

So that''s why I want to find the best way of doing this. With a good design it must be possible :-)


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

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