After looking at your other question I don't believe you are talking about the gang of fours builder pattern. I think you're talking about Josh Bloch's builder pattern.
It lets you write code like this:
NutritionFacts cocaCola = new NutritionFacts
.Builder(240, 8)
.calories(100)
.sodium(35)
.carbohydrate(27)
.build()
;
The point here is to simulate named parameters in languages that don't have them natively. This allows the creation of an immutable value object with many fields without having to resort to setters or long unreadable constructors. Since the need for this is highly language dependent it really has nothing to do with anything Domain Driven Design talks about. If it did then certain languages couldn't be used for DDD. I think DDD would be fine with this so long as you use good names. So no, I don't think DDD prohibits use of builders.
Keep in mind, be it factory, builder, or new
, this is construction code. It shouldn't be casually mixed with your behavioral code.
And of course you can use factories with builders to separate the concrete implementation from the more abstract formula.
Nutrition secretFormula(Nutrition nutrition) {
return nutrition
.Builder(240, 8)
.calories(100)
.sodium(35)
.carbohydrate(27)
.build()
;
}
Nutrition dietFormula(Nutrition nutrition) {
return nutrition
.Builder(240, 8)
.calories(0)
.sodium(40)
.carbohydrate(0)
.build()
;
}
I think this shows that the ubiquitous language can be made manifest in builders. Your domain expert should be able to read this just fine.
DDD
does not conern itself with implementation detail which is why I was confused when it was suggested to use aFactory
rather thanBuilder
. Is aFactory
not also an implementation detail?