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
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 (); } }
- 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