Created
February 25, 2019 18:28
-
-
Save webdevilopers/fa3bbfbb8e2b36a4a4619217e6469fdc to your computer and use it in GitHub Desktop.
Using Entity as Factory for Value Object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class DormerType implements DormerTypeInterface | |
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface DormerTypeInterface | |
{ | |
public function canHaveGutterInstalled(): bool; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class FlatRoofDormer extends DormerType | |
{ | |
public function canHaveGutterInstalled(): bool | |
{ | |
return true; | |
} | |
} |
After reading the following post on @culttt by @philipbrown I added a Version 3 with a pure factory.
Due to the coupled nature of the Factory and the object, it is usually fine to allow the Factory to protect the invariants of the object.
At the bottom line versions 1 and 2 "enforce" the invariants. A gutter
can not be added to a DormerType
that does not support it.
Version 3 protects the invariants but a developer could still create a Dormer
by passing any type and any gutter.
But is this a requirement at all - protecting an object from inconsistently being created by the developer?
Version 3 "feels right".
What are your thoughts on this?
Discussion continued on phpddd:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Since
DormerType
is an Entity I like the idea of only passing theDormerTypeId
in Version 2.