设计模式(java版)——行为型——命令模式

本文介绍了一种使用Java实现的命令模式,通过具体的课程视频开关案例,展示了如何将请求封装为对象,以便使用不同的请求参数化对象,将请求排队、记录日志等。此模式适用于系统需要在不同的时间点执行请求,或者需要撤销请求的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目源码已提交github:https://github.com/ckl001/designPattem_java

package com.Ljava.design.pattem.behavioral.command;

/**
 * @Auther 20173
 * @Date 2019-4-10 15:03
 * @Des 命令 (接口)
 **/
public interface Command {

    void execute();
}

package com.Ljava.design.pattem.behavioral.command;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther 20173
 * @Date 2019-4-10 15:06
 * @Des 负责视频开关的  工作人员
 **/
public class Staff {
    private List<Command> commandList = new ArrayList<>();

    public void addCommand(Command command){
        commandList.add(command);
    }

    public void executeCommands(){
        for(Command command : commandList){
            command.execute();
        }
        commandList.clear();
    }
}

package com.Ljava.design.pattem.behavioral.command;

/**
 * @Auther 20173
 * @Date 2019-4-10 15:01
 * @Des 课程视频
 **/
public class CourseVideo {

    private String name;

    public CourseVideo(String name) {
        this.name = name;
    }

    public void open(){
        System.out.println("打开"+ this.name + "课程视频");
    }

    public void close(){
        System.out.println("关闭"+ this.name + "课程视频");
    }
}

package com.Ljava.design.pattem.behavioral.command;

/**
 * @Auther 20173
 * @Date 2019-4-10 15:04
 * @Des 命令 打开视频
 **/
public class OpenCourseVideoCommand implements Command {

    private CourseVideo courseVideo;

    public OpenCourseVideoCommand(CourseVideo courseVideo) {
        this.courseVideo = courseVideo;
    }

    @Override
    public void execute() {
        courseVideo.open();
    }
}

package com.Ljava.design.pattem.behavioral.command;

/**
 * @Auther 20173
 * @Date 2019-4-10 15:06
 * @Des 命令 关闭视频
 **/
public class CloseCourseVideoCommand implements Command {

    private CourseVideo courseVideo;

    public CloseCourseVideoCommand(CourseVideo courseVideo) {
        this.courseVideo = courseVideo;
    }

    @Override
    public void execute() {
        courseVideo.close();
    }
}

package com.Ljava.design.pattem.behavioral.command;

/**
 * @Auther 20173
 * @Date 2019-4-10 15:09
 * @Des TODO
 **/
public class Test {

    public static void main(String[] args) {
        CourseVideo courseVideo = new CourseVideo("Java课程视频 -- ck.com");
        OpenCourseVideoCommand openCourseVideoCommand = new OpenCourseVideoCommand(courseVideo);
        CloseCourseVideoCommand closeCourseVideoCommand = new CloseCourseVideoCommand(courseVideo);

        Staff staff = new Staff();
        staff.addCommand(openCourseVideoCommand);
        staff.addCommand(closeCourseVideoCommand);
        staff.executeCommands();

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值