/**
  *简单工厂模式其实不是23种设计模式之一,但是它是创建型模式的最基础的内容,
  *工厂方法和抽象工厂都是在它的基础上的改良版。
  *关于简单工厂模式的优缺点,请看设计模式(1)------工厂类设计模式。
  *
  *
  *它是对对象实例化的过程和需要实例化的对象进行细节上的封装。
  *
  *  优点:工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关类。对于客户端来说,去除了与具体产品的依赖。

  *缺点:当需要增加产生一个新对象实例的方法时,需要修改工厂类,这样,工厂类就违背了“开-闭原则”。
  *
  */


  1package simpleFactroy;
  2
  3public class Operation  {
  4    private double dNumberA;
  5    private double dNumberB;
  6    
  7    public double getResults(){
  8        return 0l ;
  9    }

 10
 11    public double getdNumberA() {
 12        return dNumberA;
 13    }

 14
 15    public void setdNumberA(double dNumberA) {
 16        this.dNumberA = dNumberA;
 17    }

 18
 19    public double getdNumberB() {
 20        return dNumberB;
 21    }

 22
 23    public void setdNumberB(double dNumberB) {
 24        this.dNumberB = dNumberB;
 25    }

 26
 27    public Operation(double dNumberA, double dNumberB) {
 28        this.dNumberA = dNumberA;
 29        this.dNumberB = dNumberB;
 30    }

 31
 32    public Operation(double dNumberA) {
 33        this.dNumberA = dNumberA;
 34    }

 35
 36    public Operation() {
 37        
 38    }

 39    
 40
 41}

 42
 43
 44package simpleFactroy;
 45
 46public class OperationAdd extends Operation {
 47    public double getResults() {
 48        return  getdNumberA() + getdNumberB() ;
 49    }

 50}

 51
 52
 53package simpleFactroy;
 54
 55public class OperationSub extends Operation {
 56
 57    public double getResults() {    
 58        return  getdNumberA() - getdNumberB() ;
 59    }

 60
 61}

 62
 63
 64
 65package simpleFactroy;
 66
 67public class OperationSqrt extends Operation {
 68
 69    @Override
 70    public double getResults() {
 71        return Math.sqrt(getdNumberA());
 72    }

 73    
 74
 75}

 76
 77
 78
 79package simpleFactroy;
 80
 81public class OperationFactroy {
 82    public static Operation getOperation(String opr){
 83        Operation operation = null ;
 84        if(opr.equals("+")){
 85            operation = new OperationAdd();
 86        }
else if(opr.equals("-")){
 87            operation = new OperationSub();
 88        }
else{
 89            operation = new OperationSqrt();
 90        }

 91        return operation ;
 92    }

 93
 94}

 95
 96
 97
 98package simpleFactroy;
 99
100import java.util.Scanner;
101
102
103public class TestCalculate {
104    public static void main(String[] args)throws Exception{
105        while (true{
106            Scanner src = new Scanner(System.in);
107            double a = src.nextDouble();
108            String operation = src.next();
109            double b = src.nextDouble();
110            System.out.println(calculate(a, operation, b));
111            System.out.println(calculate(a));
112        }

113    }

114    
115    public static double calculate(double a,String operation, double b ){
116        Operation opr = OperationFactroy.getOperation(operation);
117        opr.setdNumberA(a);
118        opr.setdNumberB(b);
119        return opr.getResults();
120    }

121    public static double calculate(double a){
122        Operation opr = OperationFactroy.getOperation("");
123        opr.setdNumberA(a);
124        return opr.getResults();
125    }

126
127}

128

Feedback

# re: 设计模式(2)-------------简单工厂模式(代码实现)  回复  更多评论   

2010-05-20 22:27 by wjmgyn
大话设计 模式看多了。。。。

只有注册用户登录后才能发表评论。


网站导航: