zoukankan      html  css  js  c++  java
  • 设计模式课程 设计模式精讲 23-2 命令模式coding

    1    代码演练

    1.1  代码演练1

    1    代码演练

    1.1  代码演练1

    需求:

    木木网付费课程,第一节课可以不付费观看或者付费观看,通过命令模式实现

    注意:(个人开发中没有注意到的地方)

    a  接口无法添加属性(添加属性的接口编译器默认赋值成静态的,已经不属于本类属性范畴了),属性通过它的实现类来添加,参见打开课程命令类或者关闭课程命令类(标红部分)。

    b  员工类命令集合 用ArrayList列表实现,先进先出,后进后出。

    c  实际场景中,调用完执行命令后,记得把命令列表清空,参见员工类.

    uml类图:

    测试类:

    package com.geely.design.pattern.behavioral.command;
    
    public class Test {
    
        public static void main(String [] args){
            CourseVideo courseVideo = new CourseVideo("豆豆哲学论");
            //关闭课程的命令
            Command command = new CloseCourseVideoCommand(courseVideo);
            //打开课程的命令
            Command command1 = new OpenCourseVideoCommand(courseVideo);
    
            Staff staff1 = new Staff();
            staff1.addCommond(command);
            staff1.addCommond(command1);
    
            staff1.executeCommond();
        }
    
    }

    课程视频类:

    package com.geely.design.pattern.behavioral.command;
    
    /**
     * 课程视频类
     */
    public class CourseVideo {
        private String name;
    
        public CourseVideo(String name) {
            this.name = name;
        }
    
        public void openVideo(){
            System.out.println(this.name+"课程打开");
        }
    
        public void closeVideo(){
            System.out.println(this.name+"课程关闭");
        }
    }

    命令接口:

    package com.geely.design.pattern.behavioral.command;
    
    /**
     *  注:接口可以不传入属性,属性在实现类中注入
     */
    public interface Command {
    
        void execute();
    
    }

     打开课程视频命令类:

    package com.geely.design.pattern.behavioral.command;
    
    public class OpenCourseVideoCommand implements Command {
        private CourseVideo courseVideo ;
    
        public OpenCourseVideoCommand(CourseVideo courseVideo) {
            this.courseVideo = courseVideo;
        }
        @Override
        public void execute() {
            this.courseVideo.openVideo();
        }
    }

    关闭课程视频命令类:

    package com.geely.design.pattern.behavioral.command;
    
    public class CloseCourseVideoCommand implements Command {
        private CourseVideo courseVideo;
    
        public CloseCourseVideoCommand(CourseVideo courseVideo) {
            this.courseVideo = courseVideo;
        }
    
        @Override
        public void execute() {
            this.courseVideo.closeVideo();
        }
    }

    员工类:

    package com.geely.design.pattern.behavioral.command;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Staff {
        //这里用到 ArrayList的有序列表的属性,先进先出,后进后出
        private List<Command> commandList = new ArrayList<Command>();
    
        /**
         * 员工添加命令
         * @param command
         */
        public void addCommond(Command command){
           commandList.add(command);
        }
    
        /**
         * 员工执行命令
         */
        public void executeCommond(){
            for(int i=0;i<commandList.size();i++){
                commandList.get(i).execute();
            }
            //这个别忘了,最后把命令清空。
            commandList.clear();
        }
    }

    打印日志:

    豆豆哲学论课程关闭
    豆豆哲学论课程打开
    
    Process finished with exit code 0

     

  • 相关阅读:
    中国机读目录格式(CNMARC)
    smarty截取中文字符乱码问题?gb2312utf8
    smarty模板截取字符串乱码问题完美解决```````
    marc数据
    smarty精品教程三(高级篇)
    中国机读目录格式(CNMARC)附录
    smarty精品教程二(高级篇)
    QuickSkin简明教程
    smarty精品教程四(高级篇)可下载实例
    maven学习指南
  • 原文地址:https://www.cnblogs.com/1446358788-qq/p/12381521.html
Copyright © 2011-2022 走看看