In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Command mode
Command mode definition: encapsulates a request into an object so that other objects can be parameterized using different requests, queues, or logs. Command mode also supports undoable operations.
Command mode, which encapsulates method calls, decouples the requestor of the action from the executor of the action object. Class diagram, as follows:
Life example: restaurant order (customer, waiter, chef relationship, customer order tells the waiter that you don't have to deal with the cook directly, and the chef begins to cook a big meal after receiving the order from the waiter).
Advantages of command mode:
In ● command mode, the requester does not interact with the receiver directly, that is, the requester does not contain the receiver's reference, so the coupling between them is completely eliminated.
The ● command mode satisfies the "open-close" principle. If you add a new specific command and the recipient of the command, the caller can use the new command object without having to modify the caller's code; on the contrary, if you add a new caller, you don't have to modify the existing specific command and recipient. The new caller can use the specific command he or she already has.
● because the requester is encapsulated in a specific command, then you can save the specific command to a persistent medium and re-execute the specific command when needed. Therefore, log can be recorded by using command mode.
● uses command mode to queue requests from requesters. Each request corresponds to a specific command, so these commands can be executed in a certain order.
The usage scenario of command mode:
● command mode decouples the object that makes the request from the object that executes the request
● is used for queuing requests. Command objects, like general objects, can be passed back and forth. As long as a unified interface method is implemented, it can be called arbitrarily.
● is used for log requests to record all actions in the log so that the system can be called back to recover after a crash.
Case 1: to achieve a home automation remote control, can control lights, air conditioning, and other equipment on / off. The hardware api class is provided by the manufacturer. The class diagram is as follows:
More need to think?
If ● needs to undo the command, how to modify it?
What if ● needs to combine commands, such as (first turn on the light, then turn on the TV, then turn on the water heater, turn on the influence)?
How can ● be implemented if commands need to be executed across jvm and machines?
If ● needs to recover data after downtime, how to achieve it?
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.