gpt4 book ai didi

java - 创建同一个类的不同实例,哪种设计模式?

转载 作者:行者123 更新时间:2023-11-30 11:43:45 24 4
gpt4 key购买 nike

我有一个自定义对象 ExportType:

public class ExportType{

protected String name;
protected FetchingStrategy fetchStg;
protected ExportStrategy exportStg;

public ExportType(String name, FetchingStrategy fetch, ExportStrategy export) {
this.name = name;
this.fetchStg = fetch;
this.exportStg = export;
}

// ...
}

在我的应用程序中,我必须创建具有不同 FetchingStrategyExportStrategy 的导出类型列表。将来可以通过实现新的 FetchingStrategyExportStrategy 创建新的导出类型,因此我想将我的应用程序设计得尽可能灵活。

有没有我应该应用的设计模式来获得我需要的东西?为每个 ExportType 特定实例创建不同的 TypeFactory 是正确的方法吗?

更新

我试着总结一下我的问题:我正在开发一个用于从数据库导出数据的 Web 应用程序。我有几种从数据库中提取数据的方法 (ExportTypes),这些类型是通过 FetchingStrategyExportStrategy 的不同组合获得的。现在我需要创建这些“组合”的列表以便在必要时调用它们。我可以创建如下常量:

public static final ExportType TYPE_1 = new ExportType(..., ...);

但我想以某种方式实现它,以便我可以在未来添加新的组合/类型。

最佳答案

对此的最佳设计模式是使用为所有内容返回接口(interface)的工厂。然后,您可以抽象出所有实现,从而允许您灵活地发展和更改您的系统。

Spring 依赖注入(inject)是一个很好的解决方案

您最大的问题可能是在数据库级别,这更难抽象

关于java - 创建同一个类的不同实例,哪种设计模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11139524/

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