ITeye博客

简单工厂模式

a11632 · 2012-03-16 10:20 · 设计模式 · 编程语言 · Markdown

public interface IProgram { Double getResult(Double a,Double b); }

public class AddProgram implements IProgram { public Double getResult( Double a, Double b ) { return a+b; }

}

public class OperateProgram { public static IProgram getProgram(char c){ IProgram iProgram = null ; switch ( c ) { case '+': iProgram = new AddProgram(); break; default: break; } return iProgram; } }

public class GetResult { public static void main( String args ) { // TODO Auto-generated method stub IProgram iProgram = OperateProgram.getProgram( '+' ); Double d = iProgram.getResult( 1.2, 2.2 ); System.out.println(d); }

}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。