IDDD_Samples
IDDD_Samples copied to clipboard
Constructor vs Static Factory Method
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 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.