Java设计模式之命令模式

命令模式由三部分组成:
Receiver:命令接收者,最终执行命令的角色
Command:待执行的命令
Invoker:调用者,接收到Command后让Receiver执行
代码来自《您的设计模式》.
Receiver:


package com.pocketdigi.patterns.command;
/**
 * Receiver,命令执行者
 */
public abstract class Group {
    public abstract void find();
    public abstract void add();
    public abstract void delete();
    public abstract void change();
    public abstract void plan();
}


package com.pocketdigi.patterns.command;
/**
 * 代码组
 */
public class CodeGroup extends Group {

    @Override
    public void find() {
        // TODO Auto-generated method stub
        System.out.println("找到代码组");
    }

    @Override
    public void add() {
        // TODO Auto-generated method stub
        System.out.println("客户要求增加一项功能");

    }

    @Override
    public void delete() {
        // TODO Auto-generated method stub
        System.out.println("客户要求删除一项功能");
    }

    @Override
    public void change() {
        // TODO Auto-generated method stub
        System.out.println("客户要求修改一项功能");
    }

    @Override
    public void plan() {
        // TODO Auto-generated method stub
        System.out.println("客户要求代码变更计划");
    }

}


package com.pocketdigi.patterns.command;
/**
 * 美工组
 */
public class PageGroup extends Group {

    @Override
    public void find() {
        // TODO Auto-generated method stub
        System.out.println("找到美工组");
    }

    @Override
    public void add() {
        // TODO Auto-generated method stub
        System.out.println("客户要求增加一个页面 ");

    }

    @Override
    public void delete() {
        // TODO Auto-generated method stub
        System.out.println("客户要求删除一个页面");
    }

    @Override
    public void change() {
        // TODO Auto-generated method stub
        System.out.println("客户要求修改一个页面");
    }

    @Override
    public void plan() {
        // TODO Auto-generated method stub
        System.out.println("客户要求页面变更计划");
    }

}

package com.pocketdigi.patterns.command;

/**
 * 需求组
 */
public class RequirementGroup extends Group {

    @Override
    public void find() {
        // TODO Auto-generated method stub
        System.out.println("找到需求组");
    }

    @Override
    public void add() {
        // TODO Auto-generated method stub
        System.out.println("客户要求增加一项需求 ");

    }

    @Override
    public void delete() {
        // TODO Auto-generated method stub
        System.out.println("客户要求删除一项需求");
    }

    @Override
    public void change() {
        // TODO Auto-generated method stub
        System.out.println("客户要求修改一项需求");
    }

    @Override
    public void plan() {
        // TODO Auto-generated method stub
        System.out.println("客户要求需求变更计划");
    }

}

Command:


package com.pocketdigi.patterns.command;

public abstract class Command {
    protected RequirementGroup rg=new RequirementGroup();
    protected PageGroup pg=new PageGroup();
    protected CodeGroup cg=new CodeGroup();
    
    public abstract void execute();
}

package com.pocketdigi.patterns.command;
/**
 * 添加需求Command
 */
public class AddRequirementCommand extends Command {

    @Override
    public void execute() {
        // TODO Auto-generated method stub
        super.rg.find();
        super.rg.add();
        super.rg.plan();
    }

}


package com.pocketdigi.patterns.command;
/**
 * 添加需求Command
 */
public class DeletePageCommand extends Command {

    @Override
    public void execute() {
        // TODO Auto-generated method stub
        super.pg.find();
        super.pg.delete();
        super.pg.plan();
    }

}

Invoker:


package com.pocketdigi.patterns.command;

/**
 * 命令接收,执行
 */
public class Invoker {
    private Command command;
    public void setCommand(Command command)
    {
        this.command=command;
    }
    public void action()
    {
        command.execute();
    }
}

使用:


        
        //命令模式
        Invoker invoker=new Invoker();
        invoker.setCommand(new DeletePageCommand());
        invoker.action();

© 2014, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记

发表评论

电子邮件地址不会被公开。 必填项已用*标注