多重if替换方式

public class Test {


    public static void main(String[] args) {
        List<Order> list = Lists.newArrayList();
        Order order = null;
        Optional.ofNullable(order).ifPresent( list::add );

        System.out.println(JSON.toJSONString(list));

        Calculator calculator = new Calculator();
        /****** 方式一 ******/
        int add = calculator.calculateUsingFactory(6, 2, "add");
        System.out.println("add result = "+ add);
        int divide = calculator.calculateUsingFactory(6, 2, "divide");
        System.out.println("divide result = "+ divide);
        System.out.println("--------------------------------------------");
        /****** 方式二 ******/
        int add2 = calculator.calculate(6, 2, Operator.ADD);
        System.out.println("add result = "+ add2);
        int divide2 = calculator.calculate(6, 2, Operator.DIVIDE);
        System.out.println("divide result = "+ divide2);
    }

    public static class Calculator {
        /****** 方式一 ******/
        public int calculateUsingFactory(int a, int b, String operator) {
            Operation targetOperation = OperatorFactory
                    .getOperation(operator)
                    .orElseThrow(() -> new IllegalArgumentException("Invalid Operator"));
            return targetOperation.apply(a, b);
        }

        /****** 方式二 ******/
        public int calculate(int a, int b, Operator operator) {
            return operator.apply(a, b);
        }
    }

    /***************************** 方式一 *****************************/
    /**
     * 定义一个操作接口
     **/
    public interface Operation {
        int apply(int a, int b);
    }

    /**
     * 加法 实现操作,
     **/
    static class Addition implements Operation {
        @Override
        public int apply(int a, int b) {
            return a + b;
        }
    }

    /**
     * 除法 实现操作,
     **/
    static class Division implements Operation {
        @Override
        public int apply(int a, int b) {
            return a / b;
        }
    }

    /**
     * 实现操作工厂
     **/
    static class OperatorFactory {
        static Map<String, Operation> operationMap = new HashMap<>();
        static {
            operationMap.put("add", new Addition());
            operationMap.put("divide", new Division());
            // 更多实现操作...
        }

        public static Optional<Operation> getOperation(String operator) {
            return Optional.ofNullable(operationMap.get(operator));
        }
    }

    /***************************** 方式一 *****************************/

    /***************************** 方式二 *****************************/
    public enum Operator {
        ADD {
            @Override
            public int apply(int a, int b) {
                return a + b;
            }
        },
        DIVIDE {
            @Override
            public int apply(int a, int b) {
                return a / b;
            }
        };
        // 更多实现操作...

        public abstract int apply(int a, int b);
    }


    /***************************** 方式二 *****************************/

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嘿丶小伙计

请赏我点铜板买喵粮自己吃,谢谢

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值