1

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?

AgostinoX
  • 7,477
  • 20
  • 77
  • 137

2 Answers2

3

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
gnat
  • 6,213
  • 108
  • 53
  • 73
  • Also a builder usually constructs a graph of objects. E.g. a Document conssiting of a header, a footer, a title and some paragraphs. Nice example: +1 – Angel O'Sphere Sep 21 '11 at 14:05
1

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.

John B
  • 32,493
  • 6
  • 77
  • 98
  • I thought to the factory pattern too, then I've been disoriented because the factory method pattern seems to be focused on the insulation from the different concrete implementation of an interface. All things that i don't have. – AgostinoX Sep 13 '11 at 20:26
  • 1
    I think @gnat's comment below does a great job of explaining. – John B Sep 14 '11 at 10:00