Java设计模式--简单工厂模式

本文介绍了一个使用简单工厂模式实现的计算器示例。该计算器能够进行加法和减法运算,并通过简单工厂模式来创建相应的运算对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(1)抽象运算类

 

package com.liutao.design.model.simpleFactoryModel;

/**
 * abstract operation class
 * this demo is a caculator to show how to use simple factory mode
 *
 * @author LIUTAO
 * @version 2017/10/25
 * @see
 */
public abstract class Operation {
    private double numberA;
    private double numberB;

    public Operation() {
    }

    public Operation(double numberA, double numberB){
        this.numberA = numberA;
        this.numberB = numberB;
    }

    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;
    }

    /**
     * get the result of operation
     * @return
     */
    public abstract double getResult();
}

(2)加法运算类

package com.liutao.design.model.simpleFactoryModel;

/**
 * add operation class
 * this demo is a caculator to show how to use simple factory mode
 *
 * @author LIUTAO
 * @version 2017/10/25
 * @see
 */
public class AddOperation extends Operation {

    @Override
    public double getResult() {
        return getNumberA()+getNumberB();
    }
}

(3)减法运算类

 

 

package com.liutao.design.model.simpleFactoryModel;

/**
 * sub operation class
 * this demo is a caculator to show how to use simple factory mode
 *
 * @author LIUTAO
 * @version 2017/10/25
 * @see
 */
public class SubOperation extends Operation {
    @Override
    public double getResult() {
        return getNumberA() - getNumberB();
    }
}

(4)运算工厂类

 

 

package com.liutao.design.model.simpleFactoryModel;

/**
 * operation factory class
 * this demo is a caculator to show how to use simple factory mode
 *
 * the class is used to product the operation object
 *
 * @author LIUTAO
 * @version 2017/10/25
 * @see
 */
public class OperationFactory {
    public static Operation productOperation(String operationStr){
        Operation operation = null;
        switch (operationStr){
            case "+":operation = new AddOperation();
            break;
            case "-":operation = new SubOperation();
            break;
        }
        return operation;
    }
}

(5)测试类

 

 

package com.liutao.design.model.simpleFactoryModel;

/**
 * the class is used to test factory mode
 *
 * @author LIUTAO
 * @version 2017/10/25
 * @see
 */
public class TestDemo {
    public static void main(String[] args) {
        //if you want get the result of 2 plus 1
        Operation operationPlus = OperationFactory.productOperation("+");
        operationPlus.setNumberA(1);
        operationPlus.setNumberB(2);
        System.out.println("1+2="+operationPlus.getResult());

        //if you want get the result of 2 minus 1

        Operation operationMinus = OperationFactory.productOperation("-");
        operationMinus.setNumberA(2);
        operationMinus.setNumberB(1);
        System.out.println("2-1="+operationMinus.getResult());

    }
}

 

 

 

 

 

 

 

 

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值