gpt4 book ai didi

java - 具有复合或抽象工厂的工厂

转载 作者:行者123 更新时间:2023-12-02 12:57:32 24 4
gpt4 key购买 nike

我有以下类层次结构:

public interface RateSheetProductExtractor
{
List<ProductData> extract(SGQuotingData quotingData);
}


@Service
@Scope("requestjob")
public class ProductExtractorFactory
{
@Autowired
private OptionalRatingProductExtractorFactory optionalRatingProductExtractorFactory;
@Autowired
private QuotePlanProductExtractorFactory quotePlanProductExtractorFactory;
@Autowired
private AlternateProductExtractorFactory alternateProductExtractorFactory;

public RateSheetProductExtractor getProductExtractor(
TablePlanType tablePlanType,
TableRatingType ratingType)
{
switch (tablePlanType)
{
case QUOTED:
return quotePlanProductExtractorFactory.getInstance(ratingType);
case OPTIONAL:
return optionalRatingProductExtractorFactory.getInstance(ratingType);
case ALTERNATE:
return alternateProductExtractorFactory.getInstance(ratingType);
default:
throw new IllegalArgumentException(tablePlanType + " is unsupported yet.");
}
}
}

@Service
@Scope("requestjob")
public class OptionalRatingProductExtractorFactory
{
@Autowired
private AcaProductExtractor acaProductExtractor;
@Autowired
private AgeRatedAsCompositeProductExtractor ageRatedAsCompositeProductExtractor;

public RateSheetProductExtractor getInstance(TableRatingType ratingType)
{
switch (ratingType)
{
case AGE:
return ageRatedAsCompositeProductExtractor;
case COMPOSITE:
return acaProductExtractor;
default:
throw new IllegalArgumentException(
"Product extractor isn't defined for optional plan type and rating type " + ratingType);

}
}
}


@Service
@Scope("requestjob")
public class QuotePlanProductExtractorFactory
{
@Autowired
private GfProductExtractor gfExtractor;
@Autowired
private AgeRatedProductExtractor ageRatedProductExtractor;
@Autowired
private MedicalProductExtractor medicalProductExtractor;

public RateSheetProductExtractor getInstance(TableRatingType ratingType)
{
switch (ratingType)
{
case AGE:
return ageRatedProductExtractor;
case COMPOSITE_AGE:
return gfExtractor;
case COMPOSITE:
return medicalProductExtractor;
default:
throw new IllegalArgumentException(
ratingType + " is unsupported for QuotePlan product extractor factory.");
}
}
}


@Service
@Scope("requestjob")
public class AlternateProductExtractorFactory
{
@Autowired
private AlternateAgeRatedProductExtractor ageRatedProductExtractor;

public RateSheetProductExtractor getInstance(TableRatingType ratingType)
{
if (ratingType == AGE)
{
return ageRatedProductExtractor;
}
throw new IllegalArgumentException(
ratingType + " is unsupported for QuotePlan product extractor factory.");
}
}
将 ProductExtractorFactory 称为 AbstractProductExtractorFactory(基于适当的模式)是否正确?或者它更像是一个简单的工厂中的工厂?

抱歉,列表太大...:)

最佳答案

我不这么认为。

1) ProductExtractorFactory 是一个具体类,在 Java 中,甚至在 AbstractFactory 模式中,以 Abstract 开头的类是/应该是一个抽象类。

2)在抽象工厂中,每个工厂应该有一个具体的 RateSheetProductExtractor 子类型,但在您的代码示例中我没有看到它们。

3)您的示例更像是工厂和复合模式。

重构以使用复合:

定义通用工厂接口(interface):

public interface RateSheetProductExtractorFactory{

public RateSheetProductExtractor getProductExtractor(TablePlanType tablePlanType, TableRatingType ratingType);
}

定义复合类:

public class CompositeRateSheetProductExtractorFactory implements RateSheetProductExtractorFactory{
private Map<String, RateSheetProductExtractorFactory> factoryMap = null;

public void setFactoryMap(Map<String, RateSheetProductExtractorFactory> factoryMap){
this.factoryMap = factoryMap;
}

@Override
public RateSheetProductExtractor getProductExtractor(TablePlanType tablePlanType, TableRatingType ratingType){
RateSheetProductExtractorFactory factory = factoryMap.get( tablePlanType + "-" + ratingType );

if (factory == null) throw new IllegalArgumentException( tablePlanType + "-" + ratingType + " is unsupported yet.");

return factory.getProductExtractor(tablePlanType, ratingType);
}
}

实现每个提取器及其各自的工厂:

public class AcaProductExtractorFactory implements RateSheetProductExtractorFactory{

@Override
public RateSheetProductExtractor getProductExtractor(TablePlanType tablePlanType, TableRatingType ratingType){
return new AcaProductExtractor();
}
}

public class AcaProductExtractor implements RateSheetProductExtractor{

@Override
public List<ProductData> extract(SGQuotingData quotingData) {
return null;
}
}



public class AgeRatedAsCompositeProductExtractorFactory implements RateSheetProductExtractorFactory{

@Override
public RateSheetProductExtractor getProductExtractor(TablePlanType tablePlanType, TableRatingType ratingType){
return new AgeRatedAsCompositeProductExtractor();
}
}

public class AgeRatedAsCompositeProductExtractor implements RateSheetProductExtractor{

@Override
public List<ProductData> extract(SGQuotingData quotingData) {
return null;
}
}



public class MedicalProductExtractorFactory implements RateSheetProductExtractorFactory{

@Override
public RateSheetProductExtractor getProductExtractor(TablePlanType tablePlanType, TableRatingType ratingType){
return new MedicalProductExtractor();
}
}

public class MedicalProductExtractor implements RateSheetProductExtractor{
@Override
public List<ProductData> extract(SGQuotingData quotingData) {
return null;
}
}

Spring XML:

<bean id="ageRatedAsCompositeProductExtractorFactory" class="AgeRatedAsCompositeProductExtractorFactory"/>
<bean id="medicalProductExtractorFactory" class="MedicalProductExtractorFactory"/>
<bean id="acaProductExtractorFactory" class="AcaProductExtractorFactory"/>

<bean id="extractorFactory" class="CompositeRateSheetProductExtractorFactory">
<property name="factoryMap">
<map>
<entry key="QUOTED-AGE" value-ref="ageRatedAsCompositeProductExtractorFactory" />
<entry key="QUOTED-COMPOSITE" value-ref="medicalProductExtractorFactory" />
<entry key="ALTERNATE-AGE" value-ref="ageRatedAsCompositeProductExtractorFactory" />
<entry key="OPTIONAL-COMPOSITE" value-ref="acaProductExtractorFactory" />
</map>
</property>
</bean>

关于java - 具有复合或抽象工厂的工厂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44377275/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com