Reputation: 999
I just start to learn design patterns, one is the command pattern. After reading some materials and some documentations, such as
http://www.oodesign.com/command-pattern.html https://www.tutorialspoint.com/design_pattern/command_pattern.htm
I got the idea of using command pattern for stock buying and selling. The client can first decide which stock he/she would like to sell or buy and then let the agent/broker to invoke the command's execute function. I think this makes sense.
While another 'classic' example is restaurant, which confuses me for quite a while. As a customer, how can a customer know which cook (receiver) will be able to cook the item (soup or grill in the example)? The cook shall be not decided by the customer I think. Can anyone point me out how I should approach this idea?
Thanks!
Upvotes: 0
Views: 1168
Reputation: 12948
Actually, your question is out of the scope of the problem what the command pattern actually tries to solve.
If you are familiar with Java, you can easy have an idea of Command Pattern from Java threads. Actually Thread.run() (not only run()) is command pattern in a nutshell.
The main idea is, if we have some group of objects (concrete Command objects which extends Command interface) which have an important functionality, implement that with a method which is not rigid with any method parameters or types. So that any invoker(CommandHandler) which wants to execute that functionality can actually execute that without knowing what the concrete class is. It can execute someCommandObject.execute();
. Only requirement is to be an instance of Command interface.
In Java threads example, let's think about the JVM/Operating system. You know that any program goes to that level as a thread where the program in execution resides as that thread's process. So that thread executor can executes any threads process by anyThread.start()
, anyThread.sleep()
etc.
In the restaurant example the actual command objects are SoupOrder
, GrillOrder
etc. The CommandHandler is Waiter
. Think about a situation where we introduce another command object called LunchOrder
which is a child of Command
class and implements void execute(){}
. Now you don't have to make any change to the invoker (Waiter
) since it still can call up lunchOrder.execute()
. So the publishers(command objects) and client(invoker) implementation is decoupled. That's the beauty of Command pattern.
You may refer this also. :))
Upvotes: 1
Reputation: 1875
I believe you're not thinking about the restaurant example correctly. A customer doesn't give it's order directly to the cooks, a waitress takes the order to the kitchen and puts it in the queue where the cooks can take an order to make when they're available.
In code, this would look like a shared queue that the waitress adds to, and the cooks are in a continuous loop where they cook something then take the next order that they are able to cook. The command pattern in this example is simply the order that gets transferred from the customer to the kitchen.
Upvotes: 1