服务提供者框架适用于”服务有多个实现”的解耦, 比如商城平台接口有淘宝, 天猫, 京东等多实现.

    这里拿商城接口做示例, 结构如下图

    • MallServiceProvider中进行服务注册
    • MallService作为顶层服务接口, 当然还可以进一步根据业务功能细分, 比如OrderService ,ProductService(extends MallService) 等等
    • AbstractMallServic作为接口的抽象实现, 定义算法骨架, 提取具体实现中通用的操作, 比如通用校验等image.png

    MallServiceProvider

    1. @Component
    2. public class MallServiceProvider {
    3. private static final Map<MallType, MallService> MALL_SERVICE_MAP = new ConcurrentHashMap<>();
    4. public MallServiceProvider(List<MallService> mallServices) {
    5. for (MallService mallService : mallServices) {
    6. for (MallType mallType : mallService.supports()) {
    7. MALL_SERVICE_MAP.put(mallType, mallService);
    8. }
    9. }
    10. }
    11. public MallService getMallService(MallType mallType) {
    12. return MALL_SERVICE_MAP.getOrDefault(mallType, MALL_SERVICE_MAP.get(MallType.DEFAULT));
    13. }
    14. }

    MallService

    1. /**
    2. * @author xinzhang
    3. * @date 2021/1/29 14:09
    4. */
    5. public interface MallService {
    6. /**
    7. * 示例方法
    8. */
    9. void demoFunction();
    10. /**
    11. * 支持哪些平台
    12. *
    13. * @return
    14. */
    15. MallType[] supports();
    16. }

    AbstractMallService

    1. /**
    2. * @author xinzhang
    3. * @date 2021/1/29 14:11
    4. */
    5. public abstract class AbstractMallService implements MallService {
    6. @Override
    7. public void demoFunction() {
    8. System.out.println("一些通用校验以及处理, 巴拉巴拉");
    9. doDemoFunction();
    10. }
    11. /**
    12. * 具体实现
    13. */
    14. protected abstract void doDemoFunction();
    15. }

    DefaultMallService

    1. @Component
    2. public class DefaultMallService extends AbstractMallService {
    3. @Override
    4. protected void doDemoFunction() {
    5. }
    6. @Override
    7. public MallType[] supports() {
    8. return new MallType[]{
    9. MallType.DEFAULT
    10. };
    11. }
    12. }

    JdMallService

    1. @Component
    2. public class JdMallService extends AbstractMallService {
    3. @Override
    4. protected void doDemoFunction() {
    5. System.out.println("这里是京东商城服务");
    6. }
    7. @Override
    8. public MallType[] supports() {
    9. return new MallType[]{
    10. MallType.JD
    11. };
    12. }
    13. }

    TaoBaoMallService

    1. @Component
    2. public class TaoBaoMallService extends AbstractMallService {
    3. @Override
    4. protected void doDemoFunction() {
    5. System.out.println("这里是淘宝, 天猫平台服务");
    6. }
    7. @Override
    8. public MallType[] supports() {
    9. return new MallType[]{
    10. MallType.TAOBAO, MallType.TMALL
    11. };
    12. }
    13. }

    MallType枚举

    1. public enum MallType {
    2. DEFAULT("默认空实现", 0),
    3. TAOBAO("淘宝", 1),
    4. TMALL("天猫", 2),
    5. JD("京东", 3);
    6. private final String caption;
    7. private final int value;
    8. MallType(String caption, int value) {
    9. this.caption = caption;
    10. this.value = value;
    11. }
    12. public int value() {
    13. return this.value;
    14. }
    15. }

    测试

    1. @RunWith(SpringRunner.class)
    2. @SpringBootTest
    3. public class DemoApplicationTests {
    4. @Autowired
    5. private MallServiceProvider mallServiceProvider;
    6. @Test
    7. public void testConfiguration() {
    8. MallService mallService = mallServiceProvider.getMallService(MallType.TAOBAO);
    9. mallService.demoFunction();
    10. }
    11. }
    12. --------------输出
    13. 一些通用校验以及处理, 巴拉巴拉
    14. 这里是淘宝, 天猫平台服务