命令模式的用途:具体的例子多线程队列请求

命令模式参考:http://blog.csdn.net/dengjili/article/details/79495454

描述来自于headfirst

命令可以将运算块打包(一个接受者和一组动作),然后将它传来传去,就像是一般的对象一样。现在,即使在命令对象被创建许久之后,运算依然可以被调用。事实上,它甚至可以在不同的线程中被调用。我们可以利用这样的特性衍生一些应用,例如:日程安排、线程池、工作队列等。

想象有一个工作队列:你在某一端添加命令,然后另一端则是线程。线程进行下面的工作:从队列中取出一个命令,调用它的execute()方法,等待这个调用完成,然后将此命令对象丢弃,再取出下一个命令…..

这里写图片描述

请注意, 工作队列类和惊醒计算的对象之间完全是解耦的。此刻线程可能在进行财务运算,下一个却在读取网络数据。工作队列对象不在乎到底做些什么,他们只知道取出命令对象,然后调用其execute()方法。类似的,它们只要是实现命令模式的对象,就可以放入 队列里,当线程可用时,就调用此对象的execute()方法。

给出了一个大致代码,及其测试

这里简单处理,预先加载需要处理的工作,然后统一调用,目的是使各工作之间互不影响。
(也可以采用多线程读写,一个读,一个写,可以参考这个列子更改)

receiver代码

数据库相关

package headfirst.hd.command.app.receiver;

public class DatabaseOp {

    public void add() {
        System.out.println("向数据库添加数据");
    }

    public void delete() {
        System.out.println("向数据库删除数据");
    }

    public void update() {
        System.out.println("向数据库更新数据");
    }

}

文件相关

package headfirst.hd.command.app.receiver;

public class FileOp {

    public void upload() {
        System.out.println("文件上传");
    }

    public void download() {
        System.out.println("文件下载");
    }

    public void md5() {
        System.out.println("对文件进行md5加密");
    }

}

网络相关

package headfirst.hd.command.app.receiver;

public class ScoketOp {

    public void send() {
        System.out.println("向网络中发送数据");
    }

    public void receive() {
        System.out.println("从网络中接收数据");
    }

}

Command 接口代码

package headfirst.hd.command.app.interfaces;

public interface Command {
    //简化操作,不带参数
    void execute();
}

concreteCommand代码,对应具体功能

package headfirst.hd.command.app.concreteCommand;

import headfirst.hd.command.app.interfaces.Command;
import headfirst.hd.command.app.receiver.DatabaseOp;

public class DbAdd implements Command {

    DatabaseOp op;

    public DbAdd(DatabaseOp op) {
        this.op = op;
    }

    @Override
    public void execute() {
        op.add();
    }


}
package headfirst.hd.command.app.concreteCommand;

import headfirst.hd.command.app.interfaces.Command;
import headfirst.hd.command.app.receiver.FileOp;

public class FileDownload implements Command {

    FileOp op;

    public FileDownload(FileOp op) {
        super();
        this.op = op;
    }

    @Override
    public void execute() {
        op.download();
    }


}
package headfirst.hd.command.app.concreteCommand;

import headfirst.hd.command.app.interfaces.Command;
import headfirst.hd.command.app.receiver.FileOp;

public class FileUpload implements Command {

    FileOp op;

    public FileUpload(FileOp op) {
        super();
        this.op = op;
    }

    @Override
    public void execute() {
        op.upload();
    }


}
package headfirst.hd.command.app.concreteCommand;

import headfirst.hd.command.app.interfaces.Command;
import headfirst.hd.command.app.receiver.ScoketOp;

public class ScoketReceive implements Command {

    ScoketOp op;

    public ScoketReceive(ScoketOp op) {
        this.op = op;
    }

    @Override
    public void execute() {
        op.receive();
    }


}

Invoker代码,及其辅助类

核心控制器SchedulerControl

package headfirst.hd.command.app.invoker;

