JAVA design pattern: command mode

319 people read comment(2) Collection report

Typically, the behavior of the request and the implementation usually presents a highly coupled state. Sometimes it is not appropriate to change the behavior of the process, the highly coupled mode is not suitable.
* the behavior of the request and the behavior of decoupling, a set of behavioral abstractions as the object, the realization of the loose coupling between the two. This is the command mode (Pattern Command).
Command mode is usually composed of several parts: the command interface, the specific commands (to achieve the command interface), to accept the command of the image, such as the command controller.
* mode advantage:
1 reduce the coupling degree between objects.
2 new commands can be easily added to the system and design a combination of commands.
4 implementation calls the same method to achieve different functions.
Model deficiencies:
Each command must design a specific class, command more words, will lead to more classes, the system becomes large, the efficiency of the use of the command mode may be reduced.

/ * *
* interface to execute commands
*@date2016-1-19 am 10:26:04
* /
Public Interface OrderImpl{
    VoidExecute ();/ / execution method
/ * *
* specific commands: open air conditioning commands
*@date2016-1-19 am 10:28:18
* /
Public Class OnOrder Implements OrderImpl{
    PrivateAir AirCondition;
    Public OnOrder(light AirCondition) {
        This.air = light;
    Public Void Execute(1) ();
/ * *
* specific commands: off air conditioning commands
*@date2016-1-19 am 10:28:51
* /
Public Class OffOrder Implements OrderImpl{
    PrivateLight AirCondition;
    Public OffOrder(light AirCondition) {
        This.light = light;
    Public Void Execute(1)
Light.close ();
/ * *
Specific commands: regulating wind speed
*@date2016-1-19 am 11:04:50
* /
Public Class ChangeSpeedOrder Implements OrderImpl{
    PrivateMyTv AirCondition;
    Private IntChannel;

    Public ChangeSpeedOrder(TV AirCondition,IntChannel) {
MyTv = tv; = channel;

    Public Void Execute(1)
MyTv.updateWind (channel);
/ * *
* command controller, equivalent to the remote control: the control of the air conditioning switch, adjust the wind speed, etc.
*@date2016-1-19 am 10:57:43
* /
Public Class Control{
    PrivateOnOrder OrderImpl;/ / open
    PrivateOffOrder OrderImpl;/ / close
    PrivateUpdateChannel OrderImpl;/ / adjust speed

    Public Control(onOrder OrderImpl, offOrder OrderImpl, updateChannel OrderImpl) {
        This.onOrder = onOrder;
        This.offOrder = offOrder;
        This.updateChannel = updateChannel;


    Public Void TurnOn(1)
OnOrder.execute ();

    Public Void TurnOff(1)
OffOrder.execute ();

    Public Void ChangeChannel(1)
UpdateChannel.execute ();
/ * *
* command receiver: air conditioning for example
*@date2016-1-19 am 10:52:38
* /
Public Class AirCondition{
    Public IntCurrentChannel =Zero;/ / current wind speed gear

    Public Void Open(1)
System.out.println ("Turn on the air conditioner! ");

    Public Void Close(1)
System.out.println ("Turn off the air conditioning! ");

    Public Void UpdateWind(IntChannel) {
        This.currentChannel = channel;
System.out.println ("Change the air speed of the air conditioner:"+ channel);
Public Class{Test

    Public Static Void Main(args String[]) {
        Command / recipient (air conditioning)
MyAir AirCondition =NewAirCondition ();
        / / open air command
On OnOrder =NewOnOrder (myAir);
        Turn off the air conditioning / command
Off OnOrder =NewOnOrder (myAir);
        The wind speed change / command
Speed ChangeSpeedOrder =NewChangeSpeedOrder (myAir,Two);
        Command / control object
Control Control =NewControl (on, off, speed);
        / / boot
Control.turnOn ();
        / / switching channels
Control.changeChannel ();
        / / shutdown
Control.turnOff ();

Test results:
Open air conditioning!
Change air speed gear position: 2
Open air conditioning!

step on

Reference knowledge base

Guess you're looking for
View comments
* the above user comments only represent their personal views, does not represent the views or position of the CSDN website
    personal data
    • visit:119317 times
    • Integral:Three thousand seven hundred and seventy-four
    • Grade:
    • Rank:5674th name
    • original:254
    • Reproduced:46
    • Translation:1
    • Comments:62
    Latest comments