命令模式

来源:互联网 发布:stussy 正品淘宝 编辑:程序博客网 时间:2024/06/16 13:46

某个方法需要完成某一个功能,完成这个功能的大部分步骤已经确定了,但可能有少量具体步骤无法确定,必须等到该方法时才可以确定。

package com.fcy.model;class ProcessArray{public void each(int[] target,Command cmd){cmd.process(target);}}interface Command{void process(int[] target);}public class CommandTest{public static void main(String[] args){ProcessArray pa=new ProcessArray();int[] target={3,-4,6,4};pa.each(target,new Command(){public void process(int[] target){for(int tmp:target){System.out.println("迭代输出目录数组的元素:"+tmp);}}});System.out.println("-------------------------");pa.each(target,new Command(){public void process(int[] target){int sum=0;for(int tmp:target){sum+=tmp;}System.out.println("数组元素的总和是:"+sum);}});}}

运行结果:


0 0
原创粉丝点击