• 设计模式-行为型模式-命令模式


    设计模式-行为型模式-命令模式

    基础

    以一个MIS系统为栗子

    MIS 管理信息系统(Management Information System,MIS)是一个以人为主导的,利用计算机硬件、软件和网络设备,进行信息的收集、传递、存储、加工、整理的系统,以提高组织的经营效率。

    代码如下

    public abstract class Group{
    	// 甲乙双方公开办公,如果要和某个组讨论,要先找到这个组
    	public abstract void find();
    	// 要求增加功能
    	public abstract void add();
    	// 删除功能
    	public abstract void delete();
    	// 被要求修改功能
    	public abstract void change();
    	// 被要求给出变更计划
    	public abstract void plan();
    }
    

    抽象类中的都是命令方式。
    进行需求组

    public class RequirementGroup extends Group{
    	// 客户要求需求进行更改
    	public void find(){
    		
    	}
    	// 客户要求增加需求
    	public void add(){
    	}
    	// 客户要求修改需求
    	public void change(){
    	}
    	// 客户要求删除需求
    	public void delete(){
    	}
    	// 客户要求给出变更计划
    	public void plan(){
    	}
    }
    
    // 美工组
    public class PageGroup extends Group{
    	// 找到美工组
    	public void find(){
    	}
    	// 客户要求增加页面
    	public void add(){
    	}
    	// 客户要求对现有界面修改
    	public void change(){
    	}
    	// 客户要求删除页面
    	public void delete(){
    	}
    	// 给出计划
    	public void plan(){
    	}
    }
    
    // 代码组
    public class CodeGroup extends Group {
    	// 客户要求过去谈
    	public void find(){
    	}
    	// 客户要求增加功能
    	public void add(){
    	}
    	// 客户要求修改功能
    	public void change(){
    	}
    	// 客户要求删除功能
    	public void delete(){
    	}
    	// 客户要求变更计划
    	public void plan(){
    	}
    }
    

    最后书写场景类

    // 场景类
    public class Client{
    	public static void main(String[] args){
    		// 客户找到需求要求谈需求进行修改
    		Grop rg = new RequirementGroup();
    		// 找到需求组
    		rg.find();
    		// 增加需求组
    		rg.add();
    		// 变更计划
    		rg.plan();
    	}
    }
    

    此时客户要求多一个界面

    public class Client {
    	public static void main(String[] args){
    		Group pg = new PangeGroup();
    		// 找到需求组
    		pg.find();
    		// 删除需求组
    		pg.delete();
    		// 需求变更
    		pg.plan();
    	}
    }
    

    变更

    客户每次都要单独找一个,非常复杂,设置一个负责人,负责和客户和需求,美工之间的连接。

    // 抽象出命令类
    public abstract class Command{
    	// 定义三个组,确保子类可以直接使用
    	protected RequirementGroup rg = new RequirementGroup();	// 需求组
    	protected PageGroup pg = new PageGroup();	// 美工组
    	protected CodeGroup cg = new CodeGroup(); // 代码组
    	public abstract void execute();	// 做什么事情
    }
    

    增加需求的命令

    public class AddRequirementCommand extends  Command{
    	// 增加一项需求命令
    	public void execute(){
    		// 找到需求组
    		super.rg.find();
    		// 增加一份需求
    		super.rg.add();
    		// 给出计划
    		super.rg.plan();
    	}
    }
    

    增加删除页面的命令

    public class DeletePageCommand extends Command{
    	public void execute(){
    		// 找到页面组
    		super.pg.find();
    		// 删除页面
    		super.rg.delete();
    		// 给出计划
    		super.rg.plan();
    	}
    }
    

    最后再将这也命令封装,成一个负责人

    public class invoker {
    	// 命令
    	private Command command;
    	// 客户发出命令
    	public void setCommand(Command command){
    		this.command = command;
    	}
    	// 客户自行命令
    	public void action(){
    		this.command.execute();
    	}
    }
    

    最后场景类如下

    public class Client{
    	public static void main(String[] args){
    		// 定义接头人
    		invoker xiaoSan = new Invoker();	// 接头人
    		// 客户增加需求
    		// 客户下命令
    		Command command = new AddRequirementCommand();
    		// 街头人收到命令
    		xiaoSan.setCommand(command);
    		// 街头人执行命令
    		xiaoSan.action();
    	}
    }
    

    继续更改

    由于负责人的抽象类中声明了所有的,所以可以分离
    代码如下

    public abstract class Command {
    	// 定义子类全局共享变量
    	protected final Receiver receiver;
    	// 实现类名需要接收
    	public Command(Receiver _receiver){
    		this.receiver = _receiver;
    	}
    	// 每个命令需要有执行
    	public abstract void execute();
    }
    

    然后在Command中声明接收者。
    通过构造函数指定

    public class ConcreteCommand1 extends Command{
    	// 声明自己默认接收者
    	public ConcreteCommand1(){
    		super(new ConcreteReciver1());
    	}
    	// 设置新的接受
    	public ConcreteCommand1(Receiver _receiver){
    		super(_receiver);
    	}
    	// 处理
    	public void execute(){
    		super.receiver.doSomething();
    	}
    }
    
    // 声明场景
    public class Client{
    	public static void main(String[] args){
    		invoker invoker = new invoker();
    		// 定义要发送的命令
    		Command command = new ConcreteCommand1();
    		invoker.setCommand(command);
    		invoker.action();
    	}
    }
    

    总结

    将需求封装成命令,然后再负责人当中封装,最后,需要的时候,直接调用负责人,将命令传入进去即可。
    www.iming.info

    在无知的道路上缓步前行
  • 相关阅读:
    [SCOI2005]骑士精神
    [SCOI2005]超级格雷码
    [SDOI2013]淘金
    [SCOI2014]方伯伯的商场之旅
    P4317 花神的数论题
    RSA算法原理(一)
    PKI 笔记
    字符串解析运用-将字符串分解为多个整数,求各整数之和(华为oj)
    pycharm快捷键、常用设置、配置管理
    启动ipython notebook(jupyter)
  • 原文地址:https://www.cnblogs.com/melovemingming/p/10080455.html
Copyright © 2020-2023  润新知