-
-
Save webdevilopers/fa3bbfbb8e2b36a4a4619217e6469fdc to your computer and use it in GitHub Desktop.
<?php | |
abstract class DormerType implements DormerTypeInterface | |
{} |
<?php | |
interface DormerTypeInterface | |
{ | |
public function canHaveGutterInstalled(): bool; | |
} |
<?php | |
class FlatRoofDormer extends DormerType | |
{ | |
public function canHaveGutterInstalled(): bool | |
{ | |
return true; | |
} | |
} |
Since DormerType
is an Entity I like the idea of only passing the DormerTypeId
in Version 2.
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:
Version 1:
Version 2:
Version 3: