码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • java实现命令模式


    命令模式是一种行为设计模式,它允许您将请求封装为对象,以便您可以将其参数化、队列化、记录和撤销。在 Java 中实现命令模式涉及创建一个命令接口,具体命令类,以及一个接收者类,该接收者类执行实际操作。下面是一个简单的 Java 命令模式示例:

    首先,创建一个命令接口(Command):

    public interface Command {
        void execute();
    }
    
    • 1
    • 2
    • 3

    然后,创建一个具体命令类,实现该接口:

    public class LightOnCommand implements Command {
        private Light light;
    
        public LightOnCommand(Light light) {
            this.light = light;
        }
    
        @Override
        public void execute() {
            light.turnOn();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    创建另一个具体命令类:

    public class LightOffCommand implements Command {
        private Light light;
    
        public LightOffCommand(Light light) {
            this.light = light;
        }
    
        @Override
        public void execute() {
            light.turnOff();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    接下来,创建一个接收者类,该类包含实际的操作:

    public class Light {
        public void turnOn() {
            System.out.println("灯已打开");
        }
    
        public void turnOff() {
            System.out.println("灯已关闭");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    现在,创建一个遥控器类,该类可以设置命令并执行它们:

    public class RemoteControl {
        private Command command;
    
        public void setCommand(Command command) {
            this.command = command;
        }
    
        public void pressButton() {
            command.execute();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    最后,可以测试这些类:

    public class Main {
        public static void main(String[] args) {
            Light light = new Light();
            Command lightOn = new LightOnCommand(light);
            Command lightOff = new LightOffCommand(light);
    
            RemoteControl remoteControl = new RemoteControl();
    
            remoteControl.setCommand(lightOn);
            remoteControl.pressButton();
    
            remoteControl.setCommand(lightOff);
            remoteControl.pressButton();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    这是一个简单的命令模式示例,它演示了如何使用命令对象将操作参数化,并通过遥控器执行这些操作。您可以根据需要扩展和修改这个示例以满足特定的需求。

  • 相关阅读:
    使用docker login 远程登录harbor
    New PMC 田原: 开源给了我一个接触非业务系统的机会
    微擎模块 万能门店小程序7.4.1带九大插件 【增加】DIY商品模块,优化拼团产品页部分样式摇一摇插件
    工具及方法 - 多邻国: Duolingo
    图学习笔记
    Xilinx IP 10G Ethernet PCS/PMA IP Core
    使用 Docker 部署 Next Terminal 轻量级堡垒机
    微软开源在线代码编辑器,编辑器天花板之Monaco Editor
    typescript 根据对象的键生成枚举类型
    论文笔记:Universal Value Function Approximators
  • 原文地址:https://blog.csdn.net/sunyuhua_keyboard/article/details/132684322
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号