Design mode-Command mode

Source: Internet
Author: User

Basic Concepts:The command mode is also called the Order mode, which is the behavior design modetype of a. Command mode is called by theThe command class encapsulates the invocation behavior of the target object toand call parameters,The command pattern encapsulates the method call.. several roles of the command pattern:

Command: Abstract Commands class

Concretecommand: Specific Command class

Invoker: Caller

Receiver: Receiver

Client: Customer Class

advantages and disadvantages of the command pattern:

Advantages

1. Reduced system coupling

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

Disadvantages

Using command mode may cause some systems to have too many specific command classes.

Traders.java

 PackageCom.soyoungboy.command1; Public classTraders {/*** @Title: Sailmineralwater * @Description: TODO (selling mineral water) *@paramSettings File *@returnvoid return type *@throws     */     Public voidSailmineralwater () {System.out.println ("Selling Mineral water"); }        /*** @Title: Sailiceblacktea * @Description: TODO (selling ice red tea) *@paramSettings File *@returnvoid return type *@throws     */     Public voidSailiceblacktea () {System.out.println ("Ice red tea."); }}

abstract Command class: Command.java
/*** @ClassName: Command * @Description: TODO (Abstract command Class)*/ Public Abstract classCommand {Privatetraders traders;  PublicCommand (Traders traders) { This. Traders =traders; }        /**     * @returnBack to Traders **/     PublicTraders Gettraders () {returntraders; }        /**     * @paramtraders to set the traders*/     Public voidsettraders (Traders traders) { This. Traders =traders; }         Public Abstract voidsail (); }

specific command Class 1: Mineralwatercommand.java
 PackageCom.soyoungboy.command1;/*** * @ClassName: Mineralwatercommand * @Description: TODO (Specific command class--sellers sell mineral spring)*/ Public classMineralwatercommandextendsCommand { PublicMineralwatercommand (Traders traders) {Super(traders); } @Override Public voidsail () { This. Gettraders (). Sailmineralwater (); }     }

specific Command Class 2:Iceblackteacommand.java
  1.  PackageCom.soyoungboy.command1;/*** * @ClassName: Iceblackteacommand * @Description: TODO (Specific command class-sellers sell iced tea)*/ Public classIceblackteacommandextendsCommand { PublicIceblackteacommand (Traders traders) {Super(traders); } @Override Public voidsail () { This. Gettraders (). Sailiceblacktea (); }    }

  2. Receive class: Cashier.java
 PackageCom.soyoungboy.command1;Importjava.util.ArrayList;/*** @ClassName: Cashier * @Description: TODO (Recipient-receiver)*/ Public classCashier { PublicArraylist<command> drinks =NewArraylist<command>(); /*** @Title: Adddrinks * @Description: TODO (Buy a variety of drinks) *@param @paramCommand Settings file *@returnvoid return type *@throws     */     Public voidadddrinks (Command command) {drinks.add (command); }        /*** @Title: Removedrinks * @Description: TODO (don't have some kind of drinks) *@param @paramCommand Settings file *@returnvoid return type *@throws     */     Public voidremovedrinks (Command command) {drinks.remove (command); }        /*** @Title: Sail * @Description: TODO (Sell your own drinks) *@paramSettings File *@returnvoid return type *@throws     */     Public voidsail () { for(Command drink:drinks) {drink.sail (); }    }    }

The customer class is the test class:
 PackageCom.soyoungboy.command1; Public classTest { Public Static voidMain (string[] args) {Cashier Cashier=Newcashier (); Traders Traders=Newtraders (); Mineralwatercommand Mineralwatercommand=NewMineralwatercommand (traders); Iceblackteacommand Iceblackteacommand=NewIceblackteacommand (traders); //A bottle of iced tea and mineral water.cashier.adddrinks (Mineralwatercommand);        Cashier.adddrinks (Iceblackteacommand);        Cashier.sail (); System.out.println ("----------------"); //and a bottle of iced tea.cashier.adddrinks (Iceblackteacommand);        Cashier.sail (); System.out.println ("----------------"); //not a bottle of mineral water.cashier.removedrinks (Mineralwatercommand);    Cashier.sail (); }    }

Test Results: Selling Mineral waterSelling iced Red tea----------------Selling Mineral waterSelling iced Red teaSelling iced Red tea----------------Selling iced Red teaSelling iced Red tea use in Android:Thread, using the command mode in runnable:http://www.cnblogs.com/qianxudetianxia/archive/2011/08/13/2135478.html

Design mode-command mode

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.