策略模式用好,可以消除大量的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()));
}
}
结果: