設計模式--簡單工廠應用 java
阿新 • • 發佈:2019-01-22
要實現java面向物件,那麼我們來寫個簡單的工廠來實現java的封裝、繼承、複用的例子,還有,我們順便實現函式式的簡單工廠。
一、簡單工廠(非函式式)
1、首先我們將通用的屬性合方法放在一個普通類裡邊
/** * Created by LK on 2016/5/8. */ public class Operation { private double numberA = 0; private double numberB = 0; 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(){ double result = 0; return result; } }
2、假如說,我們現在要實現演算法的加減乘除,那麼我們分別建立對應的類繼承上面的普通類
加法:
/**
* Created by LK on 2016/5/8.
*/
public class OperationsAdd extends Operation {
public double getResult(){
double result = 0;
result = getNumberA() + getNumberB();
return result;
}
}
減法:
/** * Created by LK on 2016/5/8. */ public class OperationSub extends Operation{ public double getResult(){ double result = 0; result = getNumberA() - getNumberB(); return result; } }
乘法:
/**
* Created by LK on 2016/5/8.
*/
public class OperationMul extends Operation {
public double getResult(){
double result = 0;
result = getNumberA() * getNumberB();
return result;
}
}
除法:
/** * Created by LK on 2016/5/8. */ public class OperationDiv extends Operation { public double getResult(){ double result = 0; if(getNumberB() == 0){ System.out.println("除數不能為0!"); } result = getNumberA() / getNumberB(); return result; } }
3、現在各個演算法都已經有了實現了,因為它們很零散,那麼我們就需要將這幾個演算法集中管理起來,這就用到了工廠,用工廠去圈住它們
/**
* Created by LK on 2016/5/8.
*/
public class OperationFactory {
public static Operation createOperate(String operate){
Operation oper = null;
switch (operate){
case "+":
oper = new OperationsAdd();
break;
case "-":
oper = new OperationSub();
break;
case "*":
oper = new OperationMul();
break;
case "/":
oper = new OperationDiv();
break;
}
return oper;
}
}
4、現在是萬事俱備,只欠廠長的召喚了
/**
* Created by LK on 2016/5/8.
*/
public class FactoryClient {
public static void main(String[] args) {
/**
* 簡單工廠模式,方法一
*/
Operation operation = OperationFactory.createOperate("/");
operation.setNumberA(2);
operation.setNumberB(1);
double result = operation.getResult();
System.out.println(result);
/**
* 簡單工廠模式,方法二
*/
/*String oper = "/";
IOperationFunction operationFunction = OperationFunction.getOperationFunctionStrategy(oper);
double result1 = operationFunction.createOperation(2,1,oper);
System.out.println(result1);*/
}
}
二、簡單工廠(函式式)
為了簡化實現類的產生,我們可以考慮用函式式的方法去實現
1、建立一個函式式的介面
/**
* Created by LK on 2016/5/8.
*/
@FunctionalInterface public interface IOperationFunction {
public double createOperation(double numberA,double numberB,String oper);
}
2、建立一個函式式介面的實現,我們把這加減乘除的演算法都寫在這個實現類裡面,能類載入的時候就初始化,這樣子,以後需要用到直接呼叫即可。
/**
* Created by LK on 2016/5/8.
*/
public class OperationFunction {
private static Map<String,IOperationFunction> operationFunctionMap = new ConcurrentHashMap<>();
static{
/**
* 加法運算
*/
operationFunctionMap.put("+",(numberA,numberB,oper) ->{
double result = numberA + numberB;
return result;
});
/**
* 減法運算
*/
operationFunctionMap.put("-",(numberA,numberB,oper) ->{
double result = numberA - numberB;
return result;
});
/**
* 乘法運算
*/
operationFunctionMap.put("*",(numberA,numberB,oper) -> {
double result = numberA * numberB;
return result;
});
/**
* 除法運算
*/
operationFunctionMap.put("/",(numberA,numberB,oper) -> {
double result = numberA / numberB;
return result;
});
}
public static IOperationFunction getOperationFunctionStrategy(String oper){
return operationFunctionMap.get(oper);
}
}
現在,跟非函式式的差別就在於,我們不需要建立很多的實現,少建立了四個類
3、現在,也只是欠廠長的召喚了
/**
* Created by LK on 2016/5/8.
*/
public class FactoryClient {
public static void main(String[] args) {
/**
* 簡單工廠模式,方法一
*/
/* Operation operation = OperationFactory.createOperate("/");
operation.setNumberA(2);
operation.setNumberB(1);
double result = operation.getResult();
System.out.println(result);*/
/**
* 簡單工廠模式,方法二
*/
String oper = "/";
IOperationFunction operationFunction = OperationFunction.getOperationFunctionStrategy(oper);
double result1 = operationFunction.createOperation(2,1,oper);
System.out.println(result1);
}
}