0

I am currently learning the builder pattern. All the tutorials, everything is great, but when it comes to working with real data (not hard-coded strings inside your SetFoo(), SetBar() (in the builders)), I realized that I need to pass to the builder from inside the controllers (MVC). And then my controller happens to be the director right?

My constructor method is gone now since the data is already set by the setters of the builder.

Example:

fooBuilder.setBar("real data from db");
fooBuilder.setFoo("more real data from the database");

Does it violate the principles of the builder pattern?

geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
john
  • 5
  • 2

1 Answers1

0

I think it's OK to pass data to the builder by the controller.

But you can still keep the constructor method. Using Builder pattern does not require to remove the constructor. The essence is that it is the Builder class that knows how to create and build the part.

I suggest to rename the set method to build: BuildBar, BuildFoo.

Polymorphic
  • 420
  • 3
  • 14
  • I can't keep the construct method since the object inside the builder is already constructed... – john May 24 '18 at 09:45
  • I mean the builder pattern does not have any suggestion about not having constructor. So it's OK to keep the constructor. – Polymorphic May 25 '18 at 08:30