I am reading about command pattern and I was trying to figure out its implementation in JDK. I think executor service is an implementation of command pattern. Can any expert please confirm this?
-
It's not directly related to your question but IMHO Its very interesting: https://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns-in-javas-core-libraries?rq=1 – David Pérez Cabrera Aug 04 '17 at 13:08
-
2The Command pattern involves several key components, of which an `ExecutorService` can be viewed as one (the "invoker", in [Wikipedia](https://en.wikipedia.org/wiki/Command_pattern)'s terms). In that view, the role of the "command" is filled by `Callable` implementations. I'd prefer, however, to say that some programs use an `ExecutorService` in implementing the pattern. – John Bollinger Aug 04 '17 at 13:11
-
ExecutorService is a subinterface of Executor. Read more at http://www.dre.vanderbilt.edu/~schmidt/cs892/2017-PDFs/Java-Executor-pt1-pt2.pdf – Fuhrmanator Aug 04 '17 at 21:22
1 Answers
To expand on my comment, no, I would not say that an ExecutorService
constitutes an implementation of the Command pattern, but one could be used as part of an implementation of that pattern. ExecutorService
s themselves lack the most important aspect of the Command pattern: the commands.
The point of the pattern is to encapsulate actions in (usually reusable) objects, in such a way that they are available for use at appropriate times. You can use Callable
s to do that, and then engage an ExecutorService
to actually execute the commands at need. Many uses of ExecutorService
s don't really work like that, though -- the Callable
s that are executed don't have the coherence or generality that I am inclined to associate with a Command. This is, however, a somewhat subjective judgment.
I think a clearer example of the Command pattern is found in Swing's use of javax.swing.Action
s. One implements Action
s (the commands) that perform pieces of work that can be triggered via the application GUI. The actions are decoupled from the code that signals them to perform their work, and in fact each one can be connected to the GUI at multiple points, so that it can be executed via different GUI gestures. Swing implements the Command pattern by providing the Action
interface, components that invoke commands represented as Action
s, and an API for associating Action
s with GUI gestures on particular components.
For example, in a visualization application, an Action
X might provide the behavior of rotating the view one step to the left. That's a coherent, general, and fairly self-contained unit of work. I can then register that (same) object to be invoked in response to one or more keystrokes, menu item selections, and even mouse gestures. This decouples the work to be done from the mechanism(s) for triggering that work, which is the key functional characteristic of the Command pattern.

- 160,171
- 8
- 81
- 157
-
There's further discussion of these subtleties in https://www.dre.vanderbilt.edu/~schmidt/PDF/CommandProcessor.pdf – Fuhrmanator Aug 04 '17 at 21:27