类图
时序图
阀门接口
/** * 阀门接口 * @author administrator * */ public interface Valve { public String getName(); public void invoke( Context context ); }
基本阀门
/** * 基础阀门 * @author administrator * */ public class BasicValve implements Valve{ private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public BasicValve(String name) { super(); this.name = name; } /** * 阀门通用方法 */ public void invoke( Context context ){ System.out.println( name + "阀门完成操作。" ); } }
其它阀门
/** * 一般阀门对象 * @author administrator * */ public class CommValve implements Valve{ private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public CommValve(String name) { super(); this.name = name; } public void invoke( Context context ){ System.out.println( name + "阀门完成操作。" ); context.invokeNext(); } }
管道流
/** * 管道对象 * @author administrator * */ public class Pipeline { List<Valve> valves = new ArrayList<Valve>(); /** * 基础阀门 */ private Valve basic; public Valve getBasic() { return basic; } public void setBasic(Valve basic) { this.basic = basic; } public Pipeline(Valve basic) { super(); this.basic = basic; } /** * 添加 阀门 * @param v */ public void addValve( Valve v ){ valves.add( v ); } public void invoke(){ Context context = new Context( valves.iterator(),this ); context.invokeNext(); } }
上下文
/** * 上下文对象 * @author administrator * */ public class Context { private Iterator<Valve> valveIterator; private Pipeline pipeline; public Context(Iterator<Valve> valveIterator,Pipeline pipeline) { super(); this.valveIterator = valveIterator; this.pipeline = pipeline; } public void invokeNext(){ if( valveIterator.hasNext() ){ Valve valve = valveIterator.next(); valve.invoke( this ); } else{ pipeline.getBasic().invoke( this ); } } }
测试类
public class TestMain { /** * @param args */ public static void main(String[] args) { Pipeline line = new Pipeline( new BasicValve( "Basic" ) ); line.addValve( new CommValve( "A" ) ); line.addValve( new CommValve( "B" ) ); line.addValve( new CommValve( "C" ) ); line.addValve( new CommValve( "D" ) ); line.invoke(); } }
运行结果
A阀门完成操作。 B阀门完成操作。 C阀门完成操作。 D阀门完成操作。 Basic阀门完成操作。原文链接:https://www.f2er.com/javaschema/286776.html