Important java mode-command mode

Source: Internet
Author: User

The command mode provides a standard interface for processing user requests. EachThe request is encapsulated into an object named command.The three classes in command mode are:Command, commandmanager, and invoker.

Command

The command class indicates the encapsulation of a single behavior. Every action in the application, such as saving or deleting, is modeled as a command. In this way,The behavior of an application is a collection of command objects.To add behavior to an application, developers only need to implement the additional command object.

As follows:

package command;public interface Command {public void execute();}


The command interface only contains an execute method. Other classes that implement the command interface (defaultcommand) are a command, as follows:

Package command; public class defaultcommand implements command {public void execute () {system. Out. println (" ultcommand execution ...");}}

Of course, you can also extend the command interface to implement more functions:

Package command; import Java. util. collection; import Java. util. map;/*** extends the self-command interface. managedcommand implements this interface **/public interface managedlifecycle extends command {public void setapplicationcontext (MAP context ); // public void initialize () is used to transmit the required application data; // The initialization method is public Boolean isvalidated (); // the public collection is verified. <?> Geterrors (); // get the error message public void destory (); // destroy}

The following is the managedcommand class that implements managedlifecycle. It is also a command:

Package command; import Java. util. collection; import Java. util. hashmap; import Java. util. map;/*** is much richer than defacommand command. First, pass the required application data, call the initialization method, and perform verification, finally, run the ***/public class managedcommand implements managedlifecycle {private Map <?,?> Context; private Map <?,?> Errors = new hashmap (); // initialize public void initialize () {system. out. println ("initialization... ");} // execute public void execute () {system. out. println ("executing the managedcommand... ");} // destroy public void destory () {system. out. println ("destroying... ");} // whether the verification is successful. Public Boolean isvalidated () {system. out. println ("Verification Successful... "); Return true;} public void setapplicationcontext (MAP context) {system. out. println ("Transfer Application Data... "); this. context = context; } // Get the public collection error message <?> Geterrors () {return errors. Values ();}}
Commandmanager

Commandmanager, the next component in command mode,This class is responsible for managing available commands for Applications. The following commandmanager processes all requests and uses a hashmap. All commands will be initialized before the request is processed, and then obtained by name

Package command; import Java. util. collection; import Java. util. hashmap; import Java. util. map;/*** process all requests and use a hashmap. All commands will be initialized before the request is processed, then, get the ***/public class commandmanager {private Map <string, command> commands = new hashmap <string, command> (); Public void add (string name, command command) {commands. put (name, command);} public command getcommand (string name) {return commands. get (name );}}
Invoker

Executes command classes in certain ways. Take a look at the following client class:

Package command; import java. util. Collection; import java. util. hashmap; import java. util. Map;/*** a separate client can conceal command mode execution. The monotonous use of client to construct a notebook is that it wants the manager to add */public class client {private map context = new hashmap (); Private commandmanager manager = new commandmanager (); Public client () {manager. add ("default", new defaultcommand (); // Add a new command to commandmanager for manager. add ("managed", new managedcommand ();} public void invoke (string name) {command = manager. getcommand (name); // determines whether the executed command implements the managedlifecycle interface if (command instanceof m Anagedlifecycle) {managedlifecycle managed = (managedlifecycle) using cute ();} else {collection <?> Error = managed.geterrors(registrantprofileid managed.destory(registrantprofilelseappscommand.exe cute () ;}} public static void main (string [] ARGs) {Client client = new client (); system. out. println ("1. Execute defacommand command:"); client. invoke ("default"); system. out. println ("2. Execute the managedcommand command:"); client. invoke ("managed ");}}

Running result:

It is a very powerful concept to allow the client invoker to pass resources to commands.Inversion of control (IOC) or dependency Injection. It does not need the command class to find the services and resources that can be used for invoker.

Command mode example:Use command mode to say goodbye to if-Else


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.