释义:

组成部分:

代码实现步骤:

1、定义Strategy接口(Strategy)

package com.example.strategy.service;


import com.example.strategy.entity.ResourcesEntity;

/**
 * 获取资源信息策略接口
 *
 */
public interface ResourcesStrategy {


    /*
    * 获取对应资源信息
    * 专家、队伍、装备
    * */
    ResourcesEntity getResourcesData(String type);


}

2、实体类、常量类定义(entity、constant)

//返回实体类
package com.example.strategy.entity;


import lombok.Data;

@Data
public class ResourcesEntity {

    private String name;
    private String lat;
    private String lng;

    private String resourcesType;

}

//常量类
package com.example.strategy.constant;

public class ResourcesConstant {
    public static final String EXPERT = "expert";
    public static final String EQUIP = "equip";
    public static final String MATERIALS = "materials";
    public static final String TEAM = "team";
}

3、具体的策略实现类(EquipStrategy、ExpertStrategy、TeamStrategy)

//装备策略实现类
package com.example.strategy;

import com.example.strategy.constant.ResourcesConstant;
import com.example.strategy.entity.ResourcesEntity;
import com.example.strategy.renum.ResourcesEnum;
import com.example.strategy.service.ResourcesStrategy;
import org.springframework.stereotype.Component;


@Component(ResourcesConstant.EQUIP)
public class EquipStrategy implements ResourcesStrategy {
    @Override
    public ResourcesEntity getResourcesData(String type) {
        ResourcesEntity resources = new ResourcesEntity();
        resources.setResourcesType(ResourcesEnum.EQUIP.id);
        resources.setName("装备");
        resources.setLng("");
        resources.setLat("");
        return resources;
    }
}

//专家策略实现类

package com.example.strategy;

import com.example.strategy.constant.ResourcesConstant;
import com.example.strategy.entity.ResourcesEntity;
import com.example.strategy.renum.ResourcesEnum;
import com.example.strategy.service.ResourcesStrategy;
import org.springframework.stereotype.Component;


@Component(ResourcesConstant.EXPERT)
public class ExpertStrategy implements ResourcesStrategy {
    @Override
    public ResourcesEntity getResourcesData(String type) {
        ResourcesEntity resources = new ResourcesEntity();
        resources.setResourcesType(ResourcesEnum.EXPERT.id);
        resources.setName("李安心专家");
        resources.setLng("");
        resources.setLat("");
        return resources;
    }
}


//队伍策略实现类
package com.example.strategy;

import com.example.strategy.constant.ResourcesConstant;
import com.example.strategy.entity.ResourcesEntity;
import com.example.strategy.renum.ResourcesEnum;
import com.example.strategy.service.ResourcesStrategy;
import org.springframework.stereotype.Component;


@Component(ResourcesConstant.TEAM)
public class TeamStrategy implements ResourcesStrategy {
    @Override
    public ResourcesEntity getResourcesData(String type) {
        ResourcesEntity resources = new ResourcesEntity();
        resources.setResourcesType(ResourcesEnum.TEAM.id);
        resources.setName("队伍1");
        resources.setLng("");
        resources.setLat("");
        return resources;
    }
}

4、策略环境(上下文)Context

package com.example.strategy.context;


import com.example.strategy.service.ResourcesStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class ResourcesStrategyContext {


    /*
    * 策略集合
    * 注入实例名和对应实例
    * */
    @Autowired
    private Map<String, ResourcesStrategy>  resourcesStrategyMap = new ConcurrentHashMap<>();


    /*
    * 初始化
    * */
    public ResourcesStrategyContext(Map<String, ResourcesStrategy> resourcesStrategyMap){
        //先进行清空处理
        this.resourcesStrategyMap.clear();
        //由于我们的具体策略实现业务类使用了@Component注解,所以会被spring自动扫描并且装配到容器中
        resourcesStrategyMap.forEach(this.resourcesStrategyMap::put);

    }

    public ResourcesStrategy getResourcesStrategy(String strategy){
        return  resourcesStrategyMap.get(strategy);
    }

}

5、功能验证

package com.example.strategy.controller;
import com.example.demo.util.StringUtils;
import com.example.strategy.context.ResourcesStrategyContext;
import com.example.strategy.entity.ResourcesEntity;
import com.example.strategy.renum.ResourcesEnum;
import com.example.strategy.service.ResourcesStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/resources")
public class ResourcesController {

    @Autowired
    ResourcesStrategyContext resourcesStrategyContext;

    @RequestMapping("/getResourcesData")
    public ResourcesEntity getResourcesData(String type){
        ResourcesStrategy resourcesStrategy = resourcesStrategyContext.getResourcesStrategy(type);
        ResourcesEntity resourcesData = resourcesStrategy.getResourcesData(type);
        return resourcesData;
    }
}

6、结果展示

策略模式解决if-else问题-LMLPHP

7、策略模式和if-esle比较: 

01-11 11:37