码迷,mamicode.com
首页 > 其他好文 > 详细

简单工厂模式

时间:2017-03-24 00:28:40      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:ges   his   over   简单   ber   并且   png   print   package   

技术分享


站在用户的角度:需要知道运算父类(在相对应的简单工厂模式  对应的是那些子类抽象出来的父类),和简单工厂类,并需要传递操作符获取具体的子类。

站在实现者的角度:需要抽象出父类,并造一个简单工厂

 

package com.simplefactory1;

/**
 * 抽象的运算类 需要提供公有的数字
 * @author 黄二狗
 *
 */
public abstract class Calculator {
   //第一个数字
    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();
   
}
package com.simplefactory1;

/**
 * 具体的加法类 减法  等类似
 * @author 黄二狗
 *
 */
public class CalculatorAdd extends Calculator{

    @Override
    public double getResult() {
       double numberA=getNumberA();
       double numberB=getNumberB();
       return numberA+numberB;
    }

}
package com.simplefactory1;

public class CalculatorMinus extends Calculator{
    @Override
    public double getResult() {
       double numberA=getNumberA();
       double numberB=getNumberB();
       return numberA-numberB;
    }
}
package com.simplefactory1;

public class CalculatorMultiply extends Calculator{
    @Override
    public double getResult() {
       double numberA=getNumberA();
       double numberB=getNumberB();
       return numberA*numberB;
    }
}
package com.simplefactory1;

public class CalculatorDevide extends Calculator{

    @Override
    public double getResult() {
       double numberA=getNumberA();
       double numberB=getNumberB();
       return numberA/numberB;
    }
   
}
package com.simplefactory1;

public class CalculatorFactory {
   public static Calculator createCalculator(String type)
   {
        Calculator cal=null;
       switch(type)
       {
       case "+":
        cal=new CalculatorAdd();
        break;
        
       case "-":
        cal=new CalculatorMinus();
        break;
           
       case "*":
           cal=new CalculatorMultiply();
           break;
       case "/":
           cal=new CalculatorDevide();
           break;
       }
       return cal;
   }
   
}
package com.simplefactory1;

/**
 * 用户需要知道工厂 和运算父类 并且需要传递运算操作符
 * @author 黄二狗
 *
 */
public class Client {
    public static void main(String[] args) {
        System.out.println("测试乘法");
        Calculator multiplyCalculator=CalculatorFactory.createCalculator("*");
        multiplyCalculator.setNumberA(30);
        multiplyCalculator.setNumberB(50);
        System.out.println(multiplyCalculator.getResult());
        
        System.out.println("============================");
        
        System.out.println("测试除法");
        Calculator devideCalculator=CalculatorFactory.createCalculator("/");
        devideCalculator.setNumberA(30);
        devideCalculator.setNumberB(50);
        System.out.println(devideCalculator.getResult());
    }

}

技术分享

 

简单工厂模式

标签:ges   his   over   简单   ber   并且   png   print   package   

原文地址:http://www.cnblogs.com/HJL085/p/6607671.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!