設計模式(一)—— 簡單工廠模式

宅貓燒香發表於2020-10-13

一、工廠模式概述

1. 定義

簡單工廠模式是屬於建立型模式,又叫做靜態工廠方法(Static Factory Method)模式,但不屬於23種GOF設計模式之一。簡單工廠模式是由一個工廠物件決定建立出哪一種產品類的例項。簡單工廠模式是工廠模式家族中最簡單實用的模式,可以理解為是不同工廠模式的一個特殊實現。

2. 簡單工廠模式結構圖

在這裡插入圖片描述

二、工廠模式實現

我們以計算器為例實現簡單工廠模式:

1. 定義計算操作介面

程式碼如下:

public interface Operation {

    double GetResult(double numberA, double numberB, String operate);
}

2. 定義計算操作工廠

程式碼如下:

public class OperationFactory {

    //測試程式碼,一般使用配置檔案代替
    public static final Map<String,String> typeMap = new HashMap<>();
    static{
        typeMap.put("+","com.simplefactory.OperationAdd");
        typeMap.put("-","com.simplefactory.OperationSub");
        typeMap.put("*","com.simplefactory.OperationMul");
        typeMap.put("/","com.simplefactory.OperationDiv");
    }

    /**
     * 獲取Operation物件
     * @param type
     * @return
     */
    public static Operation getOperation(String type) {

        Operation operation = null;

        try {
            operation = (Operation) Class.forName(typeMap.get(type)).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return operation;
    }
}

3. 定義操作(加、減、乘、除)類

程式碼如下(加法):

public class OperationAdd implements Operation {

    @Override
    public double GetResult(double numberA, double numberB, String operate) {

        double resule = 0;
        resule = numberA + numberB;
        return resule;
    }
}

程式碼如下(減法):

public class OperationSub implements Operation{

    @Override
    public double GetResult(double numberA, double numberB, String operate) {

        double resule = 0;
        resule = numberA - numberB;
        return resule;
    }
}

程式碼如下(乘法):

public class OperationMul  implements Operation{

    @Override
    public double GetResult(double numberA, double numberB, String operate) {

        double resule = 0;
        resule = numberA * numberB;
        return resule;
    }
}

程式碼如下(除法):

public class OperationDiv implements Operation {

    @Override
    public double GetResult(double numberA, double numberB, String operate) {

        double resule = 0;
        if (numberB == 0)
            throw new RuntimeException("除數不能為0!");
        resule = numberA/numberB;
        return resule;
    }
}

4. 測試類及測試結果

程式碼如下:

public class Test {

    public static void main(String[] args) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
    
        Operation add = OperationFactory.getOperation("+");
        double addResult = add.GetResult(5,2,"1");
        System.out.println(addResult);

        Operation sub = OperationFactory.getOperation("-");
        double subResult = sub.GetResult(5,2,"1");
        System.out.println(subResult);

        Operation mul = OperationFactory.getOperation("*");
        double mulResult = mul.GetResult(5,2,"1");
        System.out.println(mulResult);

        Operation div = OperationFactory.getOperation("/");
        double divResult = div.GetResult(5,2,"1");
        System.out.println(divResult);
    }
}

測試結果:

7.0
3.0
10.0
2.5

相關文章