定义
将一个请求封装成一个对象,从而让用户使用不同的请求把客户端参数化;对请求排队或者记录请求日志,以及支持可撤销的操作。
Android中源码使用命令模式
PackageHandler
特点
优点
命令模式的封装性很好,更弱的耦合性,更灵活的控制性以及更好的扩展性。2.缺点
类的膨胀,大量衍生类的创建。
使用场景
(1)需要抽出待执行的动作,然后以参数的形式提供出来。
(2)在不同的时刻指定、排列和执行请求。一个命令对象可以有与初始请求无关的生存期。
(3)需要支持操作取消。
(4)支持修改日志功能,这样当系统崩溃时,这些修改可以被重做一遍。
(5)需要支持事务操作。
简单实现
需求:推箱子游戏。
/**
* Created on 2019/4/4 11:33
* 推箱子游戏类
* @author Scarf Gong
*/
public class PushBox {
public void toLeft() {
Log.d("TAG", "Left");
}
public void toRight() {
Log.d("TAG", "Right");
}
public void toUp() {
Log.d("TAG", "Up");
}
public void toDown() {
Log.d("TAG", "Down");
}
public void revoke() {
Log.d("TAG", "撤销");
}
}
/**
* Created on 2019/4/4 11:36
* 命令接口
* @author Scarf Gong
*/
public interface Command {
void execute();
void getCommand();
}
--------------------------
/**
* Created on 2019/4/4 11:37
* 向下命令
* @author Scarf Gong
*/
public class DownCommand implements Command {
private PushBox mPushBox;
public DownCommand(PushBox pushBox) {
mPushBox = pushBox;
}
@Override
public void execute() {
mPushBox.toDown();
}
@Override
public void getCommand() {
Log.d("TAG", "⬇️️");
}
}
--------------------------
/**
* Created on 2019/4/4 11:37
* 向左命令
* @author Scarf Gong
*/
public class LeftCommand implements Command {
private PushBox mPushBox;
public LeftCommand(PushBox pushBox) {
mPushBox = pushBox;
}
@Override
public void execute() {
mPushBox.toLeft();
}
@Override
public void getCommand() {
Log.d("TAG", "⬅️");
}
}
--------------------------
/**
* Created on 2019/4/4 11:37
* 向右命令
* @author Scarf Gong
*/
public class RightCommand implements Command {
private PushBox mPushBox;
public RightCommand(PushBox pushBox) {
mPushBox = pushBox;
}
@Override
public void execute() {
mPushBox.toRight();
}
@Override
public void getCommand() {
Log.d("TAG", "➡️️");
}
}
--------------------------
/**
* Created on 2019/4/4 11:37
* 向上命令
* @author Scarf Gong
*/
public class UpCommand implements Command {
private PushBox mPushBox;
public UpCommand(PushBox pushBox) {
mPushBox = pushBox;
}
@Override
public void execute() {
mPushBox.toUp();
}
@Override
public void getCommand() {
Log.d("TAG", "⬆️");
}
}
--------------------------
/**
* Created on 2019/4/4 11:37
* 撤销命令
* @author Scarf Gong
*/
public class RevokeCommand implements Command {
private PushBox mPushBox;
public RevokeCommand(PushBox pushBox) {
mPushBox = pushBox;
}
@Override
public void execute() {
mPushBox.revoke();
}
@Override
public void getCommand() {
Log.d("TAG", "取消️");
}
}
--------------------------
/**
* Created on 2019/4/4 11:49
* 请求者类
* @author Scarf Gong
*/
public class Buttons {
private LeftCommand mLeftCommand;
private RightCommand mRightCommand;
private UpCommand mUpCommand;
private DownCommand mDownCommand;
private RevokeCommand mRevokeCommand;
private List<Command> mList = new ArrayList<>();
public void getCommandList() {
for (Command c:mList) {
c.getCommand();
}
}
public void setLeftCommand(LeftCommand leftCommand) {
mLeftCommand = leftCommand;
}
public void setRightCommand(RightCommand rightCommand) {
mRightCommand = rightCommand;
}
public void setUpCommand(UpCommand upCommand) {
mUpCommand = upCommand;
}
public void setDownCommand(DownCommand downCommand) {
mDownCommand = downCommand;
}
public void setRevokeCommand(RevokeCommand revokeCommand) {
mRevokeCommand = revokeCommand;
}
public void toLeft() {
mLeftCommand.execute();
mList.add(mLeftCommand);
}
public void toRight() {
mRightCommand.execute();
mList.add(mRightCommand);
}
public void toUp() {
mUpCommand.execute();
mList.add(mUpCommand);
}
public void toDown() {
mDownCommand.execute();
mList.add(mDownCommand);
}
public void toRevoke() {
mRevokeCommand.execute();
mList.add(mRevokeCommand);
}
}
使用:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
}
private void initData() {
PushBox pushBox = new PushBox();
LeftCommand leftCommand = new LeftCommand(pushBox);
RightCommand rightCommand = new RightCommand(pushBox);
UpCommand upCommand = new UpCommand(pushBox);
DownCommand downCommand = new DownCommand(pushBox);
RevokeCommand revokeCommand = new RevokeCommand(pushBox);
Buttons buttons = new Buttons();
buttons.setLeftCommand(leftCommand);
buttons.setUpCommand(upCommand);
buttons.setRightCommand(rightCommand);
buttons.setDownCommand(downCommand);
buttons.toDown();
buttons.toLeft();
buttons.toRight();
buttons.toUp();
buttons.getCommandList();
}
}
输出结果:
2019-04-04 13:45:43 D/TAG: Down
2019-04-04 13:45:43 D/TAG: Left
2019-04-04 13:45:43 D/TAG: Right
2019-04-04 13:45:43 D/TAG: Up
2019-04-04 13:45:43 D/TAG: ⬇️️
2019-04-04 13:45:43 D/TAG: ⬅️
2019-04-04 13:45:43 D/TAG: ➡️️
2019-04-04 13:45:43 D/TAG: ⬆️