
Copyright statement: This article is the original article for the blogger, without permission may not be reproduced.
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
*@description:
*@date2016-1-19 am 10:26:04
* /
Public Interface OrderImpl{
VoidExecute ();/ / execution method
}
/ * *
* specific commands: open air conditioning commands
*@description:
*@date2016-1-19 am 10:28:18
* /
Public Class OnOrder Implements OrderImpl{
PrivateAir AirCondition;
Public OnOrder(light AirCondition) {
This.air = light;
}
@Override
Public Void Execute(1)
Air.open ();
}
}
/ * *
* specific commands: off air conditioning commands
*@description:
*@authorLDM
*@date2016-1-19 am 10:28:51
* /
Public Class OffOrder Implements OrderImpl{
PrivateLight AirCondition;
Public OffOrder(light AirCondition) {
This.light = light;
}
@Override
Public Void Execute(1)
Light.close ();
}
}
/ * *
Specific commands: regulating wind speed
*@description:
*@date2016-1-19 am 11:04:50
* /
Public Class ChangeSpeedOrder Implements OrderImpl{
PrivateMyTv AirCondition;
Private IntChannel;
Public ChangeSpeedOrder(TV AirCondition,IntChannel) {
MyTv = tv;
This.channel = 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.
*@description:
*@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
*@description:
*@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!
- top
- One
- step on
- Zero
- Last oneJAVA design patterns: Visitor patterns
- Next articleThe JAVA design pattern: the builder pattern
Reference knowledge base
- Guess you're looking for