gpt4 book ai didi

java - 初始化一个从文件加载数据的类。是建筑商吗?

转载 作者:行者123 更新时间:2023-11-29 06:09:33 24 4
gpt4 key购买 nike

我是设计模式的新手。
我想创建一个类的实例,比如 ClassA,并将它的一些字段设置为从配置文件中读取的值。
如果我将类的代码与使用值管理文件的代码区分开来,那么 ClassA 将成为一个纯粹的“业务逻辑”类。

 class ClassA {
boolean config1;
String originDB;
String destDB;
//other fields not initialized at the construction time
}

class ClassAFactory {
boolean config1;
String originDB;
String destDB;

public ClassAFactory {
//constructor of the class factory
//load config values from file
}

public ClassA newInstance() {
//create a new instance of ClassA and config fields config1, originDB, destDB
}

}

我会说这是一种构建器模式,因为构建器似乎“不仅负责实例化”,还负责初始化。
但除此之外,构建器似乎专注于逐步打破创建过程,而在我的情况下,我只有一个(但复合)步骤。

可以被认为是构建器模式吗?还是有什么不同?

最佳答案

没有步骤 - 没有Builder

    ClassA a = ClassABuilder
.config(config1)
.originDB(originDB)
.destDB(destDB)
.build();
// above reads a Builder to me and hopefully to any reader
// note above allows to swap, add, remove particular steps


ClassA a = ClassAFactory.newInstance();
// above reads Factory to me and hopefully to any reader
// note for reader, it's a one-step thing

关于java - 初始化一个从文件加载数据的类。是建筑商吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7407783/

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