枚举实现策略模式

策略模式用好,可以消除大量的if else;

demo 是以vip充值为背景,可以充值一个月、三个月 、一年和 永久

直接上代码:

1、定义接口

/**
 * <p>
 * vip服务
 * </p>
 *
 * @author zsy
 * @since 2021/7/15
 */
public interface VipService {

    /**
     *<p>
     *  充值服务
     *</p>
     * @param month 月份
     * @return: void
     * @author: zsy
     * @time: 2021/7/15 15:29
     */
    void vipRecharge(int month);
}

2、分别实现一个月、三个月、一年、永久

public class OneVipServiceImpl implements VipService {
    /**
     * <p>
     * 充值服务
     * </p>
     *
     * @param month 月份
     * @return: void
     * @author: zsy
     * @time: 2021/7/15 15:29
     */
    @Override
    public void vipRecharge(int month) {
        //todo 业务逻辑
        System.out.println("充值vip,月份="+month);
    }
}




package com.zsy.common.test;

/**
 * <p>
 * 三个月vip
 * </p>
 *
 * @author zsy
 * @since 2021/7/15
 */
public class ThreeVipServiceImpl implements VipService {
    /**
     * <p>
     * 充值服务
     * </p>
     *
     * @param month 月份
     * @return: void
     * @author: zsy
     * @time: 2021/7/15 15:29
     */
    @Override
    public void vipRecharge(int month) {
        //todo 业务逻辑
        System.out.println("充值vip,月份="+month);

    }
}


package com.zsy.common.test;

/**
 * <p>
 * 一年vip
 * </p>
 *
 * @author zsy
 * @since 2021/7/15
 */
public class YearVipServiceImpl implements VipService  {
    /**
     * <p>
     * 充值服务
     * </p>
     *
     * @param month 月份
     * @return: void
     * @author: zsy
     * @time: 2021/7/15 15:29
     */
    @Override
    public void vipRecharge(int month) {
        //todo 业务逻辑
        System.out.println("充值vip,月份="+month);
    }
}


package com.zsy.common.test;

/**
 * <p>
 * 其他vip
 * </p>
 *
 * @author zsy
 * @since 2021/7/15
 */
public class OtherVipServiceImpl implements VipService {
    /**
     * <p>
     * 充值服务
     * </p>
     *
     * @param month 月份
     * @return: void
     * @author: zsy
     * @time: 2021/7/15 15:29
     */
    @Override
    public void vipRecharge(int month) {
        //todo 业务逻辑
        System.out.println("充值vip,月份="+month);

    }
}



3、定义枚举

package com.zsy.common.test;

import lombok.Data;

/**
 * <p>
 * vip枚举
 * </p>
 *
 * @author zsy
 * @since 2021/7/15
 */
public enum VipEnum {
    ONE_VIP(1,new OneVipServiceImpl()),
    THREE_VIP(3,new ThreeVipServiceImpl()),
    YEAR_VIP(12,new YearVipServiceImpl()),
    OTHERS_VIP(99999,new OtherVipServiceImpl());

    VipEnum(int month, VipService vipService) {
        this.month = month;
        this.vipService = vipService;
    }

    private int month;
    private VipService vipService;


    //匹配
    public static VipEnum match(String name){
        VipEnum[] values = VipEnum.values();
        for (VipEnum value : values) {
            if(value.name().equals(name)){
                return value;
            }
        }
        return null;
    }
    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public VipService getVipService() {
        return vipService;
    }

    public void setVipService(VipService vipService) {
        this.vipService = vipService;
    }
}

4、测试

package com.zsy.common.test;

import java.util.Optional;

/**
 * <p>
 *
 * </p>
 *
 * @author zsy
 * @since 2021/7/15
 */
public class TestMain {
    public static void main(String[] args) {

        String type = "ONE_VIP";
        VipEnum vipEnum = VipEnum.match(type);
        Optional.ofNullable(vipEnum).ifPresent((v)-> v.getVipService().vipRecharge(v.getMonth()));
        type = "THREE_VIP";
        vipEnum = VipEnum.match(type);
        Optional.ofNullable(vipEnum).ifPresent((v)-> v.getVipService().vipRecharge(v.getMonth()));
        type = "YEAR_VIP";
        vipEnum = VipEnum.match(type);
        Optional.ofNullable(vipEnum).ifPresent((v)-> v.getVipService().vipRecharge(v.getMonth()));
        type = "OTHERS_VIP";
        vipEnum = VipEnum.match(type);
        Optional.ofNullable(vipEnum).ifPresent((v)-> v.getVipService().vipRecharge(v.getMonth()));

    }
}

结果:

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值