标签:
要实现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; } }
/** * 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; } }
/** * 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); }
/** * 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); } }
标签:
原文地址:http://blog.csdn.net/lk10207160511/article/details/51344949