文章目录
一、定义接口 二、定义工厂类 三、实现接口的处理类[组件] 四、调用
一、定义接口
package com. interotc. handler ;
public interface IStrategy {
< T > T execute ( T parameter) throws Throwable ;
}
二、定义工厂类
package com. interotc. handler ;
import java. util. Map ;
import java. util. concurrent. ConcurrentHashMap ;
public class StrategyFactory {
private static final Map < Integer , IStrategy > HANDLERS = new ConcurrentHashMap < > ( ) ;
public static IStrategy getInstance ( Integer index) {
return HANDLERS . get ( index) ;
}
public static void register ( Integer index, IStrategy tokenRequest) {
HANDLERS . put ( index, tokenRequest) ;
}
}
三、实现接口的处理类[组件]
package com. interotc. handler ;
import com. alibaba. fastjson. JSON ;
import com. alibaba. fastjson. JSONObject ;
import com. interotc. config. CodeEnum ;
import com. interotc. config. CustomConfig ;
import com. interotc. exception. EcException ;
import com. interotc. utils. ConvertUtil ;
import com. interotc. utils. CsipWebClient ;
import lombok. extern. slf4j. Slf4j ;
import org. aspectj. lang. ProceedingJoinPoint ;
import org. springframework. beans. factory. InitializingBean ;
import org. springframework. stereotype. Component ;
import javax. annotation. Resource ;
import java. util. TreeMap ;
import static com. interotc. config. CustomConstants . * ;
@Slf4j
@Component
public class TokenProceedHandler implements IStrategy , InitializingBean {
@Override
public < T > T execute ( T parameter) throws Throwable {
return null ;
}
@Override
public void afterPropertiesSet ( ) {
StrategyFactory . register ( I_TOKEN_PROCEED , this ) ;
}
}
四、调用
JSONObject res = StrategyFactory . getInstance ( I_TOKEN_REQUEST ) . execute ( parameter) ;