• Java: Command Pattern


    /**
     * 版权所有 2022 涂聚文有限公司
     * 许可信息查看:
     * 描述:
     * 命令模式 Command Patterns
     * 历史版本: JDK 14.02
     * 2022-09-12 创建者 geovindu
     * 2022-09-12 添加 Lambda
     * 2022-09-12 修改:date
     * 接口类
     * 2022-09-12 修改者:Geovin Du
     * 生成API帮助文档的指令:
     *javadoc - -encoding Utf-8 -d apidoc CommandFromGod.java
     * Interface
     * Record
     * Annotation
     * Enum
     * */
    
    
    
    package com.javapatterns.command;
    
    /**
     *
     * */
    public interface CommandFromGod {
        /**
         *
         * */
        public void Execute();
    }
    

      

    /**
     * 版权所有 2022 涂聚文有限公司
     * 许可信息查看:
     * 描述:
     * 命令模式 Command Patterns
     * 历史版本: JDK 14.02
     * 2022-09-12 创建者 geovindu
     * 2022-09-12 添加 Lambda
     * 2022-09-12 修改:date
     * 接口类
     * 2022-09-12 修改者:Geovin Du
     * 生成API帮助文档的指令:
     *javadoc - -encoding Utf-8 -d apidoc GodRestsCommand.java
     * Interface
     * Record
     * Annotation
     * Enum
     * */
    
    package com.javapatterns.command;
    import java.awt.*;
    import java.awt.event.*;
    
    /**
     *
     * */
    public class GodRestsCommand extends Button implements CommandFromGod{
    
        /**
         *
         * */
        public GodRestsCommand(String caption)
        {
            super(caption);
        }
        /**
         *
         * */
        public void Execute()
        {
            System.exit(0);
        }
    
    }
    

      

    /**
     * 版权所有 2022 涂聚文有限公司
     * 许可信息查看:
     * 描述:
     * 命令模式 Command Patterns
     * 历史版本: JDK 14.02
     * 2022-09-12 创建者 geovindu
     * 2022-09-12 添加 Lambda
     * 2022-09-12 修改:date
     * 接口类
     * 2022-09-12 修改者:Geovin Du
     * 生成API帮助文档的指令:
     *javadoc - -encoding Utf-8 -d apidoc LetThereBeLandCommand.java
     * Interface
     * Record
     * Annotation
     * Enum
     * */
    
    
    package com.javapatterns.command;
    import java.awt.*;
    import java.awt.event.*;
    
    /**
     *
     * */
    public class LetThereBeLandCommand extends Button implements CommandFromGod{
        /**
         *
         * */
        Panel p;
        /**
         *
         * */
        public LetThereBeLandCommand(String caption, Panel pnl)
        {
            super(caption);
            p = pnl;
        }
    
        /**
         *
         * */
        public void Execute()
        {
            p.setBackground(Color.orange);
        }
    
    }
    

      

    /**
     * 版权所有 2022 涂聚文有限公司
     * 许可信息查看:
     * 描述:
     * 命令模式 Command Patterns
     * 历史版本: JDK 14.02
     * 2022-09-12 创建者 geovindu
     * 2022-09-12 添加 Lambda
     * 2022-09-12 修改:date
     * 接口类
     * 2022-09-12 修改者:Geovin Du
     * 生成API帮助文档的指令:
     *javadoc - -encoding Utf-8 -d apidoc LetThereBeLightCommand.java
     * Interface
     * Record
     * Annotation
     * Enum
     * */
    
    package com.javapatterns.command;
    
    import java.awt.*;
    import java.awt.event.*;
    
    /**
     *
     * */
    public class LetThereBeLightCommand extends Button implements CommandFromGod{
        /**
         *
         * */
        Panel p;
    
        /**
         *
         * */
        public LetThereBeLightCommand(String caption, Panel pnl)
        {
            super(caption);
            p = pnl;
        }
    
        /**
         *
         * */
        public void Execute()
        {
            p.setBackground(Color.white);
        }
    
    }
    

      

    /**
     * 版权所有 2022 涂聚文有限公司
     * 许可信息查看:
     * 描述:
     * 命令模式 Command Patterns
     * 历史版本: JDK 14.02
     * 2022-09-12 创建者 geovindu
     * 2022-09-12 添加 Lambda
     * 2022-09-12 修改:date
     * 接口类
     * 2022-09-12 修改者:Geovin Du
     * 生成API帮助文档的指令:
     *javadoc - -encoding Utf-8 -d apidoc ResetCommand.java
     * Interface
     * Record
     * Annotation
     * Enum
     * */
    
    
    package com.javapatterns.command;
    import java.awt.*;
    import java.awt.event.*;
    /**
     *
     * */
    public class ResetCommand  extends Button implements CommandFromGod{
        /**
         *
         * */
        Panel p;
    
        /**
         *
         * */
        public ResetCommand(String caption, Panel pnl)
        {
            super(caption);
            p = pnl;
        }
    
        /**
         *
         * */
        public void Execute()
        {
            p.setBackground(Color.black);
        }
    }
    

      

    /**
     * 版权所有 2022 涂聚文有限公司
     * 许可信息查看:
     * 描述:
     * 命令模式 Command Patterns
     * 历史版本: JDK 14.02
     * 2022-09-12 创建者 geovindu
     * 2022-09-12 添加 Lambda
     * 2022-09-12 修改:date
     * 接口类
     * 2022-09-12 修改者:Geovin Du
     * 生成API帮助文档的指令:
     *javadoc - -encoding Utf-8 -d apidoc TheWorld.java
     * Interface
     * Record
     * Annotation
     * Enum
     * */
    
    package com.javapatterns.command;
    
    
    import java.awt.*;
    import java.awt.event.*;
    
    /**
     *
     * */
    public class TheWorld extends Frame implements ActionListener{
    
        LetThereBeLightCommand btnLight;
        LetThereBeLandCommand btnLand;
        ResetCommand btnReset;
        GodRestsCommand btnExit;
        Panel p;
        /**
         *
         * */
        public TheWorld()
        {
            super("This is the world, and God says...");
    
            p = new Panel();
            p.setBackground(Color.black);
    
            add(p);
            btnLight = new LetThereBeLightCommand("Let there be light", p);
            btnLand = new LetThereBeLandCommand("Let there be land", p);
            btnReset = new ResetCommand("Reset", p);
            btnExit = new GodRestsCommand("God rests");
    
            p.add(btnLight);
            p.add(btnLand);
            p.add(btnReset);
            p.add(btnExit);
    
            btnLight.addActionListener(this);
            btnLand.addActionListener(this);
            btnReset.addActionListener(this);
            btnExit.addActionListener(this);
            setBounds(100,100,400,200);
            setVisible(true);
        }
        /**
         *
         * */
        public void actionPerformed(ActionEvent e)
        {
            CommandFromGod obj = (CommandFromGod)e.getSource();
            obj.Execute();
        }
    
    }
    

      

    调用:

            //命令模式
                TheWorld theWorld=new TheWorld();
    

      

    输出:

  • 相关阅读:
    全排列(求所有情况的个数)--10--全排列--蓝桥杯凑算式和leetcode46全排列
    DFS+BFS(广度优先搜索弥补深度优先搜索遍历漏洞求合格条件总数)--09--DFS+BFS--蓝桥杯剪邮票
    Powermock2.0.0 详细 总结
    Springboot 前后端数据传输 常见误区
    idea src下源文件和class编译文件不一致
    java对象clone
    数据结构-链表
    队列
    稀疏数组
    数据库隔离级别
  • 原文地址:https://www.cnblogs.com/geovindu/p/16736242.html
Copyright © 2020-2023  润新知