将一个请求封装成一个对象,从而让用户使用不同的请求把客户端参数化;对请求排队或者记录请求日志,以及支持撤销的操作。
使用场景:
在某些场合,比如要对行为进行"记录、撤销/重做、事务"等处理,这种无法抵御变化的紧耦合是不合适的。
在这种情况下,如何将"行为请求者"与"行为实现者"解耦?将一组行为抽象为对象,可以实现二者之间的松耦合。
优点:
弱耦合、良好的扩展性和灵活性
缺点:
类膨胀
package 命令模式;
public class 命令模式 {
public static void main(String[] args) {
ConcreteCommand command = new ConcreteCommand(new Receiver());
Invoker invoker = new Invoker(command);
invoker.invoke();
}
}
package 命令模式;
public abstract class Command {
public abstract void execute();
}
package 命令模式;
public class ConcreteCommand extends Command{
Receiver mRecevier;
public ConcreteCommand(Receiver mRecevier) {
this.mRecevier = mRecevier;
}
@Override
public void execute() {
// TODO Auto-generated method stub
mRecevier.action();
}
}
package 命令模式;
public class Invoker {
Command mCommand;
public Invoker(Command command) {
mCommand = command;
}
public void invoke() {
mCommand.execute();
}
}
package 命令模式;
public class Receiver {
public void action() {
System.out.println("动作执行");
}
}
简单实践
package 命令模式;
public class 命令模式 {
public static void main(String[] args) {
//创建游戏
TerisMachine machine = new TerisMachine();
Button button = new Button();
//构建命令
FastToBottomCommand fastToBottomCommand = new FastToBottomCommand(machine);
LeftCommand leftCommand = new LeftCommand(machine);
RightCommand rightCommand = new RightCommand(machine);
TransformCommand transformCommand = new TransformCommand(machine);
//按钮设置执行命令
button.setmFastToBottomCommand(fastToBottomCommand);
button.setMlLeftCommand(leftCommand);
button.setmRightCommand(rightCommand);
button.setmTransformCommand(transformCommand);
//玩家按下命令
button.toLeft();
button.toRight();
button.fastToBottom();
button.transform();
}
}
package 命令模式;
public interface Command {
void execute();
}
package 命令模式;
public class Button {
private LeftCommand mLeftCommand;
private RightCommand mRightCommand;
private FastToBottomCommand mFastToBottomCommand;
private TransformCommand mTransformCommand;
public void setMlLeftCommand(LeftCommand mlLeftCommand) {
this.mLeftCommand = mlLeftCommand;
}
public void setmRightCommand(RightCommand mRightCommand) {
this.mRightCommand = mRightCommand;
}
public void setmFastToBottomCommand(FastToBottomCommand mFastToBottomCommand) {
this.mFastToBottomCommand = mFastToBottomCommand;
}
public void setmTransformCommand(TransformCommand mTransformCommand) {
this.mTransformCommand = mTransformCommand;
}
public void toLeft(){
mLeftCommand.execute();
}
public void toRight(){
mRightCommand.execute();
}
public void fastToBottom(){
mFastToBottomCommand.execute();
}
public void transform(){
mTransformCommand.execute();
}
}
package 命令模式;
public class FastToBottomCommand implements Command {
private TerisMachine mMachine;
public FastToBottomCommand(TerisMachine machine){
mMachine = machine;
}
@Override
public void execute() {
// TODO Auto-generated method stub
mMachine.fastToBottom();
}
}
package 命令模式;
public class LeftCommand implements Command {
private TerisMachine mMachine;
public LeftCommand(TerisMachine machine){
mMachine = machine;
}
@Override
public void execute() {
// TODO Auto-generated method stub
mMachine.toLeft();
}
}
package 命令模式;
public class RightCommand implements Command {
private TerisMachine mMachine;
public RightCommand(TerisMachine machine){
mMachine = machine;
}
@Override
public void execute() {
// TODO Auto-generated method stub
mMachine.toRight();
}
}
package 命令模式;
public class TransformCommand implements Command {
private TerisMachine mMachine;
public TransformCommand(TerisMachine machine){
mMachine = machine;
}
@Override
public void execute() {
// TODO Auto-generated method stub
mMachine.transform();
}
}