工厂+策略模式代码演示

工厂+策略模式代码演示

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.运行结果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

附录

1.策略模式例子1
2.策略模式例子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);
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

boy快快长大

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值