23 kinds of design patterns: command mode

Definition: Encapsulate a request as an object, so that you use a different request of the client parameters, the request queue or log requests, and can provide an undo command recovery.

Type: class behavior patterns

Class Diagram:

Structure of command mode

        As the name suggests, it is to command mode command package, first look at the basic structure of the command mode class diagram:

  • Command class: is an abstract class, the class of the command to be executed to declare, to announce a general method used to execute the command.
  • ConcreteCommand class: the Command class implementation class, abstract method declared in the class will be realized.
  • Client class: the final client calls the class.

        These three categories of action should be relatively easy to understand, let's talk about the focus Invoker class and Recevier class.

  • Invoker class: the caller is responsible for calling command.
  • Receiver class: the recipient is responsible for receiving commands and execute the command.

        The so-called package of command, plainly, nothing more than a series of operations wrote a method, and then for the client calls on the line, reflected in the class diagram, only one ConcreteCommand class and Client class can be completed to the command package, even further, in order to increase flexibility, you can add a Command abstract class appropriately, the caller and the receiver in the end is what?

        In fact, you can think about a different angle: If you just simply encapsulate some operations as a command for calling other people, how can it be called a pattern? Command mode as a mode of behavior class, the first to do low coupling, low coupling in order to improve flexibility, while adding the caller and the recipient of two roles purpose is precisely for this purpose. Common code 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();
	}
}

We can see through the code, when we called, the timing of the implementation of the first class is the caller, then the command class, and finally the recipient class. That execution of a command is divided into three steps, it's better than all the coupling operations are encapsulated into a class to be much lower, and this is the essence of the command mode: the caller of the command and actors separately, so that they do not care how the other operates.

 

Advantages and disadvantages of command mode

        First, good encapsulation command mode: each command are encapsulated, for the client, what features need to go call the appropriate command, without knowing specifically how the command execution. For example, there is a set of command file operations: create a new file, copy files, delete files. If these three operations are encapsulated into a command class, the client only needs to know that there are three classes command to, as a class encapsulated command logic, the client will not need to know.

        Second, good scalability, command mode, in command mode, the recipient will generally class operation basic package, the command classes are further encapsulated by these basic operations, the time when adding a new command , to write a command class in general is not starting from scratch, there are plenty of classes available to the recipient to call, there are plenty of available command class called good reusability of code. For example, file operations, we need to add a cut command file, you only need to copy files and delete files combination of both command on the line, very convenient.

        Finally, talk about shortcomings in command mode, that is, if a lot of command, it is necessary to develop a headache. In particular, many simple command, lines of code to implement on a few things, and then use the command mode, do not control more than simple commands, you need to write a command class to encapsulate.

 

Applicable scene command mode

       For most of the request - response mode function, more suitable for use command mode, as defined in command mode say, the command log mode implemented, undo operation more convenient functions.

 

 to sum up

       For an occasion in the end with no pattern, this is a very tangled issue for all developers. Sometimes, because of anticipated some changes will occur on the demand for flexibility and scalability of the system and use some design patterns, but does it happen that the foreseeable needs, on the contrary, did not foresee the need'd come many, resulting in a modified code, the use of design patterns but played the opposite effect, so that the entire project team complaining. Such examples, I believe that every programmer have encountered. Therefore, based on the principles of agile development, we were designing the program, according to current needs, without using a pattern can be a good solution, then we are not to introduce it, because you want to introduce a design pattern is not difficult we could have another look at the system when you really need to use, the introduction of this design pattern.

       Take command mode for instance, we have developed, the request - response mode function is very common, in general, we will package the request in response to the operation of a method, this method can be called encapsulation command, but not command mode. In the end we would want to rise to this design pattern height must be considered separately, because, if you use the command mode, it is necessary to introduce the caller, recipient of two roles, originally placed in a logical dispersed into three classes , the design must consider this price is worth it.

Published 75 original articles · won praise 48 · Views 350,000 +

Guess you like

Origin blog.csdn.net/KingJin_CSDN_/article/details/89461052