IDDD_Samples icon indicating copy to clipboard operation
IDDD_Samples copied to clipboard

Constructor vs Static Factory Method

Open jpoh97 opened this issue 5 years ago • 1 comments

Hi Vaughn.

First of all, thank u for contributions in computer science world.

While I was reading ur example, I asked myself about the domain models. I think that constructors have a lot of responsabilities, since they call set methods in which it validates and throw exceptions if it violates any domain rule. Why do u use constructors and not another pattern? Maybe a SFM (static factory method) or Builder can help to keep constructors simpler.

Thanks in advance.

jpoh97 avatar Nov 09 '18 21:11 jpoh97

@jpoh97 Thanks for your feedback. It's always the constructor's responsibility to initialize the object's state. The use of internal setter methods doesn't complicate that, just delegates some details. This technique is know as self-delegation. A Builder generally works for an anemic object, with some visibility on setter methods. A Factory Method can indeed be used in front of a private constructor to make creation more fluent.

VaughnVernon avatar Nov 10 '18 03:11 VaughnVernon