The slides for a talk I gave for the Software Craftsmanship Barcelona 2014 about Domain-Driven Design (DDD) on the main Tactical Design Patterns with examples written in PHP
“Beside using the handful of literal values offered by the language, and an even smaller complement of objects normally used as values, you will make and use new objects that represent the meaningful quantities of your business”
class Location { private $country; private $city; private $postalCode;
public function __construct(Country $country, City $city, PostalCode $postalCode) { $this->setCountry($country); $this->setCity($city); $this->setPostalCode($postalCode); }
public function country() { return $this->country; }
public function city() { return $this->city; }
public function postalCode() { return $this->postalCode; }
For each type of object that needs global access, create an object that can provide the illusion of an in-memory collection of all objects of that type
interface OrderRepository { function orderOfId(OrderId $anOrderId); function ordersOfUser(UserId $aUserId); function add(Order $anOrder); function remove(Order $anOrder); }
public function publish($aDomainEvent) { foreach ($this->subscribers as $aSubscriber) { if (get_class($aDomainEvent) === $aSubscriber->subscribedTo()) { $aSubscriber->handle($aDomainEvent); } } } }