初始化一个从文件加载数据的类。是建筑商吗?
我是设计模式的新手。
我想创建一个类的实例(例如 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
}
}
我想说这是一种构建器模式,因为构建器似乎不仅负责实例化,还负责初始化。
但除此之外,构建器似乎专注于分步打破创建过程,而就我而言,我只有一个(但复合)步骤。
可以被认为是构建器模式吗?或者有什么不同吗?
I am a newbie in design patterns.
I want to create an instance of a class, say ClassA, and set some of its fields to the values read from a config file.
If I keep distinct the code of the class from the code that manages the file with the values then ClassA becomes a pure "businness logic" class.
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
}
}
I would say that this is a builder pattern because builder seems to take care of "not only instantiation" but initialization too.
But otherwise, builder seems to be focused on breaking the creation process in steps, while in my case I have only one (yet compound) step.
May be considered a builder pattern? Or is it something different?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有步骤 - 没有Builder
no steps - no Builder
看起来更像工厂模式而不是构建器。这是因为只有一个方法可以完成所有工作。通常,构建器会有一组 setter 样式的方法,允许自定义正在构建的对象。
Looks more like a Factory pattern than a Builder. This is because there is only an single method that does all the work. Generally a builder would have a set of setter-style methods that would allow for the customization of the object being built.