设计模式----命令模式(command pattern)
Group.java代码如下:
package com.designPattern.command;/** * * @author yangjianzhou * 项目组分成了三个组,每个组都要接受增删改的命令 */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.designPattern.command;public class RequirementGroup extends Group{@Overridepublic void find() {System.out.println("找到需求组!");}@Overridepublic void add() {System.out.println("客户要求增加一项需求!");}@Overridepublic void delete() {System.out.println("客户要求删除一项需求!");}@Overridepublic void change() {System.out.println("客户要求改变一项需求!");}@Overridepublic void plan() {System.out.println("客户要求变更需求计划!");}}
package com.designPattern.command;public class PageGroup extends Group{@Overridepublic void find() {System.out.println("找到美工组!");}@Overridepublic void add() {System.out.println("客户要求增加一个页面!");}@Overridepublic void delete() {System.out.println("客户要求删除一个页面!");}@Overridepublic void change() {System.out.println("客户要求改变一个页面!");}@Overridepublic void plan() {System.out.println("客户要求页面变更计划!");}}
package com.designPattern.command;public class CodeGroup extends Group{@Overridepublic void find() {System.out.println("找到代码组!");}@Overridepublic void add() {System.out.println("客户要求增加一项功能!");}@Overridepublic void delete() {System.out.println("客户要求删除一项功能!");}@Overridepublic void change() {System.out.println("客户要求改变一项功能!");}@Overridepublic void plan() {System.out.println("客户要求变更功能计划!");}}
package com.designPattern.command;/** * * @author yangjianzhou * 一系列命令 */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.designPattern.command;public class AddRequirementCommand extends Command{@Overridepublic void execute() {super.rg.find();super.rg.add();super.rg.plan();}}
package com.designPattern.command;public class DeletePageCommand extends Command{@Overridepublic void execute() {super.pg.find();super.pg.delete();super.pg.plan();}}
package com.designPattern.command;/** * * @author yangjianzhou * 命令接受者 */public class Invoker {private Command command;public void setCommand(Command command) {this.command = command;}public void action(){this.command.execute();}}
package com.designPattern.command;public class Client {public static void main(String[] args) {Invoker invoker = new Invoker();System.out.println("==================客户要求增加一项需求===================");Command command = new AddRequirementCommand();invoker.setCommand(command);invoker.action();System.out.println("==================客户要求删除一个页面===================");command = new DeletePageCommand();invoker.setCommand(command);invoker.action();}}
==================客户要求增加一项需求===================找到需求组!客户要求增加一项需求!客户要求变更需求计划!==================客户要求删除一个页面===================找到美工组!客户要求删除一个页面!客户要求页面变更计划!