命令模式

package frank;
public class App
{
	public static void main(String[] args)
	{
		int[] array = new int[]{0,1,4,5,3,4};
		ProcessArray pa = new ProcessArray();
		pa.process(array,new PrintCommand());//相同的方法,不同的输出
		pa.process(array,new AddCommand());//相同的方法,不同的输出
	}
}
interface Command
{
	public abstract void process(int[] array);
}
class ProcessArray
{
	public void process(int[] array,Command cmd)
	{
		cmd.process(array);
	}
}
class PrintCommand implements Command
{
	public void process(int[] array)
	{
		for(int item : array)
		{
			System.out.println(item);
		}
	}
}
class AddCommand implements Command
{
	public void process(int[] array)
	{
		int sum = 0;
		for(int item : array)
		{
			sum += item;
		}
		System.out.println(sum);
	}
}

  

posted on 2013-08-07 16:20  wp456  阅读(162)  评论(0)    收藏  举报

导航