/*class Barbecuer{
public void BakeMutton() {
System.out.println("烤羊肉串");
}
public void BakeChickenWing() {
System.out.println("烤鸡翅");
}
}
public class Main {
public static void main(String args[])
{
Barbecuer b = new Barbecuer();
b.BakeMutton();
b.BakeMutton();
b.BakeMutton();
b.BakeChickenWing();
b.BakeMutton();
b.BakeMutton();
b.BakeChickenWing();
}
}*/
/*class Barbecuer{
public void BakeMutton() {
System.out.println("烤羊肉串");
}
public void BakeChickenWing() {
System.out.println("烤鸡翅");
}
}
abstract class Command{
protected static Barbecuer receiver;
public Command(Barbecuer receiver) {
super();
this.receiver = receiver;
}
public abstract void ExcuteCommand();
}
class BakeMuttonCommand extends Command{
public BakeMuttonCommand(Barbecuer receiver) {
super(receiver);
// TODO Auto-generated constructor stub
}
public void ExcuteCommand() {
// TODO Auto-generated method stub
receiver.BakeMutton();
}
}
class BakeChickenWingCommand extends Command{
public BakeChickenWingCommand(Barbecuer receiver) {
super(receiver);
// TODO Auto-generated constructor stub
}
public void ExcuteCommand() {
// TODO Auto-generated method stub
receiver.BakeChickenWing();
}
}
class Waiter{
private Command command;
public void SetOrder(Command command) {
this.command = command;
}
public void Notify() {
command.ExcuteCommand();
}
}
public class Main {
public static void main(String args[])
{
Barbecuer b = new Barbecuer();
Command bm1 = new BakeMuttonCommand(b);
Command bm2 = new BakeMuttonCommand(b);
Command bcw = new BakeChickenWingCommand(b);
Waiter w = new Waiter();
w.SetOrder(bm1);
w.Notify();
w.SetOrder(bm2);
w.Notify();
w.SetOrder(bcw);
w.Notify();
}
}*/
/*class Barbecuer{
public void BakeMutton() {
System.out.println("烤羊肉串");
}
public void BakeChickenWing() {
System.out.println("烤鸡翅");
}
}
abstract class Command{
protected static Barbecuer receiver;
public Command(Barbecuer receiver) {
super();
this.receiver = receiver;
}
public abstract void ExcuteCommand();
}
class BakeMuttonCommand extends Command{
public BakeMuttonCommand(Barbecuer receiver) {
super(receiver);
// TODO Auto-generated constructor stub
}
public void ExcuteCommand() {
// TODO Auto-generated method stub
receiver.BakeMutton();
}
}
class BakeChickenWingCommand extends Command{
public BakeChickenWingCommand(Barbecuer receiver) {
super(receiver);
// TODO Auto-generated constructor stub
}
public void ExcuteCommand() {
// TODO Auto-generated method stub
receiver.BakeChickenWing();
}
}
class waiterlist{
private ArrayList<Command> orders = new ArrayList<Command>();
public void SetOrder(Command command) {
if(command.getClass().getName()=="BakeChickenWingCommand") {
System.out.println("服务员:鸡翅没有了");
}
else {
orders.add(command);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("增加订单:"+command.getClass().getName()+"时间:"+df.format(new Date()));
}
}
public void CancleOrder(Command command) {
orders.remove(command);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("取消订单:"+command.getClass().getName()+"时间:"+df.format(new Date()));
}
public void Notify() {
Iterator<Command> cmd = orders.iterator();
while(cmd.hasNext()) {
cmd.next().ExcuteCommand();
}
}
}
public class Main {
public static void main(String args[])
{
Barbecuer b = new Barbecuer();
Command bm1 = new BakeMuttonCommand(b);
Command bm2 = new BakeMuttonCommand(b);
Command bcw = new BakeChickenWingCommand(b);
waiterlist w = new waiterlist();
w.SetOrder(bm1);
w.SetOrder(bm2);
w.SetOrder(bcw);
w.CancleOrder(bm1);
w.Notify();
}
}*/
//命令模式
abstract class Command{
protected Receiver receiver;
public Command(Receiver receiver) {
super();
this.receiver = receiver;
}
abstract public void Execute();
}
class ConcreteCommand extends Command{
public ConcreteCommand(Receiver receiver) {
super(receiver);
}
public void Execute() {
// TODO Auto-generated method stub
receiver.Action();
}
}
class Invoker{
private Command command;
public void setCommand(Command command) {
this.command = command;
}
public void ExecuteCommand() {
command.Execute();
}
}
class Receiver{
public void Action() {
System.out.println("执行请求");
}
}
public class Main {
public static void main(String args[])
{
Receiver r = new Receiver();
Command c = new ConcreteCommand(r);
Invoker i = new Invoker();
i.setCommand(c);
i.ExecuteCommand();
}
}
命令模式一步步实现
最新推荐文章于 2024-07-15 08:00:00 发布