/**
* 执行的接口
*/
public abstract class Command
{
/**
* 执行的方法
*/
public abstract void extecute();
}
/**
* 军官
*/
public class Officer
{
private Command[] command;
/**
* 构造函数
* @param command
*/
public Officer(Command[] command)
{
this.command = command;
}
/**
* 命令的方法
*/
public void order()
{
if (command != null)
{
for (Command comm : command)
{
comm.extecute();
}
}
}
}
/**
* 士兵
*/
public class Soldier
{
/**
* 跑步
*/
public void getRun()
{
System.out.println("士兵跑步..."); //$NON-NLS-1$
}
/**
* 唱歌
*/
public void getSing()
{
System.out.println("士兵唱歌..."); //$NON-NLS-1$
}
}
/**
* 跑步的命令
*/
public class RunCommand extends Command
{
private Soldier soldier;
/**
* 构造函数
* @param soldier
*/
public RunCommand(Soldier soldier)
{
this.soldier = soldier;
}
/** (此注释不是Javadoc注释)
* @see com.huawei.xbliuc.command.Command#extecute()
*/
@Override
public void extecute()
{
soldier.getRun();
}
}
/**
* 唱歌的命令
*/
public class SingCommand extends Command
{
private Soldier soldier;
/**
* 构造函数
* @param soldier
*/
public SingCommand(Soldier soldier)
{
this.soldier = soldier;
}
/**
* 执行的方法
*/
@Override
public void extecute()
{
soldier.getSing();
}
}
public static void main(String[] args)
{
Soldier soldier = new Soldier();
Command command = new RunCommand(soldier);
Command command2 = new SingCommand(soldier);
// 不同的长官给同一个士兵发送不同的命令
Officer officer = new Officer(new Command[] {command, command2});
officer.order();
}