initialize a class loading data from file. is it a builder?
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开发者_StackOverflow中文版 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?
no steps - no 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
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.
精彩评论