The command mode is like this in books. It encapsulates a request into an object, so that you can use different requests to parameterize the client, queue requests or record request logs, the command can be revoked or restored.
I personally think the command mode is relatively simple and commonly used, because it is a typical high cohesion and low coupling mode with excellent encapsulation because it separates the requester and the executor. The code with excellent scalability is also very simple. In the example in my adapted book below, I simply changed it to passing through the constructor.
Requirement Group
Package command; public abstract class group {// both parties work separately. If you want to discuss with a group, you must first find this group public abstract void find (); // The public abstract void add () function is required. // The public abstract void Delete () function is required to be deleted. // The public abstract void change () function is required (); // all change plans are required. Public abstract void Plan ();}
Package command; public class codegroup extends group {@ overridepublic void find () {system. out. println ("find code group") ;}@ overridepublic void add () {system. out. println ("add a function") ;}@ overridepublic void Delete () {system. out. println ("delete a function") ;}@ overridepublic void change () {system. out. println ("change a function") ;}@ overridepublic void Plan () {system. out. println ("Change Plan ");}}
Package command; public class requirmentgroup extends group {@ overridepublic void find () {system. Out. println ("find the requirement group .... ") ;}@ Overridepublic void add () {system. Out. println (" adds a function requirement .... ") ;}@ Overridepublic void Delete () {system. out. println ("delete a feature ...... ") ;}@ overridepublic void change () {system. out. println ("modify a requirement ...... ") ;}@ overridepublic void Plan () {system. out. println ("the customer requested a change plan ..... ");}}
An abstract command class
Package command; public abstract class command {// there is only one method. What do you want me to do? Public abstract void execute ();}
package command;public class AddRequrimentCommand extends Command{private Group require;public AddRequrimentCommand(Group group){this.require = group;}@Overridepublic void execute() {this.require.find();this.require.add();}}
package command;public class DeleteCodeCommand extends Command{private Group code;public DeleteCodeCommand(Group _group){this.code = _group;}@Overridepublic void execute() {this.code.find();this.code.delete();this.code.plan();}}
Contact Person
Package command; public class invoker {private command; Public void setcommand (command) {This. command = command;} // execute the customer's command public void action({%this.command.exe cute ();}}
Package command; public class client {public static void main (string [] ARGs) {invoker invoke = new invoker (); // The customer requests to add a new requirement system. out. println ("the customer requested to add a requirement .... "); // Group group1 = new requirmentgroup (); // command = new addrequrimentcommand (group1); group Group = new codegroup (); command command = new deletecodecommand (Group); invoke. setcommand (command); invoke. action ();}}
You can copy the code and execute it once to understand the specific advantages of the command mode.