策略模式(策略枚举)

基础策略模式:

## 首先定义一个接口:

package com.yecc.suanfa.strategy;

/**
 * Created by yecc on 2020/11/16 19:35
 */
public interface Strategy {
    public int doOperation(int num1, int num2);
}

## 定义三个继承接口的类:

package com.yecc.suanfa.strategy;

/**
 * Created by yecc on 2020/11/16 19:36
 */
public class OperationAdd implements Strategy {
    @Override
    public int doOperation(int num1, int num2) {
        return num1+num2;
    }
}

package com.yecc.suanfa.strategy;

/**
 * Created by yecc on 2020/11/16 19:38
 */
public class OperationMultiply implements Strategy {
    @Override
    public int doOperation(int num1, int num2) {
        return num1*num2;
    }
}

package com.yecc.suanfa.strategy;

/**
 * Created by yecc on 2020/11/16 19:37
 */
public class OperationSubtract implements Strategy {
    @Override
    public int doOperation(int num1, int num2) {
        return num1-num2;
    }
}

## 创建一个context类:
context类主要是用来切换策略,实际上就是拿到三个继承类的实例,这里其实也可以不用这个类,当用反射拿到三个继承类的实例的时候,用实例调用继承接口中的方法,自然就会走到自己继承实现的方法了。

package com.yecc.suanfa.strategy;

/**
 * Created by yecc on 2020/11/16 19:38
 */
public class Context {
    private Strategy strategy;
    public Context(Strategy strategy){
        this.strategy = strategy;
    }

    public int executeStrategy(int num1, int num2){
        return strategy.doOperation(num1, num2);
    }
}

## 测试:

package com.yecc.suanfa.strategy;

/**
 * Created by yecc on 2020/11/16 19:40
 */
public class StrategyPatternDemo {
    public static void main(String[] args) {
        Context context = new Context(new OperationAdd());
        System.out.println("10 + 5 = " + context.executeStrategy(10, 5));

        context = new Context(new OperationSubtract());
        System.out.println("10 - 5 = " + context.executeStrategy(10, 5));

        context = new Context(new OperationMultiply());
        System.out.println("10 * 5 = " + context.executeStrategy(10, 5));
    }
}

策略枚举(非常nice)

package com.yecc.suanfa.strategy.enums;

/**
 * Created by yecc on 2020/11/26 16:58
 */
public enum  Calculator {
    //加法运算
    ADD("+"){
        @Override
        public int exec(int a, int b){
            return a+b;
        }
    },
    //减法运算
    SUB("-"){
        @Override
        public int exec(int a, int b){
            return a - b;
        }
    };
    String value = "";
    //定义成员值类型
    private Calculator(String _value){
        this.value = _value;
    }
    //获得枚举成员的值
    public String getValue(){
        return this.value;
    }
    //声明一个抽象函数
    public abstract int exec(int a,int b);
}

package com.yecc.suanfa.strategy.enums;

/**
 * Created by yecc on 2020/11/26 16:59
 */
public class demo {
    public static void main(String[] args) {
        int a=10;
        int b=3;
        System.out.println(Calculator.ADD.exec(a, b));
        System.out.println(Calculator.SUB.getValue());
    }
}

注意:策略枚举是一个非常优秀和方便的模式,但是它受枚举类型的限制,每个枚举项
都是public、final、static的,扩展性受到了一定的约束,因此在系统开发中,策略枚举一般
担当不经常发生变化的角色。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值