import java.util.LinkedList;
import java.util.Queue;

import headfirst.hd.command.app.interfaces.Command;

public class SchedulerControl {

    Queue<Command> queue = new LinkedList<Command>();

    public void setScheduler(Command command) {
        queue.add(command);
    }

    //开启多线程
    public void startComand() throws Exception {
        Command command = queue.poll();
        while (command != null) {
            ThreadUtils.startThread(command);
            command = queue.poll();
        }
    }

}

线程辅助类ComandThread

package headfirst.hd.command.app.invoker;

import java.util.Random;

import headfirst.hd.command.app.interfaces.Command;

public class ComandThread implements Runnable {

    Command command;

    public ComandThread(Command command) {
        this.command = command;
    }

    @Override
    public void run() {
        System.out.println(command.getClass() + "开始");
        //随机延时 1到10秒,模拟任务需要很久的时间
        int nextInt = new Random().nextInt(10);
        try {
            Thread.sleep(nextInt * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        command.execute();

        System.out.println(command.getClass() + "结束, 共用时" + nextInt + "秒");
    }

}

线程公共方法ThreadUtils ,创建,并开启线程

package headfirst.hd.command.app.invoker;

import headfirst.hd.command.app.interfaces.Command;

public class ThreadUtils {

    //开启多线程
    public static void startThread(Command command) throws Exception {
        ComandThread target = new ComandThread(command);
        Thread thread = new Thread(target);
        thread.start();
    }

}

测试代码DriveTest

package headfirst.hd.command.app;

import headfirst.hd.command.app.concreteCommand.DbAdd;
import headfirst.hd.command.app.concreteCommand.FileDownload;
import headfirst.hd.command.app.concreteCommand.FileUpload;
import headfirst.hd.command.app.concreteCommand.ScoketReceive;
import headfirst.hd.command.app.invoker.SchedulerControl;
import headfirst.hd.command.app.receiver.DatabaseOp;
import headfirst.hd.command.app.receiver.FileOp;
import headfirst.hd.command.app.receiver.ScoketOp;

public class DriveTest {

    public static void main(String[] args) throws Exception {

        //Receiver
        DatabaseOp databaseOp = new DatabaseOp();
        FileOp fileOp = new FileOp();
        ScoketOp scoketOp = new ScoketOp();

        //concreteCommand
        DbAdd dbAdd = new DbAdd(databaseOp);
        FileDownload fileDownload = new FileDownload(fileOp);
        FileUpload fileUpload = new FileUpload(fileOp);
        ScoketReceive scoketReceive = new ScoketReceive(scoketOp);

        //Invoker绑定concreteCommand
        SchedulerControl control = new SchedulerControl();
        control.setScheduler(dbAdd);
        control.setScheduler(fileUpload);
        control.setScheduler(fileDownload);
        control.setScheduler(scoketReceive);
        //再次向数据库插入数据
        control.setScheduler(dbAdd);

        //开启工作队列
        control.startComand();
    }

}
class headfirst.hd.command.app.concreteCommand.FileUpload开始
class headfirst.hd.command.app.concreteCommand.DbAdd开始
class headfirst.hd.command.app.concreteCommand.FileDownload开始
class headfirst.hd.command.app.concreteCommand.ScoketReceive开始
class headfirst.hd.command.app.concreteCommand.DbAdd开始
文件下载
class headfirst.hd.command.app.concreteCommand.FileDownload结束, 共用时1秒
文件上传
向数据库添加数据
class headfirst.hd.command.app.concreteCommand.DbAdd结束, 共用时5秒
class headfirst.hd.command.app.concreteCommand.FileUpload结束, 共用时5秒
从网络中接收数据
class headfirst.hd.command.app.concreteCommand.ScoketReceive结束, 共用时6秒
向数据库添加数据
class headfirst.hd.command.app.concreteCommand.DbAdd结束, 共用时8
测试结果

满足工作队列要求,各工作之间互不影响

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值