首页 > 代码库 > 设计模式-12-命令模式

设计模式-12-命令模式

简介:以对象来代表实际行动,命令对象可以把行动(action) 及其参数封装起来,这些行动可以被

  • 重复多次
  • 取消(如果该对象有实现的话)
  • 取消后又再重做

目的:

总结:

 

组成:对象,行动
1,对象

package com.design.l.command;
/**
 * 程序设计员
 */
public class Programmer {
    
    private String userName;
    public Programmer(String userName) {
        super();
        this.userName = userName;
    }
    
    
    public void handleDemand(){
        System.out.println("程序员 : " + userName + " ,正在处理新需求 ... ...");
    }
    public void handleBug(){
        System.out.println("程序员 : " + userName + " ,正在处理Bug ... ...");
    }
    public void handleProblem(){
        System.out.println("程序员 : " + userName + " ,正在处理线上问题 ... ...");
    }

    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
}

 

package com.design.l.command;

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

/**
 * 产品经理
 */
public class ProductManager {
    
    //任务集合
    private List<TaskService> taskList;
    //程序员
    private List<Programmer> programmerList;
    private static final int TASK_MAX_NUM = 4;//任务最大数量
    private int idx = 0;

    public ProductManager(Programmer ... programmers) {
        super();
        this.taskList = new ArrayList<>();
        this.programmerList = Arrays.asList(programmers);
    }
    
    //新增任务
    public void addTaskToProduct(TaskService taskService){
        taskList.add(taskService);
    }
    
    //分配任务
    public void assignments(){
        TaskService[] copy = new TaskService[taskList.size() > TASK_MAX_NUM ? taskList.size() - TASK_MAX_NUM : 0];
        for ( int i = 0; i < TASK_MAX_NUM && i < taskList.size(); i ++){
            taskList.get(i).handle();
        }
        
        System.arraycopy(taskList.toArray(), TASK_MAX_NUM > taskList.size() ? taskList.size() : TASK_MAX_NUM, copy, 0, copy.length);
        taskList = Arrays.asList(copy);
    }
    
    //获取程序员
    public Programmer chooseProgrammer(){
        return programmerList.get(idx == programmerList.size() ? 0 : idx ++); 
    }
    
    //打印出未处理的任务
    public void printTask(){
        if(taskList == null || taskList.size() == 0){
            System.out.println("----无任务----");
            return ;
        }
        
        System.out.println("----剩余任务----");
        for(TaskService task : taskList){
            System.out.println(task);
        }
    }

}

 

package com.design.l.command;
/**
 * 销售人员
 */
public class Sales {
    
    private String userName;
    private ProductManager productManager;//产品经理
    
    public Sales(String userName) {
        super();
        this.userName = userName;
    }
    public Sales(String userName, ProductManager productManager) {
        super();
        this.userName = userName;
        this.productManager = productManager;
    }
    
    
    public void putDemand(){
        System.out.println("业务员 : " + userName + ", 提出新需求  ... ... ");
        productManager.addTaskToProduct(new DemandTaskServiceImpl(productManager.chooseProgrammer()));
    }
    public void putBug(){
        System.out.println("业务员 : " + userName + ", 提出新Bug  ... ... ");
        productManager.addTaskToProduct(new BugTaskServiceImpl(productManager.chooseProgrammer()));
    }
    public void putProblem(){
        System.out.println("业务员 : " + userName + ", 提出线上问题     ... ... ");
        productManager.addTaskToProduct(new ProblemTaskServiceImpl(productManager.chooseProgrammer()));
    }
    
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public ProductManager getProductManager() {
        return productManager;
    }
    public void setProductManager(ProductManager productManager) {
        this.productManager = productManager;
    }

}

 

2,行为

package com.design.l.command;
/**
 * 任务接口
 */
public interface TaskService {

    /**
     * 任务处理方法
     */
    void handle();
}

 

package com.design.l.command;
/**
 * 新需求任务
 */
public class DemandTaskServiceImpl implements TaskService {
    

    private Programmer programmer;
    public DemandTaskServiceImpl(Programmer programmer) {
        super();
        this.programmer = programmer;
    }


    @Override
    public void handle() {
        programmer.handleDemand();
    }

}

 

package com.design.l.command;
/**
 * Bug任务
 */
public class BugTaskServiceImpl implements TaskService {
    
    private Programmer programmer;
    public BugTaskServiceImpl(Programmer programmer) {
        super();
        this.programmer = programmer;
    }

    @Override
    public void handle() {
        programmer.handleBug();
    }

}

 

package com.design.l.command;
/**
 * 线上问题任务
 */
public class ProblemTaskServiceImpl implements TaskService {

    private Programmer programmer;
    public ProblemTaskServiceImpl(Programmer programmer) {
        super();
        this.programmer = programmer;
    }

    @Override
    public void handle() {
        programmer.handleProblem();
    }

}

 

3,Main

package com.design.l.command;
/**
 * 12-命令模式
 */
public class MainTest {
    
    public static void main(String[] args) {
        
        Programmer programmerA = new Programmer("程序员A");
        ProductManager productManager = new ProductManager(programmerA);
        Sales salesA = new Sales("业务员A", productManager);
        Sales salesB = new Sales("业务员B", productManager);
        Sales salesC = new Sales("业务员C", productManager);
        Sales salesD = new Sales("业务员D", productManager);
        
        salesA.putDemand();
        salesA.putBug();
        salesB.putProblem();
        salesC.putBug();
        salesD.putProblem();
        
        productManager.assignments();
        productManager.printTask();
        
        productManager.assignments();
        productManager.printTask();
    }

}

4,Result

业务员 : 业务员A, 提出新需求  ... ... 
业务员 : 业务员A, 提出新Bug  ... ... 
业务员 : 业务员B, 提出线上问题     ... ... 
业务员 : 业务员C, 提出新Bug  ... ... 
业务员 : 业务员D, 提出线上问题     ... ... 
程序员 : 程序员A ,正在处理新需求 ... ...
程序员 : 程序员A ,正在处理Bug ... ...
程序员 : 程序员A ,正在处理线上问题 ... ...
程序员 : 程序员A ,正在处理Bug ... ...
----剩余任务----
com.design.l.command.ProblemTaskServiceImpl@15db9742
程序员 : 程序员A ,正在处理线上问题 ... ...
----无任务----

 

设计模式-12-命令模式