"Design Mode" Re-talk about McDonald's Order Mode--Command

McDonald's, I take you for example in Phase III design mode, contact me privately for money!!!

Summary

  • Command mode is also known as Action or Transaction mode.
  • Command mode encapsulates a request or operation into an object.Command mode allows the system to parameterize the client using different requests; queuing requests or logging requests provides command revocation and recovery capabilities.
  • Command mode is the encapsulation of commands.Command mode divides responsibility for commands from responsibility for executing them and delegates them to different objects.

Pattern structure

Schematic Code

namespace Command mode
{
    class Receiver
    {
        public void Action()
        {
            Console.WriteLine("Operation Successful!");
        }
    }
    abstract class Command
    {
        protected Receiver receiver;
        public Command(Receiver receiver)
        {
            this.receiver = receiver;
        }
        abstract public void Execute();
    }
    class ConcreteCommand:Command
    {
        public ConcreteCommand(Receiver receiver) : base(receiver) { }
        public override void Execute()
        {
            receiver.Action();
        }
    }
    class Invoker
    {
        private Command command;
        public void SetCommand(Command command)
        {
            this.command = command;
        }public void ExecuteCommand()
        {
            command.Execute();
        }    
    }
    class Program
    {
        static void Main(string[] args)
        {
            Receiver r = new Receiver();
            Command c = new ConcreteCommand(r);
            Invoker i = new Invoker();
            i.SetCommand(c);
            i.ExecuteCommand();
            Console.Read();
        }
    }
}
  • Place the recipient of the request (Receiver) in ConcreteCommand, a specific subclass of Command. When the request arrives (Invoker sends an Invoker message to activate the Command object), ConcreteCommand handles the request to the Receiver object for processing.
  • Command mode decouples the object that invokes the operation from the object that knows how to do it.In the structure diagram of Command above, the Invoker object does not know exactly which object is handling the Excute operation.
  • It is easy to add new processing operation objects under Command, and we can do this by creating new subclasses that inherit from Command.
  • Command mode can be combined with Mento mode to support Undo operations.

Use command mode

  • If you need to specify, arrange, and execute requests at different times, you can use command mode to encapsulate these requests as command objects and then queue them
  • It is easy to restore and redo commands by managing command objects if you need to support cancel operations from an optional command mode
  • If you need support to re-execute the operating functions of the system once when the system crashes, you can use the command mode to encapsulate the requests for these operating functions as command objects, and then implement the log commands. After the system recovers, you can retrieve the list of commands from the log and re-execute the functions once again.
  • In systems that require transactions, you can choose the command mode, which provides a way to model transactions. Transaction is the command mode with two names.

Advantage

Commands allow the requesting party and the receiving party to evolve independently.

  • Command mode makes it easy to incorporate new commands into your system
  • Allows the receiving party to decide whether to veto the request.
  • It is easier to design a command queue.
  • Undo and Redo for requests can be easily implemented.
  • Commands can be easily logged when needed.
  • Command mode separates the object requesting an operation from the object knowing how to perform it.
  • Command classes, like any other class, can be modified and extended.

essence

Encapsulate the request.

Example:

McDonald's order form:

Fried chicken burgers are delicious, but please have a good meal!Singles


Chef Commands Receiver

package Command_McDonald;

public class Cook {
	public void Chicken()
	{
		System.out.println("Make a fried chicken");
	}
	public void Hamburger()
	{
		System.out.println("Make a hamburger");
	}
	public void fries() {
		System.out.println("Make a fries");
	}
}

Command abstract interface

package Command_McDonald;

public abstract class Command {
	protected Cook cook_receiver;

	public Command(Cook cook_receiver) {
	 
		this.cook_receiver = cook_receiver;
	}
	abstract public void  Excute_Command(); 
	
}

Chicken command, specific command

package Command_McDonald;

public class Chicken_Command extends Command {
	@Override
	public void Excute_Command() {
		cook_receiver.Chicken();

	}

	public Chicken_Command(Cook cook_receiver) {
		super(cook_receiver);
		// TODO Auto-generated constructor stub
	}

	 

}

package Command_McDonald;

public class Fries_Command extends Command {
	@Override
	public void Excute_Command() {
		cook_receiver.fries();

	}

	public Fries_Command(Cook cook_receiver) {
		super(cook_receiver);
		// TODO Auto-generated constructor stub
	}

	 

}

package Command_McDonald;

public class Hamburger_Command extends Command {
	@Override
	public void Excute_Command() {
		cook_receiver.Hamburger();

	}

	public Hamburger_Command(Cook cook_receiver) {
		super(cook_receiver);
		// TODO Auto-generated constructor stub
	}
}

Cashier, command caller

package Command_McDonald;

import java.util.ArrayList;
import java.util.List;

public class Cashier {
	private List<Command> Command_Undo = new ArrayList<Command>();

	public void add_Command(Command com) {
		Command_Undo.add(com); //Add Command
	}

	public void Undo_Command(Command com) {
		Command_Undo.remove(com); //Remove Command
	}

	public void Notify() // Submit Menu
	{
		for(Command con:Command_Undo)
		{
			con.Excute_Command();//Everything told the back chef to make
		}
	}
}

Tags: Java

Posted on Wed, 17 Jun 2020 21:22:10 -0400 by ecxzqute