spring增强抽象工厂,继承改组合优化

This commit is contained in:
fujiayang
2022-04-08 14:28:53 +08:00
parent d10845eb7e
commit 25e546e024
4 changed files with 42 additions and 52 deletions

View File

@@ -0,0 +1,27 @@
package top.fjy8018.designpattern.pattern.creational.factorymethod.spring.after;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.fjy8018.designpattern.pattern.creational.factorymethod.spring.bean.FormDataHandler;
import top.fjy8018.designpattern.pattern.creational.factorymethod.spring.bean.FormItemConverter;
import top.fjy8018.designpattern.pattern.creational.factorymethod.spring.bean.FormItemTypeEnum;
/**
* TODO:
*
* @author F嘉阳
* @date 2022/4/8 14:26
*/
@Configuration
public class FactoryConfig {
@Bean
public StrategyFactory<String, FormDataHandler> formDataHandlerFactory() {
return new StrategyFactory<>(FormDataHandler.class);
}
@Bean
public StrategyFactory<FormItemTypeEnum, FormItemConverter> formItemConverterFactory() {
return new StrategyFactory<>(FormItemConverter.class);
}
}

View File

@@ -1,14 +0,0 @@
package top.fjy8018.designpattern.pattern.creational.factorymethod.spring.after;
import org.springframework.stereotype.Component;
import top.fjy8018.designpattern.pattern.creational.factorymethod.spring.bean.FormDataHandler;
/**
* TODO:
*
* @author F嘉阳
* @date 2022/4/8 14:14
*/
@Component
public class FormDataHandlerFactory extends AbstractStrategyFactory<String, FormDataHandler> {
}

View File

@@ -1,15 +0,0 @@
package top.fjy8018.designpattern.pattern.creational.factorymethod.spring.after;
import org.springframework.stereotype.Component;
import top.fjy8018.designpattern.pattern.creational.factorymethod.spring.bean.FormItemConverter;
import top.fjy8018.designpattern.pattern.creational.factorymethod.spring.bean.FormItemTypeEnum;
/**
* TODO:
*
* @author F嘉阳
* @date 2022/4/8 14:17
*/
@Component
public class FormItemConverterFactory extends AbstractStrategyFactory<FormItemTypeEnum, FormItemConverter> {
}

View File

@@ -6,8 +6,6 @@ import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Map;
@@ -17,12 +15,23 @@ import java.util.Map;
* @author F嘉阳
* @date 2022/4/8 13:42
*/
public abstract class AbstractStrategyFactory<T, S extends Strategy<T>> implements InitializingBean, ApplicationContextAware {
public class StrategyFactory<T, S extends Strategy<T>> implements InitializingBean, ApplicationContextAware {
private Map<T, S> strategyMap;
private ApplicationContext appContext;
private final Class<S> strategyType;
/**
* 创建一个策略工厂
*
* @param strategyType 策略的类型
*/
public StrategyFactory(Class<S> strategyType) {
this.strategyType = strategyType;
}
/**
* 根据策略 id 获得对应的策略的 Bean
*
@@ -36,13 +45,14 @@ public abstract class AbstractStrategyFactory<T, S extends Strategy<T>> implemen
@Override
public void afterPropertiesSet() throws Exception {
// 获取 Spring 容器中所有 S 类型的 Bean
Collection<S> strategies = appContext.getBeansOfType(getStrategyType()).values();
Collection<S> strategies = appContext.getBeansOfType(strategyType).values();
strategyMap = Maps.newHashMapWithExpectedSize(strategies.size());
// 所有 S 类型的 Bean 放入到 strategyMap
for (final S strategy : strategies) {
T id = strategy.getId();
strategyMap.put(id, strategy);
}
}
@@ -51,22 +61,4 @@ public abstract class AbstractStrategyFactory<T, S extends Strategy<T>> implemen
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
appContext = applicationContext;
}
/**
* 通过反射获取策略的类型
*
* @return 策略的类型
*/
protected Class<S> getStrategyType() {
// getClass 获取当前运行时实例的类getGenericSuperclass 获得泛型父类
Type superclass = getClass().getGenericSuperclass();
ParameterizedType pt = (ParameterizedType) superclass;
Type[] actualTypeArguments = pt.getActualTypeArguments();
// 获得索引为 1 的实际参数类型即第二个实际参数的类型
Type actualTypeArgument = actualTypeArguments[1];
@SuppressWarnings("unchecked")
Class<S> result = (Class<S>) actualTypeArgument;
return result;
}
}