0

I'd like to create object that will be created based on several other objects. I want to use Builder pattern but I have some doubts. I can see that all examples for Builder pattern show that builder has simple methods (e.g. withName(), withSurname(), etc.). Each of these methods assign input parameter to field within builder. Then build() method creates targeted object.

I want to use more complex objects as parameters for these methods. In some of these methods I'm going have more complex logic to create value of field that will be assigned within builder.

Is builder pattern right choice for this? Can builder methods have more complex logic? Should I use another design pattern?

Kara
  • 6,115
  • 16
  • 50
  • 57
John Smith
  • 173
  • 2
  • 10
  • IIRC, the builder pattern's motivation is to avoid bloated constructors with many parameters and many overloaded constructors. I don't think the complexity of builder's methods necessarily need to be constrained to simple assignments. However, I would compare the builder with a factory and see which might be a better fit. – David Osborne Jun 15 '17 at 10:20
  • This might be useful https://stackoverflow.com/questions/757743/what-is-the-difference-between-builder-design-pattern-and-factory-design-pattern – Gayan Dasanayake Jun 16 '17 at 04:19

1 Answers1

3

Programming isn't about following patterns to the T. If standard patterns don't fit, make your own. They serve as guides since they exhibit a well tested design.

In this case you can have complex methods if they are necessary, I've seen builders like that before.

Neil Locketz
  • 4,228
  • 1
  • 23
  • 34