大话设计模式--简单工厂模式

Posted 程序员小熊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了大话设计模式--简单工厂模式相关的知识,希望对你有一定的参考价值。

问题:请用C++,JAVA,C#任意一种以面向对象语言实现一个计算器控制台程序:要求输入2个数和运算符号,得到结果


第一次编码:

public class Program   

    public static void main(String[] args) throws IOException   
        System.out.println("请输入数字A:");   
        String a = new BufferedReader(new InputStreamReader(   
        System.in)).readLine();  
        System.out.println("请输入运算符:");  
        String b=new BufferedReader(new InputStreamReader(System.in)).readLine().trim();  
        System.out.println("请输入数字c:");  
        String c=new BufferedReader(new InputStreamReader(System.in)).readLine();  
        double d = 0;  
        if("+".equalsIgnoreCase(b))  
            d=Double.valueOf(a)+Double.valueOf(c);  
          
        if("-".equalsIgnoreCase(b))  
            d=Double.valueOf(a)-Double.valueOf(c);            
          
        if("*".equalsIgnoreCase(b))  
            d=Double.valueOf(a)*Double.valueOf(c);         
          
        if("/".equalsIgnoreCase(b))  
            d=Double.valueOf(a)/Double.valueOf(c);    
          
        System.out.println("结果为:"+d);  
      
  
  

存在的问题:

1、以a, b, c这种命名方式不能很好的表示参数的意义

2、以if语句这样的方式判断,表示每次运算时都要进行三次无用的判断

3、没有考虑除法中除数为0或者未填写的情况


第二次编码:

public class ProgramTwo   
  
    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();  
            String strResult;  
            if("+".equalsIgnoreCase(stroperate))  
                strResult=String.valueOf(Double.valueOf(strA)+Double.valueOf(strB));  
            else if("-".equalsIgnoreCase(stroperate))  
                strResult=String.valueOf(Double.valueOf(strA)-Double.valueOf(strB));  
            else if("*".equalsIgnoreCase(stroperate))  
                strResult=String.valueOf(Double.valueOf(strA)*Double.valueOf(strB));  
            else  
               if("0".equalsIgnoreCase(strB))  
                   strResult="除数不能为0";   
               else  
                   strResult=String.valueOf(Double.valueOf(strA)/Double.valueOf(strB));    
                 
              
            System.out.println("结果为:"+strResult);  
        catch(Exception e)  
            System.out.println("你的输入有错误!");  
          
  
      
  
  


存在的问题:

以面向对象的思想考虑(以活字印刷启迪):

1、更改只需要修改需要改的地方即可,不需要全部重新做,即为可维护

2、替换下来的字还可以在其余的地方重复使用,即为可服用

3、若要添加其余的字只需要添加额外的字即可,即为可扩展

4、字的排列其实可能是竖排可能横排此时只需将活字移动就可做到满足排列需求,即为灵活性好


第三次编码:

public abstract class Operation   
  
    private double numberA;  
    private double numberB;  
      
    public double getNumberA()   
        return numberA;  
      
  
    public void setNumberA(double numberA)   
        this.numberA = numberA;  
      
  
    public double getNumberB()   
        return numberB;  
      
  
    public void setNumberB(double numberB)   
        this.numberB = numberB;  
      
  
    public abstract double getResult() throws Exception;  
 

public class OperationAdd extends Operation   
  
    @Override  
    public double getResult()   
        double numberd;  
        numberd=getNumberA()+getNumberB();  
        return numberd;  
      
  

public class OperationDiv extends Operation   
  
    @Override  
    public double getResult() throws Exception   
        double numberd;  
        if(getNumberB()==0)  
            throw new Exception("除数不为0");  
          
        numberd=getNumberA()/getNumberB();  
        return numberd;  
      
  
 

public class OperationMul extends Operation   
  
    @Override  
    public double getResult()   
        double numberd;  
        numberd=getNumberA()*getNumberB();  
        return numberd;  
      
  
 

public class OperationSub extends Operation   
  
    @Override  
    public double getResult()   
        double numberd;  
        numberd=getNumberA()-getNumberB();  
        return numberd;  
      
  

public class ProgramThree   
  
    public static double getResult(double numberA,double numberB,String operate)  
        double result=0;  
        if("+".equalsIgnoreCase(operate))  
            result=numberA+numberB;  
        else if("-".equalsIgnoreCase(operate))  
            result=numberA-numberB;  
        else if("*".equalsIgnoreCase(operate))  
            result=numberA*numberB;  
        else  
            result=numberA/numberB;  
          
        return result;  
      
    /** 
     * @param args 
     */  
    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();  
            double strResult;  
            strResult=getResult(Double.valueOf(strA), Double.valueOf(strB), stroperate);  
            System.out.println("结果为:"+strResult);  
        catch(Exception e)  
            System.out.println("你的输入有错误!");  
          
  
      
  
  


存在的问题:

面向对象的三大特性(封装、继承、多态)这里只用到了封装,添加新的功能不方便


第四次编码:

public class OperationFactory   
  
    public static Operation createOperate(String operate)  
        Operation operation=null;  
        if("+".equalsIgnoreCase(operate))  
            operation=new OperationAdd();  
        else if("-".equalsIgnoreCase(operate))  
            operation=new OperationSub();  
        else if("*".equalsIgnoreCase(operate))  
            operation=new OperationMul();  
        else  
            operation=new OperationDiv();  
          
        return operation;  
      


public class ProgramFour   

    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;  
            oper=OperationFactory.createOperate(stroperate);  
            oper.setNumberA(Double.valueOf(strA));  
            oper.setNumberB(Double.valueOf(strB));  
            double result=oper.getResult();  
            System.out.println(result);  
         catch (Exception e)   
  
          
  
      
  
  


到底要实例化谁,将来会不会增加实例化的对象,应该考虑用一个单独的类来做这个创建实例的过程,这就是工厂 

以上是关于大话设计模式--简单工厂模式的主要内容,如果未能解决你的问题,请参考以下文章

大话设计模式——简单工厂模式

大话设计模式之简单工厂模式

大话设计模式之简单的工厂模式

大话设计模式之简单工厂模式

大话涉及模式Python实现-简单工厂模式

大话设计模式------简单工厂模式