命令模式-实例使用

未使用命令模式的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 天前
解耦的艺术:深入理解设计模式之命令模式
设计模式·命令模式
序属秋秋秋4 天前
《Linux系统编程之入门基础》【Linux基础 理论+命令】(上)
linux·运维·服务器·ubuntu·centos·命令模式
路明非1265 天前
QT界面实现2
命令模式
金涛03196 天前
QT-day2,信号和槽
开发语言·qt·命令模式
笨手笨脚の10 天前
设计模式-命令模式
设计模式·命令模式·行为型设计模式
web前端神器11 天前
webpack,vite,node等启动服务时运行一段时间命令窗口就卡住
命令模式·命令
青草地溪水旁14 天前
第十五章:令行禁止,运筹帷幄——Command的命令艺术
命令模式
jh_cao14 天前
(1)SwiftUI 的哲学:声明式 UI vs 命令式 UI
ui·swiftui·命令模式
青草地溪水旁16 天前
第十六章:固本培元,守正出奇——Template Method的模板艺术
命令模式
bkspiderx19 天前
C++设计模式之行为型模式:命令模式(Command)
c++·设计模式·命令模式