命令模式-实例使用

未使用命令模式的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);
    }
}
相关推荐
ximu_polaris2 天前
设计模式(C++)-行为型模式-命令模式
c++·设计模式·命令模式
其实防守也摸鱼8 天前
GDB安装与配置(保姆级教程)【Linux、Windows系统】
linux·运维·windows·命令模式·工具·虚拟机·调试
其实防守也摸鱼13 天前
无线网络安全---WLAN相关安全工具--kali(理论附题目)
linux·安全·web安全·学习笔记·kali·命令模式·wlan
sg_knight15 天前
设计模式实战:命令模式(Command)
python·设计模式·命令模式
yaaakaaang15 天前
十四、命令模式
java·命令模式
无籽西瓜a15 天前
【西瓜带你学设计模式 | 第十八期 - 命令模式】命令模式 —— 请求封装与撤销实现、优缺点与适用场景
java·后端·设计模式·软件工程·命令模式
23.24 天前
【Linux】grep -F 及 双横线--的妙用
linux·命令模式
摸鱼仙人~1 个月前
快照模式 vs 命令模式:一篇分清什么时候用谁
命令模式
2301_764441331 个月前
Dify工作流中实现查询优化(QO):将查询复杂度分类法与QOL框架融入工作流
人工智能·语言模型·自然语言处理·命令模式
fe7tQnVan1 个月前
三大 Agent-UI 协议深度剖析:AG-UI、A2UI 与 MCP-UI 的设计哲学与工程实践
ui·状态模式·命令模式