工厂+策略模式代码演示
1. 代码结构及代码
public enum EatEnum {
eatChicken("吃鸡"),
eatNoodles("吃面"),
eatDonkeyFire("吃驴肉火烧");
private final String type;
EatEnum(String type) {
this.type = type;
}
public String getType() {
return type;
}
}
------
public class Topic {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
------
@Service
public class ChickenServiceImpl implements IService {
private final String desc = "小鸡真好吃,一份16元。每次鸡肉越来越少!";
@Override
public String handler(Topic topic) {
return topic.getName() + "-------->" + desc;
}
}
------
@Service
public class DonkeyFireServiceImpl implements IService {
private final String desc = "这里是自己付钱,现宝不用提心吊胆大家不给钱了,子松说这方便面这么贵啊!";
@Override
public String handler(Topic topic) {
return topic.getName() + "-------->" + desc;
}
}
------
@Service
public class NoodlesServiceImpl implements IService {
private final String desc = "多加辣椒,少咸汤,总是想买个饼,每次爬坡!";
@Override
public String handler(Topic topic) {
return topic.getName() + "-------->" + desc;
}
}
------
@Service
public class EatService {
@Resource
private ServiceFactory serviceFactory;
public String eat(Topic topic) {
return serviceFactory.getService(topic.getName()).handler(topic);
}
}
------
public interface IService {
String handler(Topic topic);
}
------
@Service
public class ServiceFactory {
private static final Map<String, IService> map = new ConcurrentHashMap<>();
static {
map.put(EatEnum.eatChicken.getType(), new ChickenServiceImpl());
map.put(EatEnum.eatNoodles.getType(), new NoodlesServiceImpl());
map.put(EatEnum.eatDonkeyFire.getType(), new DonkeyFireServiceImpl());
}
public IService getService(String type) {
return map.get(type);
}
}
------
class Java1230ApplicationTests {
@Resource
EatService eatService;
@Test
void contextLoads() {
Topic topic = new Topic();
topic.setName(EatEnum.eatChicken.getType());
System.out.println(eatService.eat(topic));
}
}
2.运行结果
附录
demo
public interface MoPeopleDetailConfigStrategy {
/**
* 组装数据抽象处理逻辑方法
*
* @param name String 员工名字
* @param content String 配置内容
* @param item MoPeopleConfigListVo.Item 数据列表
*/
void process(String name, String content, MoPeopleConfigListVo.Item item);
}
-------------------------
public class SecondLevelApproverStrategy implements MoPeopleDetailConfigStrategy {
@Override
public void process(String name, String content, MoPeopleConfigListVo.Item item) {
StringBuilder secondLevelApprover = new StringBuilder(item.getSecondLevelApprover());
item.setSecondLevelApprover(secondLevelApprover.append(name).append(",").toString());
List<String> secondLevelApproverFeishuIdList = item.getSecondLevelApproverFeishuIdList();
secondLevelApproverFeishuIdList.add(content);
item.setSecondLevelApproverFeishuIdList(secondLevelApproverFeishuIdList);
}
}
-----------------------------
public class FirstLevelApproverStrategy implements MoPeopleDetailConfigStrategy {
@Override
public void process(String name, String content, MoPeopleConfigListVo.Item item) {
StringBuilder firstLevelApprover = new StringBuilder(item.getFirstLevelApprover());
item.setFirstLevelApprover(firstLevelApprover.append(name).append(",").toString());
List<String> firstLevelApproverFeishuIdList = item.getFirstLevelApproverFeishuIdList();
firstLevelApproverFeishuIdList.add(content);
item.setFirstLevelApproverFeishuIdList(firstLevelApproverFeishuIdList);
}
}
----------------------------
略其他Strategy
Factory
public class MoPeopleDetailConfigFactory {
private static final Map<Integer, MoPeopleDetailConfigStrategy> STRATEGY_MAP;
static {
STRATEGY_MAP = new HashMap<>();
STRATEGY_MAP.put(MoConfigDetailPeopleContentTypeEnum.ABNORMAL_WORK_ORDER_INFLUENCER.getCode(), new AbnormalWorkOrderInfluencerStrategy());
STRATEGY_MAP.put(MoConfigDetailPeopleContentTypeEnum.FIRST_LEVEL_APPROVER.getCode(), new FirstLevelApproverStrategy());
STRATEGY_MAP.put(MoConfigDetailPeopleContentTypeEnum.SECOND_LEVEL_APPROVER.getCode(), new SecondLevelApproverStrategy());
STRATEGY_MAP.put(MoConfigDetailPeopleContentTypeEnum.OPER.getCode(), new OpUserStrategy());
STRATEGY_MAP.put(MoConfigDetailPeopleContentTypeEnum.DEPARTMENTAL_SURPLUS.getCode(), new DepartmentalSurplusStrategy());
}
public static MoPeopleDetailConfigStrategy createStrategy(Integer contentType) {
return STRATEGY_MAP.get(contentType);
}
}
MoPeopleDetailConfigStrategy strategy = MoPeopleDetailConfigFactory.createStrategy(contentType);
if (strategy != null) {
strategy.process(name, content, item);
}