1024programmer Java Detailed explanation of Java command design pattern

Detailed explanation of Java command design pattern

Pass the request from the client into an object, allowing you to parameterize the client with different requests. Used to decouple “behavior requester” and “behavior implementer” to achieve loose coupling between the two in order to adapt to changes. Separate changing and constant factors.

1. Role
Define the interface of the command and declare the execution method.
The command interface implementation object is a “virtual” implementation; it usually holds the receiver and calls the receiver’s function to complete the operation to be performed by the command.
The receiver is the object that actually executes the command. Any class may become a receiver as long as it can implement the corresponding functions required by the command.
The command object is required to execute the request. It usually holds the command object and can hold many command objects. This is where the client actually triggers the command and requires the command to perform the corresponding operation, which is equivalent to the entrance to the command object.
Create a specific command object and set the receiver of the command object. Note that this is not a client in our conventional sense, but is assembling the command object and receiver. Perhaps it would be better to call this Client an assembler, because the client that actually uses the command triggers execution from the Invoker.

2. Advantages

  • 1. Reduce the coupling between objects.
  • 2. New commands can be easily added to the system.
  • 3. It is easier to design a combined command.
  • 4. Call the same method to implement different functions

3. Disadvantages
Using command mode may result in some systems having too many specific command classes. Because a specific command class needs to be designed for each command, some systems may require a large number of specific command classes, which will affect the use of command patterns.

4. Applicability

  • The system needs to decouple the request caller and the request receiver so that the caller and receiver do not interact directly.
  • The system needs to specify, queue, and execute requests at different times.
  • The system needs to support the undo (Undo) operation and the recovery (Redo) operation of the command.
  • The system needs to group a group of operations together, that is, to support macro commands.

5. Application
Simulates the operations of the TV with commands to turn on, turn off, and change channels. The code is as follows

 //Interface for executing commands
 public interface Command {
 void execute();
 //Command receiverReceiver
 public class Tv {
 public int currentChannel = 0;
 public void turnOn() {
 System.out.println("The televisino is on.");
 public void turnOff() {
 System.out.println("The television is off.");
 public void changeChannel(int channel) {
 This.currentChannel = channel;
 System.out.println("Now TV channel is " + channel);
 //Boot commandConcreteCommand
 public class CommandOn implements Command {
 private Tv myTv;
 public CommandOn(Tv tv) {
 myTv = tv;
 public void execute() {
 //Shutdown commandConcreteCommand
 public class CommandOff implements Command {
 private Tv myTv;
 public CommandOff(Tv tv) {
 myTv = tv;
 public void execute() {
 //Channel switching commandConcreteCommand
 public class CommandChange implements Command {
 private Tv myTv;
 private int channel;
 public CommandChange(Tv tv, int channel) {
 myTv = tv;
  This.channel = channel;
 public void execute() {
 //Can be regarded as a remote control Invoker
 public class Control {
 private Command onCommand, offCommand, changeChannel;
 public Control(Command on, Command off, Command channel) {
  OnCommand= on;
  offCommand = off;
 changeChannel = channel;
 public void turnOn() {
 ​ onCommand.execute();
 public void turnOff() {
 public void changeChannel() {
  ​ changeChannel.execute();
 //Test class Client
 public class Client {
 public static void main(String[] args) {
  // Command receiver Receiver
  Tv myTv = new Tv();
  // Boot command ConcreteCommond
  CommandOn on = new CommandOn(myTv);
  // Shutdown command ConcreteCommond
  CommandOff off= new CommandOff(myTv);
  //Channel switching command ConcreteCommond
  CommandChange channel = new CommandChange(myTv, 2);
  // Command control object Invoker
 Control cOntrol= new Control(on, off, channel);
  ​ // Start up
  ​ control.turnOn();
  //Switch channel
  ​ control.changeChannel();
  ​ // Shut down
  ​ control.turnOff();

Execution results
The televisino is on.
Now TV channel is 2
The television is off.

6. Summary
1. The essence of the command mode is to encapsulate the command and separate the responsibility of issuing the command and the responsibility of executing the command.
2. Each command is an operation: the requesting party issues a request to perform an operation; the receiving party receives the request and performs the operation.
3. The command mode allows the requesting party and the receiving party to be independent, so that the requesting party does not need to know the interface of the party receiving the request, let alone how the request is received, whether and when the operation is executed, and How is it executed.
4. The command mode makes the request itself an object, which can be stored and transferred like other objects.
5. The key to the command mode is the introduction of an abstract command interface, and the sender programs for the abstract command interface. Only specific commands that implement the abstract command interface can be associated with the receiver.

The above is a detailed introduction to Java command design patterns. I hope it will be helpful to everyone’s learning.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/703490

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us


Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top