码迷,mamicode.com
首页 > 编程语言 > 详细

设计模式--简单工厂应用 java

时间:2016-05-13 00:08:38      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

要实现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);
    }
}



设计模式--简单工厂应用 java

标签:

原文地址:http://blog.csdn.net/lk10207160511/article/details/51344949

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!