feat: 使用抽象泛型工厂模式解决问题,增强稳定性,可无限复用
This commit is contained in:
parent
4742c1d50c
commit
dd2f780249
@ -18,9 +18,21 @@ import java.util.List;
|
||||
*/
|
||||
public final class Queries {
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
private static final List<QueryChainFactory> FACTORIES =
|
||||
SpringFactoriesLoader.loadFactories(QueryChainFactory.class, null);
|
||||
|
||||
/**
|
||||
* 内部带参工厂函数,调用工厂方法创建实例
|
||||
*
|
||||
* @param type 工厂类型,最终产出类型需匹配
|
||||
* @param <C> 产出查询链泛型
|
||||
*/
|
||||
private static <C extends QueryChain<C, ?>> C produce(Class<?> type) {
|
||||
QueryChainFactory<C> factory = getFactory(type);
|
||||
return factory.produce();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建基于字符串字段名的查询
|
||||
*
|
||||
@ -28,7 +40,7 @@ public final class Queries {
|
||||
* @return 结果
|
||||
*/
|
||||
public static QueryCondition<NamedQueryChain> where(String column) {
|
||||
NamedQueryChain chain = getFactory(NamedQueryChain.class).produce();
|
||||
NamedQueryChain chain = produce(NamedQueryChain.class);
|
||||
return chain.and(column);
|
||||
}
|
||||
|
||||
@ -39,7 +51,7 @@ public final class Queries {
|
||||
* @return 结果
|
||||
*/
|
||||
public static <T extends Domain> QueryCondition<LambdaQueryChain<T>> where(DomainFunction<T> getter) {
|
||||
LambdaQueryChain<T> chain = getFactory(LambdaQueryChain.class).produce();
|
||||
LambdaQueryChain<T> chain = produce(LambdaQueryChain.class);
|
||||
return chain.and(getter);
|
||||
}
|
||||
|
||||
@ -50,11 +62,10 @@ public final class Queries {
|
||||
* @return 结果
|
||||
*/
|
||||
public static NamedQueryChain within(NamedQueryChain chain) {
|
||||
NamedQueryChain created = getFactory(LambdaQueryChain.class).produce();
|
||||
NamedQueryChain created = produce(LambdaQueryChain.class);
|
||||
return created.and(chain);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 以嵌套条件开始
|
||||
*
|
||||
@ -62,7 +73,7 @@ public final class Queries {
|
||||
* @return 结果
|
||||
*/
|
||||
public static <T extends Domain> LambdaQueryChain<T> within(LambdaQueryChain<T> chain) {
|
||||
LambdaQueryChain<T> created = getFactory(LambdaQueryChain.class).produce();
|
||||
LambdaQueryChain<T> created = produce(LambdaQueryChain.class);
|
||||
return created.and(chain);
|
||||
}
|
||||
|
||||
@ -72,7 +83,8 @@ public final class Queries {
|
||||
* @param targetType 目标类型
|
||||
* @return 匹配的工厂
|
||||
*/
|
||||
private static QueryChainFactory getFactory(Class<?> targetType) {
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <C extends QueryChain<C, ?>> QueryChainFactory<C> getFactory(Class<?> targetType) {
|
||||
return FACTORIES.stream()
|
||||
.filter(factory -> factory.supports(targetType))
|
||||
.findFirst()
|
||||
|
@ -3,7 +3,7 @@ package com.flyfish.framework.query.spi;
|
||||
import com.flyfish.framework.query.QueryChain;
|
||||
|
||||
/**
|
||||
* 查询链创建抽象工厂
|
||||
* 查询链创建抽象泛型工厂
|
||||
* 基于spi提供实现类,最终返回真正实例化的内容
|
||||
* 根据引入的jar包自动加载,无需手动处理
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user