工厂方法模式

工厂方法模式的核心点
(1).抽象产品:定义产品的规范描述产品的主要特征和方法
(2).抽象工厂:提供创建产品的接口
(3).具体产品:实现了抽象产品的所定义的接口,由具体工厂创建
(4).具体工厂:实现抽象工厂中的抽象创建方法,完成某个具体产品的创建,具体工厂和具体产品之间存在对应关系
抽象产品以及具体产品:

package factoryPatterMethod;

/**
 * @version 1.0
 * @author: 小原
 * @date: 2021-06-02 08:33
 */
public class Operation {
    private double num1;
    private double num2;
    private static Operation operation=null;
    public double getNum1() {
        return num1;
    }

    public void setNum1(double num1) {
        this.num1 = num1;
    }

    public double getNum2() {
        return num2;
    }

    public void setNum2(double num2) {
        this.num2 = num2;
    }
    private Operation(){}
    public static Operation getInstance(){
        if(operation==null){
            operation=new Operation();
        }
        return operation;
    }
}

package factoryPatterMethod;

/**
 * @version 1.0
 * @author: 小原
 * @date: 2021-06-02 08:29
 */
public class OperationAdd implements OperationInterfaceFactory {

    private Operation operation=Operation.getInstance();
    @Override
    public double getResult() throws Exception {
        return operation.getNum1()+operation.getNum2();
    }
}

package factoryPatterMethod;

/**
 * @version 1.0
 * @author: 小原
 * @date: 2021-06-02 09:00
 */
public class OperationDiv implements OperationInterfaceFactory {
    private Operation operation = Operation.getInstance();

    @Override
    public double getResult() throws Exception {
        if (operation.getNum2() == 0) {
            System.out.println("被除数不能为0");
        } else {
            return operation.getNum1() + operation.getNum2();
        }
        return 0;
    }
}

package factoryPatterMethod;

/**
 * @version 1.0
 * @author: 小原
 * @date: 2021-06-02 08:57
 */
public class OperationMul implements OperationInterfaceFactory {
    private Operation operation=Operation.getInstance();
    @Override
    public double getResult() throws Exception {
        return operation.getNum1()*operation.getNum2();
    }
}

package factoryPatterMethod;

/**
 * @version 1.0
 * @author: 小原
 * @date: 2021-06-02 08:55
 */
public class OperationSub  implements OperationInterfaceFactory {
    private Operation operation=Operation.getInstance();
    @Override
    public double getResult() throws Exception {
        return operation.getNum1()-operation.getNum2();
    }
}

抽象工厂以及具体工厂:

package factoryPatterMethod;

/**
 * @version 1.0
 * @author: 小原
 * @date: 2021-06-02 09:03
 */
public interface   OperationInterface {
    /**
     * 得到工厂对象
     * @return 具体工厂对象
     */
    public OperationInterfaceFactory newInstance();
}

package factoryPatterMethod;

/**
 * @version 1.0
 * @author: 小原
 * @date: 2021-06-02 09:05
 */
public class OperationInterfaceAdd implements  OperationInterface {
    @Override
    public OperationInterfaceFactory newInstance() {
        return new OperationAdd();
    }
}

package factoryPatterMethod;

/**
 * @version 1.0
 * @author: 小原
 * @date: 2021-06-02 09:08
 */
public class OperationInterfaceDiv implements OperationInterface {
    @Override
    public OperationInterfaceFactory newInstance() {
        return new OperationDiv();
    }
}

package factoryPatterMethod;

/**
 * @version 1.0
 * @author: 小原
 * @date: 2021-06-02 09:07
 */
public class OperationInterfaceMul implements  OperationInterface{
    @Override
    public OperationInterfaceFactory newInstance() {
        return new OperationMul();
    }
}

package factoryPatterMethod;

/**
 * @version 1.0
 * @author: 小原
 * @date: 2021-06-02 09:06
 */
public class OperationInterfaceSub implements OperationInterface{
    @Override
    public OperationInterfaceFactory newInstance() {
        return new OperationSub();
    }
}

服务层:

package factoryPatterMethod;

/**
 * @version 1.0
 * @author: 小原
 * @date: 2021-06-02 09:09
 */
public class OperationTest {
    /**
     * 程序入口
     *
     * @param args 入口参数
     */
    public static void main(String[] args) throws Exception {
        Operation operation=Operation.getInstance();
        OperationInterfaceFactory oif = new OperationInterfaceDiv().newInstance();
        operation.setNum1(3);
        operation.setNum2(6);
        System.out.println("除法计算的值:"+oif.getResult());
        OperationInterfaceFactory oif1 = new OperationInterfaceAdd().newInstance();
        operation.setNum1(6);
        operation.setNum2(6);
        System.out.println("加法计算的值:"+oif1.getResult());
        OperationInterfaceFactory oif2 =new OperationInterfaceSub().newInstance();
        operation.setNum1(2);
        operation.setNum2(6);
        System.out.println("减法计算的值:"+oif2.getResult());
        OperationInterfaceFactory oif3 = new OperationInterfaceMul().newInstance();
        operation.setNum1(7);
        operation.setNum2(6);
        System.out.println("乘法计算的值:"+oif1.getResult());
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值