Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constructor vs Static Factory Method #27

Open
jpoh97 opened this issue Nov 9, 2018 · 1 comment
Open

Constructor vs Static Factory Method #27

jpoh97 opened this issue Nov 9, 2018 · 1 comment

Comments

@jpoh97
Copy link

jpoh97 commented Nov 9, 2018

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.

@VaughnVernon
Copy link
Owner

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants