1. 程式人生 > >初學《大話設計模式》——簡單工廠模式

初學《大話設計模式》——簡單工廠模式

一、簡單工廠模式:

實質是由一個工廠類根據傳入的引數,動態決定應該建立哪一個產品類(這些產品類繼承自一個父類或介面)的例項。簡單工廠模式的建立目標,所有建立的物件都是充當這個角色的某個具體類的例項。

二、場景

使用Java面嚮物件語言實現一個計算機控制檯程式,要求輸入兩個數和運算子號,得到結果。
為了使程式達到易擴充套件,易維護,易複用且靈活的目的,本程式將使用到面向物件程式設計思想的三大特性:封裝、繼承和多型,以及簡單工廠模式

三、程式程式碼

程式共有七個類:
這裡寫圖片描述

MyOperation.java


public class MyOperation {

    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 double getResult() throws Exception { double result=0; return result; } }

OperaAdd.java


public class OperaAdd extends MyOperation {

    @Override
    public double getResult() {
        double result=0;
        result=this.getNumberA()+this
.getNumberB(); return result; } }

OperaSub.java


public class OperaSub extends MyOperation {

    @Override
    public double getResult() {

        return this.getNumberA()-this.getNumberB();
    }
}

OperaMul.java


public class OperaMul extends MyOperation {

    @Override
    public double getResult() {

        return this.getNumberA()*this.getNumberB();
    }
}

OperaDiv.java


public class OperaDiv extends MyOperation{

    @Override
    public double getResult() throws Exception {
        if(this.getNumberB()==0){
            throw new Exception("除數不能為0");
        }
        return this.getNumberA()/this.getNumberB();
    }
}

OperationFactory.java


public class OperationFactory {

    public static MyOperation createOperation(String operate){
        MyOperation operation=null;
        switch (operate) {
        case "+":
            operation=new OperaAdd();
            break;

        case "-":
            operation=new OperaSub();
            break;

        case "*":
            operation=new OperaMul();
            break;

        case "/":
            operation=new OperaDiv();
            break;
        default:
            operation=new MyOperation();
            break;
        }
        return operation;
    }
}

Calculator.java


import java.util.Scanner;

public class Calculator {

    /**
     * @Title: main
     * @Description: 計算器
     * @param args   
     * @return void    返回型別
     * @throws
     */
    public static void main(String[] args) {
        MyOperation operation=null;
        try {
            System.out.println("請輸入數字A:");
            double numberA=Double.valueOf(new Scanner(System.in).nextLine());
            System.out.println("請輸入運算子:");
            String operaString=new Scanner(System.in).nextLine();
            System.out.println("請輸入數字B:");
            double numberB=Double.valueOf(new Scanner(System.in).nextLine());
            operation=OperationFactory.createOperation(operaString);
            operation.setNumberA(numberA);
            operation.setNumberB(numberB);
            System.out.println("結果是:"+operation.getResult());

        } catch (NumberFormatException e) {
            System.out.println("只能輸入數字");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }


    }

}