自学内容网 自学内容网

《重学Java设计模式》之 工厂方法模式

《重学Java设计模式》之 建造者模式

《重学Java设计模式》之 原型模式

《重学Java设计模式》之 单例模式

模拟发奖多种商品

工程结构

奖品发放接口

package com.yys.mes.design.factory.store;

public interface ICommodity {

    /**
     * @Author Sherry
     * @Date 14:20 2024/11/6
     **/
    void sendCommodity(String uId, String commodityId, String bizId);
}

购物卡发放实现类

package com.yys.mes.design.factory.store.impl;

import com.yys.mes.design.factory.store.ICommodity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class CardCommodityService implements ICommodity {

    @Override
    public void sendCommodity(String uId, String commodityId, String bizId) {
        //购物卡
        log.info("购物卡发放成功!");
    }
}

优惠券发放实现类

package com.yys.mes.design.factory.store.impl;

import com.yys.mes.design.factory.store.ICommodity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@Slf4j
public class CouponCommodityService implements ICommodity {
    @Override
    public void sendCommodity(String uId, String commodityId, String bizId) {
        //优惠券发放
        log.info("优惠券发放成功!");
    }
}

实物发放实现类

package com.yys.mes.design.factory.store.impl;

import com.yys.mes.design.factory.store.ICommodity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class GoodsCommodityService implements ICommodity {
    @Override
    public void sendCommodity(String uId, String commodityId, String bizId) {
        //实物商品
        log.info("实物商品发放成功!");
    }
}

奖品类型工厂

通过 Map<String, ICommodity> 来存储所有的实现类。Spring 会自动将所有实现了 ICommodity 接口的 Bean 注入到这个 Map 中。我们可以通过传入类型(例如 "card")来获取对应的实现类。

package com.yys.mes.design.factory.config;

import com.yys.mes.design.factory.store.ICommodity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class CommodityFactory {

    private final Map<String, ICommodity> commodityServices;

    @Autowired
    public CommodityFactory(Map<String, ICommodity> commodityServices) {
        this.commodityServices = commodityServices;
    }

    // 根据类型返回对应的ICommodity实现
    public ICommodity getCommodityService(String type) {
        ICommodity commodityService = commodityServices.get(type + "CommodityService");
        if (commodityService == null) {
            throw new IllegalArgumentException("未知的奖品类型: " + type);
        }
        return commodityService;
    }
}

调用工厂

package com.yys.mes.design.factory.config;

import com.yys.mes.design.factory.store.ICommodity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class StoreFactory {

    private final CommodityFactory commodityFactory;

    @Autowired
    public StoreFactory(CommodityFactory commodityFactory) {
        this.commodityFactory = commodityFactory;
    }

    public void distributeCommodity(String type, String uId, String commodityId, String bizId) {
        // 通过工厂获取对应的ICommodity实现
        ICommodity commodityService = commodityFactory.getCommodityService(type);
        commodityService.sendCommodity(uId, commodityId, bizId);
    }
}

测试验证

package com.yys.mes.design.factory.Controller;

import com.yys.mes.design.factory.config.StoreFactory;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/send")
public class ExampleController {
    @Resource
    private StoreFactory storeFactory;

    @GetMapping("/distribute")
    public String distributeCommodity(@RequestParam String type, @RequestParam String uId,
                                      @RequestParam String commodityId, @RequestParam String bizId) {
        storeFactory.distributeCommodity(type, uId, commodityId, bizId);
        return "奖品发放成功!";
    }
}


原文地址:https://blog.csdn.net/WaaTong/article/details/143572159

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!