My Android application is concerned with communicating with another hardware device over a Bluetooth or USB connection. 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. 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). Is there any particular pattern name for this, and what would be the best kind of class name? Perhaps CommandExecutor
, CommandDispatcher
?