命令模式

将一个请求封装成一个对象,从而让用户使用不同的请求把客户端参数化;对请求排队或者记录请求日志,以及支持撤销的操作。

使用场景:
在某些场合,比如要对行为进行"记录、撤销/重做、事务"等处理,这种无法抵御变化的紧耦合是不合适的。
在这种情况下,如何将"行为请求者"与"行为实现者"解耦?将一组行为抽象为对象,可以实现二者之间的松耦合。

优点:
弱耦合、良好的扩展性和灵活性

缺点:
类膨胀

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值