设计模式-策略模式

策略模式

定义:定义了算法族,分别封装起来,让他们之间可以互相替换,此模式的变化独立于算法的使用者

有一个电商平台,当用户消费满1000元,根据vip等级可享受优惠

  • 普通会员 不打折
  • 白银会员 优惠50元
  • 黄金会员 打8折
  • 白金会员 优惠50元,再打七折

Version 1.0

会员类型枚举

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public enum UserType {
/**
* ORDINARY_VIP: 普通会员
* SILVER_VIP: 白银会员
* GOLD_VIP: 黄金会员
* PLATINUM_VIP:白金会员
*/
ORDINARY_VIP(1),
SILVER_VIP(2),
GOLD_VIP(3),
PLATINUM_VIP(4);
private int code;
UserType(int code) {
this.code = code;
}

public int getCode() {
return code;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
private static double getResult(long money, int type){
double result = money;
if(money >= 1000){
if(type == UserType.SILVER_VIP.getCode()){
result = money - 50;
}else if(type == UserType.GOLD_VIP.getCode()){
result = money * 0.8;
}else if(type == UserType.PLATNUM_VIP.getCode()){
result = (money - 50)*0.7;
}
}
return result;
}

下面使用策略模式来优化

Version 1.2

策略接口

1
2
3
public interface Strategy {
double compute(long money);
}

普通会员

1
2
3
4
5
6
public class OrdinaryStrategy implements Strategy{
@Override
public double compute(long money) {
return money;
}
}

黄金会员

1
2
3
4
5
6
public class GoldStrategy implements Strategy{
@Override
public double compute(long money) {
return money*0.8;
}
}

白金会员

1
2
3
4
5
6
public class PlatinumStrategy implements Strategy{
@Override
public double compute(long money) {
return (money-50)*0.7;
}
}

白银会员

1
2
3
4
5
6
public class SilverStrategy implements Strategy{
@Override
public double compute(long money) {
return money-50;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static double getResult(long money,int type){
double result = money;
if(money<1000){
return result;
}
if(type== UserType.ORDINARY_VIP.getCode()){
result = new OrdinaryStrategy().compute(money);
}else if(type == UserType.SILVER_VIP.getCode()){
result = new SilverStrategy().compute(money);
}else if(type == UserType.GOLD_VIP.getCode()){
result = new GoldStrategy().compute(money);
}else if(type==UserType.PLATINUM_VIP.getCode()){
result = new PlatinumStrategy().compute(money);
}
return result;
}

Version 2.1

1
2
3
4
5
6
7
8
9
10
11
12
13
public class StrategyFactory {
public static Map<Integer,Strategy> map;
private StrategyFactory(){
map = new HashMap<>();
init();
}
private void init(){
map.put(UserType.ORDINARY_VIP.getCode(), new OrdinaryStrategy());
map.put(UserType.SILVER_VIP.getCode(), new SilverStrategy());
map.put(UserType.GOLD_VIP.getCode(),new GoldStrategy());
map.put(UserType.PLATINUM_VIP.getCode(),new PlatinumStrategy());
}
}
1
2
3
4
5
6
7
8
9
public static double getResult(int userType, long money){
if(money<1000){
return money;
}
if(StrategyFactory.map.get(userType)==null){
throw new IllegalArgumentException("参数错误,无此类型的用户");
}
return map.get(userType).compute(money);
}

这里的工厂还可以通过这种方式来实现

Version 2.2

策略接口

1
2
3
4
public interface Strategy {
double compute(long money);
int getType();
}

黄金会员

1
2
3
4
5
6
7
8
9
10
11
12
public class GoldStrategy implements Strategy {
@Override
public double compute(long money) {
return money*0.8;
}

@Override
public int getType() {
return UserType.GOLD_VIP.getCode();
}

}

普通会员

1
2
3
4
5
6
7
8
9
10
11
public class OrdinaryStrategy implements Strategy {
@Override
public double compute(long money) {
return money;
}

@Override
public int getType() {
return UserType.ORDINARY_VIP.getCode();
}
}

白金会员

1
2
3
4
5
6
7
8
9
10
11
public class PlatinumStrategy implements Strategy {
@Override
public double compute(long money) {
return (money-50)*0.7;
}

@Override
public int getType() {
return UserType.PLATINUM_VIP.getCode();
}
}

白银会员

1
2
3
4
5
6
7
8
9
10
11
public class SilverStrategy implements Strategy {
@Override
public double compute(long money) {
return money-50;
}

@Override
public int getType() {
return UserType.SILVER_VIP.getCode();
}
}

策略工厂

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class StrategyFactory {
private static Map<Integer, Strategy> map;

public StrategyFactory() {
map=new HashMap<>();
init();
}
private void init() {
List<Strategy> strategies=new ArrayList<>();
strategies.add(new GoldStrategy());
strategies.add(new OrdinaryStrategy());
strategies.add(new PlatinumStrategy());
strategies.add(new SilverStrategy());
map=strategies.stream().collect(Collectors.toMap(Strategy::getType,strategy -> strategy));
}
}
1
2
3
4
5
6
7
8
9
public static double getResult(int userType, long money){
if(money<1000){
return money;
}
if(StrategyFactory.map.get(userType)==null){
throw new IllegalArgumentException("参数错误,无此类型的用户");
}
return map.get(userType).compute(money);
}
给作者买杯咖啡吧~~~