Java Design Pattern Rookie series (vii) Modeling and implementation of command pattern

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/lhy_ycu/article/details/39804057


Command mode: Encapsulates a request (command/password) into an object to parameterize its objects with different requests, queues, or logs. The command mode also supports undo operations. The purpose of the command pattern is to achieve decoupling between the issuer and the executor of the command, and to implement separate requests and executions.


First, UML modeling


Second, the code implementation:

/** * Example: Take me to the restaurant for example, divided into 3 steps * * 1, Hachao said, to a Kung Pao chicken--and the customer issued a password * 2, small second: Kung Pao diced chicken. The command was then passed on to the chef. -and the password is delivered to the chef * 3, then the chef starts to make Kung Pao chicken. -Chef According to the password to execute * * FROM these 3 steps can be seen, Kung Pao chicken is not I want to eat, I do, but communicate to others to do. * What I want is a result-kung Pao chicken This dish is ready, and I don't have to relate to how the dish is done. */interface Command {/** * password execution */public void execute ();/** * Password Revocation */public void undo ();} /** * Password--pass */class Ordercommand implements Command {private cookreceiver cook;public Ordercommand (Cookreceiver Cook {This.cook = Cook;} @Overridepublic void Execute () {cook.cooking ();} @Overridepublic void Undo () {cook.uncooking ();}} /** * Chef--true password performer */class cookreceiver {public void cooking () {System.out.println ("Start fry Kung Pao chicken ...");} public void uncooking () {System.out.println ("Do not fry Kung Pao chicken ...");}} /** * Customer--The true password issuer */class customer {private command command;public Customer (Command command) {this.command = command;} /** * Separate the issuing of the command from execution */public void order () {Command.Execute ();} public void UnOrder () {Command.undo ();}} /** * Client Test class * * @author Leo */public class Test {public statIC void Main (string[] args) {/** * Waiting for the password executor--stir-fry have to have a chef. */cookreceiver receiver = new Cookreceiver ();/** * Wait for the password to be communicated to the chef For the customer what food is not known, but the password is always communicated to the chef's ears this is sure. */command cmd = new Ordercommand (receiver); Customer customer = new Customer (cmd);/** * Execute password */customer.order ();/** * Revoke password */customer.unorder ();}}

Third, the application scenario

Restaurant ordering, remote control, queue request, log request.


Four: summary

As you can see from the example above, the command pattern decouples the "requestor of the action" from the "Performer of the Action" object, which is the benefit of encapsulating the invocation of the method.




Java Design Pattern Rookie series (vii) Modeling and implementation of command pattern

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.