In lifeCommandMode: eat at a fast food restaurant
The following is an example of real life in the Part of the content. The example is a little different from the original text. Most of them adopt free translation. In some cases, the translation may be inappropriate.
(Assume that you have a fast food meal. The simplified process is as follows: 1. As a customer, you hand the menu (order) to the waiter (waitress); 2: the waiter (waitress) took the menu (order) and put it at the menu counter (for further processing of the menu ordered by the customer) and said "It's time to pass the menu to the cook" 3: short-order cook prepares your meals according to the menu) Interaction of several roles (The interaction is initiated by the customer. He knows what he wants, that is, he determines what kind of food he wants (create an order ), for example, you say to the waiter, "I want a cheesecake with cheese and a Maltose (? Malt shake) "in this way, the reatorder () method is available. The waiter takes the menu and has the food ordered by the customer. After passing it to the menu counter for processing, she then "pass the menu to the cook (orderup ()" (that is, the orderup () method is called. At this time, the menu shows all the instructions for preparing the meals ordered by the customer. With the help of a description makeburger () on this menu, the cook will make a sandwich and, similarly, make a shake .) Roles and responsibilities A menu (which is formed after the waiter takes the menu to the counter for processing) encapsulates a customer's request, that is, what kind of food is requested. The menu here is regarded as an object, an object that is responsible for requesting the food that the customer wants.Like any object, it can be passed-from the waiter to the menu processing counter, or to the next waiter for another step. This menu has an interface, which has only one orderup () method, which encapsulates all the behaviors of preparing meals required by the customer. This menu also has a reference pointing to the objects that need to cook these meals (in this example, it refers to the cook ). The reason for this encapsulation is that the waiter does not have to know the details in the menu or even who will cook the meals (OK, in the current life, a waiter may be concerned about who else will do the detailed content on the menu, but in this example, he will not consider it like this .); The only thing she needs to do is get the processed menu through the menu processing counter and then say, "Go to the cook! (Order up !)" It is a menu processed by the menu processing counter. The above shows a method: Public void orderup (){ Cookmakeburger (); Cookmakeshake (); } The waiter is responsible for holding the menu and then calling its method. Orderup () . The waiter's work is very simple: Get the original menu of the food he ordered from the customer, continue to serve the customer until she handed the original menu to the menu processing counter, and then call Method Orderup () Prepare meals for customers.As we have discovered, in this example, the waiter actually does not have to worry about what the menu content is or who will cook the food; she only knows that there is a method for the menu processed by the menu processing counter. Orderup ()You can call it for her to complete her task. Now, every day, this waiter's Takeorder ()The method can be parameterized because of the different meals ordered by different customers, but so many customers won't bother the waiter's work, because she knows that all menus support orderup (), and she can call this method whenever she needs to prepare a meal. The cook knows how to cook meals ordered by those customers The cook is the object that truly knows how to cook the meals ordered by those customers.Once the Waiter calls the orderup () method, the cook implements all the methods for cooking. Note that there is a loose coupling between the waiter and the cook: the waiter has a menu, which encapsulates details about the food; she knows to call a method on each menu to make the food ordered by the customer ready. At the same time, the cook gave her a description of the food ordered by the customer from the menu provided by the waiter, which he cares about. He never directly communicates with the waiter. From fast food restaurant to command mode OK. We have spent enough space to describe the roles involved in eating at a fast food restaurant, the relationship between them, and their responsibilities. I believe you have a good understanding of this. Now we will re-analyze this example to map to the command mode. You have the same roles in the club, but the name has changed. Each object corresponds to the role name in the method and command mode.
| Waitress (waiter) |
Invoker |
| Short order cook) |
Cycler |
| Orderup (for chefs) |
Execute () |
| Order (menu, not processed yet) |
Command |
| Customer) |
Client |
| Takeorder (take the original menu from the customer) |
Setcommand () |
Definition of command modeSo far, we have a clear understanding of how classes interact with objects in the command mode. Now we will define the command mode. First, let's take a look at the definition of the official (gof:
| Command modeEncapsulate a request as an object so that you can parameterize the customer with different requests, queue requests or record request logs, and support unrecoverable operations. |
Let's analyze the above definition carefully. We know that a command object encapsulates a request from the client and implements this request by binding a series of corresponding actions to the receiver to receive the request. To achieve this, it wraps behavior and recipient into an object and only exposes a method execute (). When this method is called, it delegates the receiver to execute the corresponding action. From the outside, no other object actually knows which action is executed by the command receiver; they only know whether they have called the execute () method, and their requests will be implemented. We can also see that different requests can be used to parameterize customers. Back to the example above, the waiter is parameterized through different menus in a day. Currently, we have not yet encountered a request queuing or logging request logs, as well as support for unrecoverable operations. We don't have to worry about it. These are all very simple extensions to the Basic command mode. According to the above idea, I wrote a simple code corresponding to this instance in the next article. It is recommended that you check it together to achieve better results.