------Command mode for Java 23 design Patterns

Source: Internet
Author: User

First, the command mode(Command)

Command mode well understood, for example, the commander ordered the soldiers to do something, from the perspective of the whole matter, the role of the commander is to issue a password, the password passed through to the soldiers ' ears, soldiers to carry out. The process is good, the three mutual decoupling, no one side to rely on other people, just do their own thing, the commander wants the result, not to pay attention to how the soldiers are realized. Let's look at the diagram:

Invoker is the caller (commander), receiver is the callee (soldier), and MyCommand is the command that implements the commands interface, holds the receiving object, and looks at the implementation code:

    1. Public Interface Command {
    2. public void exe ();
    3. }
 
  1. Public Class mycommand implements Command {
  2. private receiver receiver;
  3. Public mycommand (receiver receiver) {
  4. this. receiver = receiver;
  5. }
  6. @Override
  7. public void exe () {
  8. Receiver.action ();
  9. }
  10. }
    1. public class RECEIVER {  
    2.     public  void action () {   
    3.          system.out.println ( "command received!"
    4.      }  
    5. }  
  1. Public class Invoker {
  2. private Command command;
  3. Public Invoker (Command command) {
  4. this. Command = command;
  5. }
  6. public void action () {
  7. Command.exe ();
  8. }
  9. }
  1. Public class Test {
  2. public static void main (string[] args) {
  3. Receiver receiver = new receiver ();
  4. Command cmd = new mycommand (receiver);
  5. Invoker Invoker = new Invoker (CMD);
  6. Invoker.action ();
  7. }
  8. }

Output: Command received!

This is very good understanding, the purpose of the command mode is to achieve the command of the issuer and the executor decoupling between the implementation of the request and execution of separation, familiar with struts should know that struts is actually a request and presentation separation technology, which inevitably involves the idea of command mode!

Reprinted from Https://www.cnblogs.com/maowang1991/archive/2013/04/15/3023236.html

------Command mode for Java 23 design Patterns

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.