Command,命令模式:将一个请求封装为一个对象,从而使你可以用不同的请求对客户进行参数化,对请求排队和记录请求日志,以及支持可撤销的操作。

 

package com.cbf4life;
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.cbf4life;

public class RequirementGroup extends Group {

public void find() {
System.out.println("找到需求组...");
}

public void add() {
System.out.println("客户要求增加一项需求...");
}

public void change() {
System.out.println("客户要求修改一项需求...");
}

public void delete() {
System.out.println("客户要求删除一项需求...");
}

public void plan() {
System.out.println("客户要求需求变更计划...");

}
} package com.cbf4life;

public class PageGroup extends Group {

public void find() {
System.out.println("找到美工组...");
}

public void add() {
System.out.println("客户要求增加一个页面...");
}

public void change() {
System.out.println("客户要求修改一个页面...");
}

public void delete() {
System.out.println("客户要求删除一个页面...");
}

public void plan() {
System.out.println("客户要求页面变更计划...");
}
}

package com.cbf4life;

public class CodeGroup extends Group {

public void find() {
System.out.println("找到代码组...");
}public void add() {
System.out.println("客户要求增加一项功能...");
}

public void change() {
System.out.println("客户要求修改一项功能...");
}

public void delete() {
System.out.println("客户要求删除一项功能...");
}public void plan() {
System.out.println("客户要求代码变更计划...");
}
}

package com.cbf4life.command;
import com.cbf4life.receiver.CodeGroup;
import com.cbf4life.receiver.PageGroup;
import com.cbf4life.receiver.RequirementGroup;

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.cbf4life.command;

public class AddRequirementCommand extends Command {

public void execute() {

super.rg.find();

super.rg.add();

super.rg.plan();
}
}

package com.cbf4life.command;
public class DeletePageCommand extends Command {

public void execute() {

super.pg.find();

super.rg.delete();

super.rg.plan();
}
} //接头人,就是Invoker 类的实现:
package com.cbf4life.invoker;
import com.cbf4life.command.Command;
public class Invoker {

private Command command;

public void setCommand(Command command){
this.command = command;
}
public void action(){
this.command.execute();
}
}

package com.cbf4life;
import com.cbf4life.command.AddRequirementCommand;
import com.cbf4life.command.Command;
import com.cbf4life.invoker.Invoker;

public class Client {
public static void main(String[] args) {

Invoker xiaoSan = new Invoker(); //接头人就是小三System.out.println("-------------客户要求增加一项需求-----------------");

Command command = new AddRequirementCommand();//接头人接收到命令
xiaoSan.setCommand(command);
//接头人执行命令
xiaoSan.action();
}
} package com.cbf4life;
import com.cbf4life.command.Command;
import com.cbf4life.command.DeletePageCommand;
import com.cbf4life.invoker.Invoker;
//客户
public class Client {
public static void main(String[] args) {
Invoker xiaoSan = new Invoker();
System.out.println("-------------客户要求删除一个页面-----------------");
//客户给我们下命令来
//Command command = new AddRequirementCommand();
Command command = new DeletePageCommand();
xiaoSan.setCommand(command);

xiaoSan.action();
}
}