Home > Java > javaTutorial > Example explanation of command pattern in Java design patterns

Example explanation of command pattern in Java design patterns

黄舟
Release: 2017-08-10 09:58:22
Original
1623 people have browsed it

Command mode is the encapsulation of commands. The basic structure of the command mode class diagram is introduced below. Friends who are interested in the command mode related knowledge of Java design mode should take a look.

Definition : Encapsulates a request into an object, allowing you to parameterize the client with different requests, queue requests or record request logs, and provide command undo and recovery functions.

Type: Behavioral class pattern

Class diagram:

Structure of command pattern

As the name suggests, the command pattern is an encapsulation of commands. First, let’s take a look at the basic structure in the command pattern class diagram:

  • Command class: It is an abstract class , the class declares the command that needs to be executed. Generally speaking, an execute method must be published to execute the command.

  • ConcreteCommand class: The implementation class of the Command class, which implements the methods declared in the abstract class.

  • Client class: The final client calling class.

The functions of the above three classes should be relatively easy to understand. Let’s focus on the Invoker class and the Recevier class.

  • Invoker class: Caller, responsible for calling commands.

  • Receiver class: Receiver, responsible for receiving commands and executing them.

The so-called encapsulation of commands, to put it bluntly, is nothing more than writing a series of operations into a method and then calling it by the client. Reflecting it on the class diagram, you only need A ConcreteCommand class and a Client class can complete the encapsulation of commands. Even further, in order to increase flexibility, you can add a Command class for appropriate abstraction. What are the functions of this caller and receiver?

In fact, you can think about it from another angle: If you simply encapsulate some operations as a command for others to call, how can it be called a pattern? As a behavioral pattern, the command pattern must first achieve low coupling. Low coupling can improve flexibility, and the purpose of adding the two roles of caller and receiver is precisely for this purpose. The general
code of the command mode is as follows:


class Invoker { 
 private Command command; 
 public void setCommand(Command command) { 
  this.command = command; 
 } 
 public void action(){ 
  this.command.execute(); 
 } 
} 
abstract class Command { 
 public abstract void execute(); 
} 
class ConcreteCommand extends Command { 
 private Receiver receiver; 
 public ConcreteCommand(Receiver receiver){ 
  this.receiver = receiver; 
 } 
 public void execute() { 
  this.receiver.doSomething(); 
 } 
} 
class Receiver { 
 public void doSomething(){ 
  System.out.println("接受者-业务逻辑处理"); 
 } 
} 
public class Client { 
 public static void main(String[] args){ 
  Receiver receiver = new Receiver(); 
  Command command = new ConcreteCommand(receiver); 
  //客户端直接执行具体命令方式(此方式与类图相符) 
  command.execute(); 
 
  //客户端通过调用者来执行命令 
  Invoker invoker = new Invoker(); 
  invoker.setCommand(command); 
  invoker.action(); 
 } 
}
Copy after login

We can see through the code that when we call, the execution sequence is first the caller class, and then the command class, and finally the receiver class. In other words, the execution of a command is divided into three steps, and its coupling degree is much lower than encapsulating all operations into a class. This is the essence of the command pattern: the caller of the command Separate from the executor so that neither party has to care about how the other party operates.

Advantages and Disadvantages of Command Mode

First, the command mode has good encapsulation: each command is encapsulated. For the client, it can call the corresponding command if it needs any function without knowing how the command is executed. For example, there is a set of file operation commands: create new files, copy files, and delete files. If these three operations are encapsulated into a command class, the client only needs to know that there are these three command classes. As for the logic encapsulated in the command class, the client does not need to know.

Secondly, the command mode is very scalable. In the command mode, the receiver class generally encapsulates the most basic operations, and the command class encapsulates these basic operations twice. When adding a new command, the writing of the command class generally does not start from scratch. There are a large number of receiver classes to be called, and a large number of command classes to be called. The code reusability is very good. For example, if we need to add a command to cut files during file operations, we only need to combine the two commands of copying files and deleting files, which is very convenient.

Finally, let me talk about the shortcomings of the command mode, that is, if there are many commands, development will be a headache. In particular, many simple commands can be implemented with just a few lines of code. If you use the command mode, no matter how simple the command is, you need to write a command class to encapsulate it.

Applicable scenarios of command mode

For most request-response mode functions, it is more suitable to use command mode, just like command As the mode definition says, the command mode is more convenient for implementing functions such as logging and undoing operations.

Summarize

For the case of one occasion, this is a very tangled issue for all developers. Sometimes, because some changes in demand are foreseen, a certain design pattern is used for the flexibility and scalability of the system, but the foreseen demand does not happen. On the contrary, the unforeseen demand comes. Quite often, when modifying the code, the design patterns used had the opposite effect, causing the entire project team to complain. I believe every programmer has encountered such an example. Therefore, based on the principles of agile development, when we design a program, if it can be solved well without using a certain pattern according to the current needs, then we should not introduce it, because it is not difficult to introduce a design pattern. , we can revamp the system and introduce this design pattern when we really need to use it.

Take the command mode as an example. In our development, the request-response mode function is very common. Generally speaking, we will encapsulate the response operation to the request into a method. This encapsulation Methods can be called commands, but they are not command patterns. Whether or not this design should be raised to the level of a pattern needs to be considered separately, because if the command pattern is used, the two roles of caller and receiver will be introduced, and the logic originally placed in one place will be dispersed into three classes. , when designing, one must consider whether the cost is worth it.

The above is the detailed content of Example explanation of command pattern in Java design patterns. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template