静止的流水的学习日志
静止的流水
全部博文(210)
Windows(1)
Linux(31)
2011年(34)
2010年(121)
2009年(37)
2008年(18)
cynthia
wojiuzhu
jd_lzh
鄙人姓王
Jack_Tan
fanbiubi
猜我咯
aaccp1
chrisy52
分类: Java
2011-02-25 16:13:25
package hello;/** * 抽象工厂简单示例 * @author HZ20232 * */public class FactoryMethodPattern{ public static void main(String args[]){ Factory longFactory = new LongWeaponFactory(); Gun longGun = longFactory.createGun(); Connon longConnon = longFactory.createConnon(); longGun.Beng(); longConnon.Beng(); Factory shorFactory = new ShortWeaponFactory(); Gun shorGun = shorFactory.createGun(); Connon shortConnon = shorFactory.createConnon(); shorGun.Beng(); shortConnon.Beng(); }}interface Gun{ void Beng();}interface Connon{ void Beng();}class LongGun implements Gun{ public void Beng(){ System.out.println("Big Gun Beng!"); }}class ShortGun implements Gun{ public void Beng(){ System.out.println("Little Gun Beng!"); }}class LongConnon implements Connon{ public void Beng(){ System.out.println("Big Connon Beng!"); }}class ShortConnon implements Connon{ public void Beng(){ System.out.println("Little Connon Beng!"); }}interface Factory{ Gun createGun(); Connon createConnon();}class LongWeaponFactory implements Factory{ public Gun createGun(){ return new LongGun(); } public Connon createConnon(){ return new LongConnon(); }}class ShortWeaponFactory implements Factory{ public Gun createGun(){ return new ShortGun(); } public Connon createConnon(){ return new ShortConnon(); }}
package hello;import java.io.*;/** * 简单工厂简单示例 * @author HZ20232 * */public class FactoryMethodPattern{ public static void main(String args[]){ try { System.out.println("请输入数字1:"); String strA = new BufferedReader(new InputStreamReader(System.in)) .readLine(); System.out.println("请输入运算符:"); String stroperate = new BufferedReader(new InputStreamReader( System.in)).readLine(); System.out.println("请输入数字2:"); String strB = new BufferedReader(new InputStreamReader(System.in)) .readLine(); Operation oper; OperationFactory factory = new OperationFactory(); oper=factory.getOperation(stroperate); oper.setA(Integer.parseInt(strA)); oper.setB(Integer.parseInt(strB)); int result = oper.operator(); System.out.println(result); } catch (Exception e) { } }}abstract class Operation{ int operandA; int operandB; public Operation(){ this.setA(0); this.setB(0); } public Operation(int a,int b){ this.setA(a); this.setB(b); } public void setA(int numA){ this.operandA = numA; } public int getA(){ return this.operandA; } public void setB(int numB){ this.operandB = numB; } public int getB(){ return this.operandB; } public abstract int operator();}class AddOperation extends Operation{ public int operator(){ return this.operandA+this.operandB; }}class MulOperation extends Operation{ public int operator(){ return this.operandA*this.operandB; }}interface Factory{ Operation getOperation(String a);}class OperationFactory implements Factory{ public Operation getOperation(String operator){ Operation operation = null; if("+".equalsIgnoreCase(operator)) operation = new AddOperation(); else if("*".equalsIgnoreCase(operator)) operation = new MulOperation(); return operation; }}
上一篇:Java设计模式-工厂方法模式(一)
下一篇:Java设计模式-单态模式(三)
chinaunix网友2011-03-06 08:58:12
很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com
登录 注册