命令模式-实例使用
2023-12-30 15:38:05
未使用命令模式的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);
}
}
文章来源:https://blog.csdn.net/wcg_jishuo/article/details/135305491
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!