1. 程式人生 > >設計模式學習筆記(5) --計算器的設計模式(簡單工廠,工廠方法)

設計模式學習筆記(5) --計算器的設計模式(簡單工廠,工廠方法)

1.面向過程的計算器

package com.skiff.www.factory.calculator;

import java.util.Scanner;

/**
 * @Auther: 一葉扁舟
 * @Date: 2018/11/1 20:23
 * @Description:  簡單的計算器
 *
 */
public class Calculator {

public static void main(String[] args) {
        Double firstNum;
        Double secondNum;
        String operateType;
        System.out.print("請輸入第一個數:");
        Scanner scanner = new Scanner(System.in);
        firstNum =  Double.parseDouble(scanner.nextLine());
        System.out.print("請輸入操作符:");
        operateType =  scanner.nextLine();
        System.out.print("請輸入第二個數:");
        secondNum =  Double.parseDouble(scanner.nextLine());
        Double result  = 0.0;
        if("+".equals(operateType)){
            result = firstNum + secondNum;
        }else if("-".equals(operateType)){
            result = firstNum - secondNum;
        }else if("*".equals(operateType)){
            result = firstNum * secondNum;
        }else if("/".equals(operateType)){
            result = firstNum / secondNum;
        }
        System.out.println("結果:");

        System.out.printf("%.2f %s %.2f = %.2f",firstNum,operateType,secondNum,result);
    }

}

2.採用簡單工廠的計算器

 

2.1UML草圖:

  

 

2.2程式碼:

package com.skiff.www.factory.calculator.simple.factory;

/**
 * @Auther: 一葉扁舟
 * @Date: 2018/11/1 20:46
 * @Description:
 */
public abstract class Operation {
    private Double firstNum;
    private Double secondNum;

    public abstract Double calculator();

    public Double getFirstNum() {
        return firstNum;
    }

    public Double getSecondNum() {
        return secondNum;
    }

    public void setFirstNum(Double firstNum) {
        this.firstNum = firstNum;
    }

    public void setSecondNum(Double secondNum) {
        this.secondNum = secondNum;
    }
}

 

package com.skiff.www.factory.calculator.simple.factory;

/**
 * @Auther: 一葉扁舟
 * @Date: 2018/11/1 20:49
 * @Description:
 */
public class AddOperation extends Operation {
    @Override
    public Double calculator() {
        return getFirstNum() + getSecondNum();
    }
}

 

package com.skiff.www.factory.calculator.simple.factory;

/**
 * @Auther: 一葉扁舟
 * @Date: 2018/11/1 20:52
 * @Description:
 */
public class SubOperation extends Operation {
    @Override
    public Double calculator() {
        return getFirstNum() - getSecondNum();
    }
}
package com.skiff.www.factory.calculator.simple.factory;

/**
 * @Auther: 一葉扁舟
 * @Date: 2018/11/1 20:50
 * @Description:
 */
public class SimpleOperationFactory {

    public static Operation getOperation(String operationType){
        if("+".equals(operationType)){
            return new AddOperation();
        }else if("-".equals(operationType)){
            return new SubOperation();
        }//乘法 除法 ……
        //缺點,每次新增一種運算,客戶端雖然不用動,但必須這裡判斷,不滿足開閉原則
        return null;
    }


}

 

package com.skiff.www.factory.calculator.simple.factory;

import com.skiff.www.factory.simplefactory.fruit.SimpleFactory;

import java.util.Scanner;

/**
 * @Auther: 一葉扁舟
 * @Date: 2018/11/1 20:54
 * @Description:
 */
public class ClientTest {
    public static void main(String[] args) {
        Double firstNum;
        Double secondNum;
        String operateType;
        System.out.print("請輸入第一個數:");
        Scanner scanner = new Scanner(System.in);
        firstNum =  Double.parseDouble(scanner.nextLine());
        System.out.print("請輸入操作符:");
        operateType =  scanner.nextLine();
        System.out.print("請輸入第二個數:");
        secondNum =  Double.parseDouble(scanner.nextLine());
        Double result  = 0.0;

        Operation operation = SimpleOperationFactory.getOperation(operateType);
        operation.setFirstNum(firstNum);
        operation.setSecondNum(secondNum);
         result = operation.calculator();
        System.out.println("結果:"+result);
    }
}

 

 

 

3.採用工廠方法的計算器:

 

3.1UML草圖:

3.2程式碼:

Operation和AddOperation,SubOperation同程式碼2.2

 

package com.skiff.www.factory.calculator.factory.method;

/**
 * @Auther: 一葉扁舟
 * @Date: 2018/11/1 21:08
 * @Description:
 */
public interface AbstractFactory {

    public abstract  Operation getOperation();
}

 

package com.skiff.www.factory.calculator.factory.method;

/**
 * @Auther: 一葉扁舟
 * @Date: 2018/11/1 21:09
 * @Description:
 */
public class AddFactory implements AbstractFactory {
    @Override
    public Operation getOperation() {
        return new AddOperation();
    }
}

 

package com.skiff.www.factory.calculator.factory.method;

/**
 * @Auther: 一葉扁舟
 * @Date: 2018/11/1 21:12
 * @Description:
 */
public class SubFactory implements AbstractFactory {
    @Override
    public Operation getOperation() {
        return new SubOperation();
    }
}

 

package com.skiff.www.factory.calculator.factory.method;

import java.util.Scanner;

/**
 * @Auther: 一葉扁舟
 * @Date: 2018/11/1 20:54
 * @Description:
 */
public class ClientTest {
    public static void main(String[] args) {
        Double firstNum;
        Double secondNum;
        String operateType;
        System.out.print("請輸入第一個數:");
        Scanner scanner = new Scanner(System.in);
        firstNum =  Double.parseDouble(scanner.nextLine());
        System.out.print("請輸入操作符:");
        operateType =  scanner.nextLine();
        System.out.print("請輸入第二個數:");
        secondNum =  Double.parseDouble(scanner.nextLine());
        Double result  = 0.0;
        AbstractFactory factory = null;
        //加法工廠
        if("+".equals(operateType)){
            factory = new AddFactory();
        }else if("-".equals(operateType)){
            factory = new SubFactory();
        }//新增乘法,除法的工廠

        Operation operation = factory.getOperation();
        operation.setFirstNum(firstNum);
        operation.setSecondNum(secondNum);
         result = operation.calculator();
        System.out.println("結果:"+result);
    }
}