Definition: The request is wrapped in an object in the form of a command and passed to the calling object. The call object looks for the appropriate object that can process the command and pass the command to the corresponding object, and the object executes the command.
Features:
1. Reduced system coupling.
2. New commands can be easily added to the system.
Applications in enterprise-level development and common frameworks: transactions, action controllers for struts
Example:
public class Demo { public static void main(String[] args) { Reicever receiver = new Reicever(); Command command = new ActualCommand(reicever); Invoker invoker = new Invoker(); invoker.addCommand(command); invoker.call(); }}/** * The real executor of the command may have different command executors for different commands*/class Reicever{ public void action(){ System.out.println("Reicever.action()"); }}/** * Command interface, also to unify the subsequent commands */interface Command{ public void exhaust();}/** * In actual command objects, there may be many different command objects*/class ActualCommand implements Command{ private Reicever receiver; public ActualCommand(Reicever receiver) { this.reicever = receiver; } public void exhaust() { receiver.action(); }}/** * Command issuer*/class Invoker{ private List<Command> commands = new ArrayList<>(); public void addCommand(Command command){ commands.add(command); } public void call(){ for(Command c:commands){ c.excute(); } } }There are very few cases when the command mode is used alone, and it is usually used in combination with the memo mode.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.