Permalink
Browse files

Version19

So as you may have noticed I've been avoiding persistence throughout
this. It's a small, simple, contrived example which I didn't want to
muddy by adding any means of persistence. I realise this perhaps removes
validity to the example, but I don't really care. This is more about how to
model a domain with a modelling by example approach, as opposed to the
intricacies of database persistence with DDD.

However, I did want a tidier way of dealing with the state of the House.
Building the house in the constructor every time wasn't ideal. So I came
up with this idea of a factory. Because we all know houses come from
factories right? In essence it's more of a singleton, which works for me
here. I'm stil building the house on every request, but at least this
does it in a clear and re-usable manner. It also doesn't get me bogged
down in persistence when it's not my primary aim here.

The final thing to note is that I added some basic handlers to handle
the commands I created earlier. These are likely to change but are fine
for the time being.
  • Loading branch information...
jenkoian committed Jan 19, 2015
1 parent e4ace65 commit 0952bb45115bb90786b0b685a0ce04de41cf7091
@@ -0,0 +1,20 @@
<?php
namespace spec\Jenko\House\Factory;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class HomeAloneHouseFactorySpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('Jenko\House\Factory\HomeAloneHouseFactory');
$this->shouldHaveType('Jenko\House\Factory\HouseFactory');
}
function it_should_return_a_house()
{
self::getHouse()->shouldHaveType('Jenko\House\House');
}
}
@@ -0,0 +1,44 @@
<?php
namespace Jenko\House\Factory;
use Jenko\House\Garden;
use Jenko\House\House;
use Jenko\House\Room;
/**
* Note: Usually you would have some kind of persistence mechanism. However, in our case a static factory (essentially
* a singleton) is fine, as the house itself won't change.
*/
final class HomeAloneHouseFactory implements HouseFactory
{
/**
* @var House $house
*/
private static $house;
/**
* {@inheritdoc}
*/
public static function getHouse($currentLocation = null)
{
if (isset(static::$house)) {
return static::$house;
}
$locations = [
Garden::named('front garden'),
Room::named('hallway'),
Room::named('living room'),
Room::named('kitchen'),
];
static::$house = House::build($locations);
if (null !== $currentLocation) {
static::$house->enterRoom($currentLocation);
}
return static::$house;
}
}
@@ -0,0 +1,15 @@
<?php
namespace Jenko\House\Factory;
use Jenko\House\House;
use Jenko\House\Location;
interface HouseFactory
{
/**
* @param Location|string|null $currentLocation
* @return House
*/
public static function getHouse($currentLocation = null);
}
@@ -0,0 +1,24 @@
<?php
namespace Jenko\House\Handler;
use Jenko\House\Factory\HomeAloneHouseFactory;
final class EnterRoomHandler
{
private $house;
public function __construct()
{
$this->house = HomeAloneHouseFactory::getHouse();
}
/**
* @param $command
* @return mixed|void
*/
public function handle($command)
{
$this->house->enterRoom($command->room);
}
}
@@ -0,0 +1,24 @@
<?php
namespace Jenko\House\Handler;
use Jenko\House\Factory\HomeAloneHouseFactory;
final class ExitRoomHandler
{
private $house;
public function __construct()
{
$this->house = HomeAloneHouseFactory::getHouse();
}
/**
* @param $command
* @return mixed|void
*/
public function handle($command)
{
$this->house->exitToRoom($command->room);
}
}
@@ -39,7 +39,7 @@ public static function build(array $locations)
}
/**
* @return array
* @return Location[]|array
*/
public function getLocations()
{

0 comments on commit 0952bb4

Please sign in to comment.