package com.command;
public class NoodlesReceiver {
String name;
public NoodlesReceiver(String name){
this.name=name;
}
@Override
public void doSomeThing(int table_num,int food_count) {
// TODO Auto-generated method stub
System.out.println(name+"给"+table_num+"号桌下"+food_count+"碗面条");
}
}
package com.command;
public class BakeChickenWingReceiver {
String name;
public BakeChickenWingReceiver(String name){
this.name=name;
}
public void doSomeThing(int table_num,int food_count) {
// TODO Auto-generated method stub
System.out.println(name+"给"+table_num+"号桌来"+food_count+"个烤鸡翅");
}
}
package com.command;
public class client {
public static void main(String[] args) {
//初始化两个厨师
NoodlesReceiver noodlesReceiver=
new NoodlesReceiver("下面条的李师傅");
BakeChickenWingReceiver bakeChickenWingReceiver=
new BakeChickenWingReceiver("烤鸡翅的张师傅");
noodlesReceiver.doSomeThing(10, 1);
bakeChickenWingReceiver.doSomeThing(10, 3);
}
}
package com.command;
public class Waiter2 {
NoodlesReceiver noodlesReceiver;
BakeChickenWingReceiver bakeChickenWingReceiver;
String name;
public Waiter2(String name){
this.name=name;
//初始化两个厨师 一个服务员
noodlesReceiver=new NoodlesReceiver("下面条的李师傅");
bakeChickenWingReceiver=new BakeChickenWingReceiver("烤鸡翅的张师傅");
}
public void order(int table_num,int food_count,String food){
switch (food) {
case "鸡翅":
bakeChickenWingReceiver.doSomeThing(table_num, food_count);
break;
case "面条":
noodlesReceiver.doSomeThing(table_num, food_count);
break;
default:
System.out.println("sorry 小店没有您要点的食物");
break;
}
}
}
package com.command;
public class client2 {
public static void main(String[] args) {
Waiter2 waiter2=new Waiter2("小美");
waiter2.order(10, 1, "面条");
waiter2.order(10, 3, "鸡翅");
waiter2.order(10, 3, "馒头");
}
}
public interface IReceiver {
public void doSomeThing(int table_num,int food_count);
}
package com.command;
public interface ICommand {
public void action();
}
package com.command;
public class BakeChickenWingCommand implements ICommand{
int count;
int num;
IReceiver receiver;
public BakeChickenWingCommand(IReceiver receive,int count,int num){
this.receiver=receive;
this.count=count;
this.num=num;
}
@Override
public void action() {
// TODO Auto-generated method stub
receiver.doSomeThing(num, count);
}
}
public class Command {
int count;
int num;
IReceiver receiver;
public Command(IReceiver receive,int count,int num){
this.receiver=receive;
this.count=count;
this.num=num;
}
public void action() {
// TODO Auto-generated method stub
receiver.doSomeThing(num, count);
}
}
诸位看官以为如何. 别着急发表意见 想想再说.先看看命令模式的类图.
其实在去饭店吃饭的例子里,是可以的,因为不管厨师是做面条还是鸡翅,都可以用一个doSomeThing来代替.可换一个场景呢?古代皇帝,让臣下打仗,镇守地方,升官,贬职你还都用一个doSomeThing搞定吗?所以必须分层必须用接口.package com.command;
import java.util.ArrayList;
public class Waiter {
String name;
ArrayList list;
public Waiter(String name){
this.name=name;
}
public void addCommand(ICommand command){
if(list==null)
list=new ArrayList();
list.add(command);
}
public void removeCommand(ICommand command){
System.out.println("顾客取消订单");
list.remove(command);
}
public void submit(){
int n=list.size();
for(int i=0;i
package com.command;
public class Client {
public static void main(String[] args) {
//初始化两个厨师 一个服务员(可以理解为饭店聘请了这三个人)
NoodlesReceiver noodlesReceiver=
new NoodlesReceiver("下面条的李师傅");
BakeChickenWingReceiver bakeChickenWingReceiver=
new BakeChickenWingReceiver("烤鸡翅的张师傅");
Waiter waiter=new Waiter("小美");
//顾客发出三个命令
NoodelsCommand noodelsCommand1=
new NoodelsCommand(noodlesReceiver, 6, 3);
NoodelsCommand noodelsCommand2=
new NoodelsCommand(noodlesReceiver, 30, 2);
BakeChickenWingCommand bakeChickenWingCommand=
new BakeChickenWingCommand(bakeChickenWingReceiver, 4, 8);
//服务员记录三个命令后 又撤销一个
waiter.addCommand(noodelsCommand1);
waiter.addCommand(noodelsCommand2);
waiter.addCommand(bakeChickenWingCommand);
waiter.removeCommand(noodelsCommand2);
//服务员将菜单上交 厨师做菜
waiter.submit();
}
}