Easy to Master Java Command mode _java

Source: Internet
Author: User

definition: The request is wrapped in an object in the form of a command and passed to the calling object. The calling object looks for the appropriate object that can handle the command, and passes the command to the appropriate object, which executes the command.

Characteristics:

1, reduce the system coupling degree.

2. New commands can be easily added to the system.

Enterprise-class development and applications in common frameworks: Transaction, Struts action controller

Instance:

public class Demo {public

 static void Main (string[] args) {
 reicever reicever = new Reicever ();
 Command command = new Actualcommand (reicever);
 Invoker invoker = new Invoker ();
 Invoker.addcommand (command);
 Invoker.call ();
 }

/**
 * Command true performer, for different commands may have different command performer
/class reicever{public
 Void action () {
 System.out.println ("Reicever.action ()");
 }

/**
 * Command interface, also for the back of the following unified command * 
 *
interface command{public
 void Excute ();
}
/**
 * Actual Command object, there may be many different command objects
/class Actualcommand implements command{
 private reicever reicever;
 Public Actualcommand (Reicever reicever) {
 this.reicever = reicever;
 }
 public void Excute () {
 reicever.action ();
 }
}
/**
 * Command issued by * *
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 ()}
 }}
 


The command mode is used alone in very few situations, and is generally used in combination with Memo mode.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.