策略模式实践

目录

前言

五个部分

名词解释

代码

controller层

HelloService接口

实现类

自定义注解

上下文

策略工厂

[Java SPI配置](#Java SPI配置)

验证


前言

五个部分

接口、实现类、自定义注解、上下文、策略工厂

名词解释

自定义注解(方便后期增加实现类后灵活控制策略)

上下文(初始化接口,进行数据承接)

策略工厂(利用java SPI使接口与实现解耦,并通过验证注解是否存在,调用不同的策略)

代码

controller层

java 复制代码
package com.zsp.sheji.JavaSPI;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("hello")
public class HelloController {

    @PostMapping("/pay")
    public String pay(){
        PayContext payContext = new PayContext();
        payContext.setHelloService(PayFactory.makeHello("helloOne"));
        String result = payContext.sayHello("你好");
        return result;
    }
}

HelloService接口

java 复制代码
package com.zsp.sheji.JavaSPI;

public interface HelloService {
    String sayHello(String hello);
}

实现类

这里写了两个实现类,模拟真实环境中的不同策略调用

HelloOneServiceImpl

java 复制代码
package com.zsp.sheji.JavaSPI;

import org.springframework.stereotype.Service;

@Pay(type = "helloOne")
@Service
public class HelloOneServiceImpl implements HelloService{
    @Override
    public String sayHello(String hello) {
       return  hello + "=== one";
    }
}

HelloTwoServiceImpl

java 复制代码
package com.zsp.sheji.JavaSPI;

import org.springframework.stereotype.Service;

@Pay(type = "helloTwo")
@Service
public class HelloTwoServiceImpl implements HelloService{
    @Override
    public String sayHello(String hello) {
        return hello + "=== two";
    }
}

自定义注解

java 复制代码
package com.zsp.sheji.JavaSPI;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Pay {
    String type();
}

上下文

java 复制代码
package com.zsp.sheji.JavaSPI;

import org.springframework.stereotype.Component;

@Component
public class PayContext {

    private HelloService helloService;

    public void setHelloService(HelloService helloService){
        this.helloService = helloService;
    }

    public PayContext(){}

    public String sayHello(String hello){
        // 上下文进行数据承接
        return this.helloService.sayHello(hello);
    }
}

策略工厂

java 复制代码
package com.zsp.sheji.JavaSPI;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.ServiceLoader;

public class PayFactory {
    private static Map<String,HelloService> helloMap = new HashMap<>();

    static {
        ServiceLoader<HelloService> load = ServiceLoader.load(HelloService.class);
        Iterator<HelloService> iterator = load.iterator();
        while (iterator.hasNext()) {
            HelloService next = iterator.next();
            Class<? extends HelloService> aClass = next.getClass();
            if (!aClass.isAnnotationPresent(com.zsp.sheji.JavaSPI.Pay.class)) {
                // 不存在添加进去
                throw new IllegalStateException("class: " + aClass + " expect @com.zsp.sheji.策略模式高级注解方式.PayType, but not found!");
            }
            helloMap.put(aClass.getAnnotation(Pay.class).type(), next);
        }
    }

    public static HelloService makeHello(String type){
        return helloMap.get(type);
    }
}

Java SPI配置

文件名:com.zsp.sheji.JavaSPI.HelloService 对应接口的全限定类名

java 复制代码
com.zsp.sheji.JavaSPI.HelloOneServiceImpl
com.zsp.sheji.JavaSPI.HelloTwoServiceImpl

项目结构

验证

相关推荐
koping_wu15 小时前
【设计模式】设计模式原则、单例模式、工厂模式、模板模式、策略模式
单例模式·设计模式·策略模式
kkk_皮蛋15 小时前
WebRTC 中的临界锁实现:从 CritScope 到 RAII 机制的深度解析
webrtc·策略模式
世转神风-2 天前
qt-弹框提示-界面提醒
开发语言·qt·策略模式
Wcowin3 天前
OneClip 开发经验分享:从零到一的 macOS 应用开发
经验分享·macos·策略模式
开心香辣派小星7 天前
23种设计模式-19策略模式(Strategy Pattern)
java·设计模式·策略模式
weixin_4624462311 天前
【原创实践】python版playwright截取多个图
开发语言·python·策略模式
MC丶科12 天前
Java设计模式漫画英雄宇宙之策略模式:从支付系统重构到软考高频考点(附完整代码 + 面试高频问法)
java·设计模式·重构·策略模式
benxin123412 天前
macOS 上使用 Homebrew 安装和配置 PostgreSQL 的详细步骤
macos·postgresql·策略模式
佛祖让我来巡山14 天前
设计模式深度解析:策略模式、责任链模式与模板模式
设计模式·责任链模式·策略模式·模版模式
王柏龙16 天前
Win11 无法找到本地组策略编辑器的解决方法
windows·策略模式