java设计模式之 简单工厂模式

工厂模式从字面可以理解为批量生产的意思,也就是说一个类的创建可以进行批量的生产,那么对于批量生产类其实有一个公共的特点,那就是肯定有一个模型,不然不可能批量,从这种思想我们可以想到在java面向对象中,该思想不谋为何,就是事物的抽象,通过抽象的事物可以创建我们现实生活中的具体事物。

因此对于工厂模型我们可以这么归类:

1: 要想实现工厂模式,必须要有一个模型,这个模型就是事物的抽象,拥有对象的基本特征。

2:要想生成多个对象的特征,必须需要多个类去继承该模型的基本特征。

【JAVA中哪些地方用到了工厂模式】

 1:在spring中。容器创建bean的实例

 2:在运用动态代理中也存在

【具体使用】

【场景】自定义计算器,接收键盘数字可以进行加减乘除等基本的数字运算

首先建立模型,也就是事物的抽象类,在改类中包含基本的事物特征

package com.example.springtest.other.pojo;

/**
 * ***GOOD LUCK****
 *
 * @Author : Wukn
 * @Date : 2018/6/
 */
public class Operation {

    //返回结果
    double result = 0;
    //除数
    private int param1 ;
    //被除数
    private int param2 ;

    public Operation() {
    }

    public Operation(int param1, int param2) {
        this.param1 = param1;
        this.param2 = param2;
    }

    public int getParam1() {
        return param1;
    }

    public void setParam1(int param1) {
        this.param1 = param1;
    }

    public int getParam2() {
        return param2;
    }

    public void setParam2(int param2) {
        this.param2 = param2;
    }

    /**
     * 结果
     * @return
     */
    public double getResult() {
        return result;
    }
}

建立加减乘除四个四类,这四个子类都继承Operation基本类,并重写计算结果的方法,即getResult()方法

1:


/**
 * ***GOOD LUCK****
 *
 * @Author : Wukn
 * @Date : 2018/6/
 */
public class AddOp extends Operation {

    private double result = 0;

    /**
     * 加法类,重写getResult()方法,重写的内容可以是业务逻辑,比如判断参数是否合法等
     * @return
     */
    @Override
    public double getResult() {
        result = getParam1()+getParam2();
        return result;
    }

}

:2:

package com.example.springtest.other.pojo;

/**
 * ***GOOD LUCK****
 *
 * @Author : Wukn
 * @Date : 2018/6/
 */
public class ReduceOp extends Operation{
    private double result;
    
    /**
     * 减法
     * 结果
     * @return
     */
    @Override
    public double getResult() {
        result = getParam1() - getParam2();
        return result;
    }

}

3

package com.example.springtest.other.pojo;

/**
 * ***GOOD LUCK****
 *
 * @Author : Wukn
 * @Date : 2018/6/
 */
public class RideOp extends Operation{

    private double result;

    /**
     * 乘法
     * 结果
     * @return
     */
    @Override
    public double getResult() {
        result = getParam1() * getParam2();
        return result;
    }
}

4:

package com.example.springtest.other.pojo;

/**
 * ***GOOD LUCK****
 *
 * @Author : Wukn
 * @Date : 2018/6/
 */
public class DeleteOp extends Operation{

    private double result;

    /**除法
     * 结果
     * @return
     */
    @Override
    public double getResult() {
        result = getParam1() / getParam2();
        return result;
    }
}

子类写好之后,可以思考,java的三大特性  封装  继承  多态 在这样的业务中刚好可以使用多太,因为一个父类下面有四个子类,在我们业务中,不管是要进行加还是减,最终都是使用Operation的一个子类,建立一个方法,可以产生不同的子类,这个类其实就是工厂类

package com.example.springtest.other.service;

import com.example.springtest.other.pojo.*;

/**
 * ***GOOD LUCK****
 *
 * @Author : Wukn
 * @Date : 2018/6/
 */
public class OperationFactory {


    /**
     * 可以通过判断符号来生成不同的子类,这里用到的就是多态
     * @param str
     * @return
     */
    public static Operation getOperation(String str) {
        Operation operation = null;
        switch (str) {
            case "+":
                operation = new AddOp();
                break;
            case "-":
                operation = new ReduceOp();
                break;
            case "*":
                operation = new RideOp();
                break;
            case "/":
                operation = new DeleteOp();
                break;

        }
        return operation;
    }
}

测试结果:

package com.example.springtest.other.test;

import com.example.springtest.other.pojo.Operation;
import com.example.springtest.other.service.OperationFactory;
import org.junit.jupiter.api.Test;

/**
 * ***GOOD LUCK****
 *
 * @Author : Wukn
 * @Date : 2018/6/
 */
public class SDemo {


    @Test
    public void test1() {
        Operation operation = null;
        operation = OperationFactory.getOperation( "+" );
        operation.setParam1( 1 );
        operation.setParam2( 2 );
        double result = operation.getResult();
        System.out.println(result);
    }
}

以上就是简单工厂的设计思想,值得我们去思考。

结构图

      

猜你喜欢

转载自blog.csdn.net/weixin_41404773/article/details/82152318