简体   繁体   English

我的课程是“命令”和“调度程序”模式的示例吗?

[英]Are my classes examples of “Command” and “Dispatcher” patterns?

My Android application is concerned with communicating with another hardware device over a Bluetooth or USB connection. 我的Android应用程序与通过蓝牙或USB连接与另一个硬件设备进行通信有关。 To communicate with that hardware device, it uses a command-response protocol. 为了与该硬件设备进行通信,它使用了命令响应协议。 Each kind of command type has its own expected length of response, time-out value, what has got to be done with the response that comes back, and other parameters that are unique to each kind of command. 每种命令类型都有自己的预期响应长度,超时值,返回的响应必须执行的操作以及每种命令所独有的其他参数。

I decided to encapsulate everything about each type of command into a Command object, which is subclassed by each kind of Command to create concrete implementations. 我决定将有关每种命令的所有内容封装到一个Command对象中,该对象由每种Command子类化以创建具体的实现。 These objects have methods that are called to provide the initial command (series of bytes), a method to call with the response that comes back, and so on. 这些对象具有被调用以提供初始命令(字节序列)的方法,用于调用返回的响应的方法等等。 For me this is elegant because everything about a particular command to the external hardware device, from what to send initially to how to process the response, what to do on errors, etc. is all encapsulated in a class. 对我来说,这很优雅,因为有关特定命令到外部硬件设备的所有信息(从最初发送的内容到如何处理响应,如何处理错误等)都封装在一个类中。

Is this an example of "command pattern"? 这是“命令模式”的示例吗?

Secondly, I have a class that is concerned with holding such Command objects in a queue and sequentially executing them (that is, sending out their command bytes via Bluetooth / USB, and calling methods on that Command to handle the response, errors, and so forth). 其次,我有一个类与将此类Command对象保持在队列中并顺序执行它们有关(即,通过Bluetooth / USB发送它们的命令字节,并在该Command上调用方法来处理响应,错误等)。向前)。 Is there any particular pattern name for this, and what would be the best kind of class name? 有没有特定的模式名称,最好的类名称是什么? Perhaps CommandExecutor , CommandDispatcher ? 也许CommandExecutorCommandDispatcher

Yes, this sounds like a standard implementation of the Command Pattern. 是的,这听起来像命令模式的标准实现。 The pattern is described eg in the book Design Patterns by the "Gang of Four". 例如在“四人帮”中的“ 设计模式 ”一书中描述了该模式 If you have no hardcopy at hand, you can find it online eg on Wikipedia or Blackwasp . 如果您手边没有印刷本,则可以在线找到它,例如在WikipediaBlackwasp上

Your CommandExecutor / CommandDispatcher seem to be Invoker s in the Pattern terminology. 在Pattern术语中,您的CommandExecutor / CommandDispatcher似乎是Invoker How the Invoker consumes the Commands is an implementation detail, a FIFO queue is just one way. 调用方如何使用命令是实现细节,FIFO队列只是其中一种方法。 So there is no specific name for this. 因此,没有特定的名称。 But you could use another Design Pattern for this, if it matches your needs, eg a scheduler with or without parallel execution of the commands (see also Active Object ). 但是,如果满足您的需要,则可以使用其他设计模式,例如,带有或不带有并行执行命令的调度程序(另请参见Active Object )。

Hint: If you take a look at the Pattern explanations, you will see that you have merged the Receiver s and Command s into one class, since your Commands know the business logic to execute (=sending bytes, process response, etc.). 提示:如果查看一下Pattern的说明,您会发现您已经将ReceiverCommand合并到一个类中,因为Commands知道要执行的业务逻辑(=发送字节,流程响应等)。 So you can improve your design if you put this business logic into specific Receiver classes, which are used by the Commands. 因此,如果将此业务逻辑放入命令使用的特定Receiver类中,则可以改进设计。 This way the Command Pattern is just a layer which uses the Receivers/business logic. 这样,命令模式只是使用接收器/业务逻辑的一层。 This enables better testing of the business logic without taking care of the Commands as well as makes the Command Pattern exchangeable. 这可以更好地测试业务逻辑,而无需照顾命令,并且可以互换命令模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM