命令模式-实例使用

未使用命令模式的UML

使用命令模式后的UML

public abstract class Command {
    public abstract void execute();
}

public class Invoker {
    private Command command;

    /**
     * 为功能键注入命令
     * @param command
     */
    public void setCommand(Command command) {
        this.command = command;
    }

    /**
     * 点击按钮事件
     */
    public void click() {
        System.out.print("点击按钮事件:");
        command.execute();
    }
}

public class BarCommand extends Command{

    final private BarCodeActivity barCodeActivity;
    final private Context context;
    final private String str;

    public BarCommand(Context context, String str){
        barCodeActivity = new BarCodeActivity();
         this.str = str;
         this.context = context;
    }
    @Override
    public void execute() {
        barCodeActivity.printBarCode(context,str);
    }
}
public class LabelCommand extends Command{

    final private LabelActivity labelActivity;
    final private Context context;
    final private String str;

    public LabelCommand(Context context, String str){
         labelActivity = new LabelActivity();
         this.str = str;
         this.context = context;
    }
    @Override
    public void execute() {
        labelActivity.printLabel(context,str);
    }
}
public class QrCommand extends Command{

    final private QrActivity qrActivity;
    final private Context context;
    final private String str;

    public QrCommand(Context context, String str){
         qrActivity = new QrActivity();
         this.str = str;
         this.context = context;
    }
    @Override
    public void execute() {
        qrActivity.printQrCode(context,str);
    }
}

public void onQrcode(View view){
        Logs.d(TAG,"");
        Command command = CommandFactory.createQrCommand(getApplicationContext(), mEditText.getText().toString());
        executeCommand(command);
    }
    public void onBarcode(View view){
        Command command = CommandFactory.createBarCommand(getApplicationContext(), mEditText.getText().toString());
        executeCommand(command);
    }
    public void onLabel(View view){
        Command command = CommandFactory.createLabelCommand(getApplicationContext(), mEditText.getText().toString());
        executeCommand(command);
    }

public class CommandFactory {

    public static Command createQrCommand(Context context, String data) {
        return new QrCommand(context, data);
    }

    public static Command createBarCommand(Context context, String data) {
        return new BarCommand(context, data);
    }

    public static Command createLabelCommand(Context context, String data) {
        return new LabelCommand(context, data);
    }
}
相关推荐
三玖诶2 天前
在 Qt 中使用 QLabel 设置 GIF 动态背景
开发语言·qt·命令模式
我码玄黄3 天前
JS 的行为设计模式:策略、观察者与命令模式
javascript·设计模式·命令模式
jzpfbpx3 天前
[go] 命令模式
开发语言·golang·命令模式
图像处理大大大大大牛啊6 天前
使用mingw64 编译 QT开发流程
开发语言·c++·qt·命令模式
写bug如流水7 天前
【FastAPI】离线使用Swagger UI 或 国内网络如何快速加载Swagger UI
ui·fastapi·命令模式
AI让世界更懂你8 天前
漫谈设计模式 [13]:命令模式
python·设计模式·命令模式
2401_839115739 天前
9.11.
命令模式
纵码驰骋9 天前
探索最佳 Shell 工具:全面测评 Bash、Zsh、Fish、Tcsh 和 Ksh
linux·服务器·ide·ssh·bash·策略模式·命令模式
榴月子11 天前
命令模式(Command Pattern)
java·命令模式
胖虎江11 天前
9.9(QT Day 2)
开发语言·qt·命令模式