java 设计模式_行为型_19命令模式

19.命令模式

首先就是我们的 Client 想要实现一个功能,于是它就创建了一个 Command, 为了方便调用将 Command 封装在了 Invoker 中,当我们想调用的时候,Invoker 会执行内部 Command 提供的方法, Receiver 接收到 Command 的请求,为其提供底部支持。

多说无益,我将通过一个例子介绍命令模式。

实例

目前大部分的软件都支持用户自定义界面,比如说我们可以修改字体大小,背景颜色等。我们就以此为例。首先,写出两个类。

java 复制代码
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Font {
    private String fontSize = "normal";
}
java 复制代码
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BackGround {
    private String bgColor = "default color";
}

这是 Command, 修改字体大小和背景的类都继承于此。

java 复制代码
public interface Command {
    void execute();
}

我们只是在类中获得一个 Font 类的引用,然后调用setFontSize() 方法对字体的大小进行设置。

java 复制代码
@Data
@NoArgsConstructor
@AllArgsConstructor
public class LargeFontCommand implements Command {


    private Font font;


    @Override
    public void execute() {
        font.setFontSize("large");
    }
}
java 复制代码
@AllArgsConstructor
@NoArgsConstructor
@Data
public class NormalFontCommand implements Command {
    private Font font;


    @Override
    public void execute() {
        font.setFontSize("Normal");
    }
}

background类的引用,调背景颜色

java 复制代码
@NoArgsConstructor
@AllArgsConstructor
@Data
public class CustomBackGround implements Command {


    private BackGround backGround;
    private String color;


    public CustomBackGround(BackGround backGround){
        this.backGround = backGround;
    }




    @Override
    public void execute() {
        backGround.setBgColor("Custom background");
    }
}
java 复制代码
@AllArgsConstructor
@NoArgsConstructor
@Data
public class DefaultBackground implements Command {


    private BackGround backGround;


    @Override
    public void execute() {
        backGround.setBgColor("default color");
    }
}

Invoker类存放命令

java 复制代码
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Invoker {


    private List<Command> commands = new ArrayList<>();


    public void setCommand(int i,Command command){
        commands.add(i,command);
    }
    public void update(int i){
        commands.get(i).execute();
    }
}
java 复制代码
public class CommandTest {
    public static void main(String[] args) {
        Font font = new Font();
        BackGround backGround = new BackGround();


        NormalFontCommand normalFontCommand = new NormalFontCommand(font);
        LargeFontCommand largeFontCommand = new LargeFontCommand(font);
        DefaultBackground defaultBackground = new DefaultBackground(backGround);
        CustomBackGround customBackGround = new CustomBackGround(backGround);


        Invoker invoker = new Invoker();
        invoker.setCommand(0,normalFontCommand);
        invoker.setCommand(1,largeFontCommand);
        invoker.setCommand(2,defaultBackground);
        invoker.setCommand(3,customBackGround);


        invoker.update(3);
        System.out.println(backGround.getBgColor());


    }
}

我们首先把所有的命令添加到了 Invoker , 然后直接调用 update() 方法就可以了。

这么做有什么好处呢?看的出来,可以将很多命令放进 Invoker , 它并不知道功能是如何实现的,它就像一个中介, Client 请求一个功能,它就将这个请求转给 Command 去实现。这种模式有很多的用途,比如说多功能遥控器,日志打印等。

还有一点不得不说的,我们可以使用宏命令,什么是宏命令呢?就是写一个 Command ,这个 Command 可以实现多个功能。比如说我们可以同时修改背景和颜色。

相关推荐
皮皮林5512 小时前
SpringBoot 加载外部 Jar,实现功能按需扩展!
java·spring boot
rocksun2 小时前
认识Embabel:一个使用Java构建AI Agent的框架
java·人工智能
Java中文社群3 小时前
AI实战:一键生成数字人视频!
java·人工智能·后端
王中阳Go4 小时前
从超市收银到航空调度:贪心算法如何破解生活中的最优决策谜题?
java·后端·算法
shepherd1114 小时前
谈谈TransmittableThreadLocal实现原理和在日志收集记录系统上下文实战应用
java·后端·开源
维基框架4 小时前
Spring Boot 项目整合Spring Security 进行身份验证
java·架构
渣渣_Maxz4 小时前
使用 antlr 打造 Android 动态逻辑判断能力
android·设计模式
日月星辰Ace5 小时前
Java JVM 垃圾回收器(四):现代垃圾回收器 之 Shenandoah GC
java·jvm
天天摸鱼的java工程师6 小时前
商品详情页 QPS 达 10 万,如何设计缓存架构降低数据库压力?
java·后端·面试
天天摸鱼的java工程师6 小时前
设计一个分布式 ID 生成器,要求全局唯一、趋势递增、支持每秒 10 万次生成,如何实现?
java·后端·面试