2013-05-11 16:13:43 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DesignPatternPHP
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace DesignPatterns\Facade;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The primary goal of a Facade Pattern is not to avoid you to read the manual of
|
|
|
|
* a complex API. It's only a side-effect.
|
|
|
|
*
|
2013-05-11 22:18:12 +02:00
|
|
|
* The first goal is to reduce coupling and follow the Law of Demeter.
|
2013-05-11 16:13:43 +02:00
|
|
|
*
|
2013-05-11 16:20:55 +02:00
|
|
|
* A Facade is meant to decouple a client and a sub-system by embedding
|
2013-05-11 16:13:43 +02:00
|
|
|
* many (but sometimes just one) interface, and of course to reduce complexity.
|
|
|
|
*
|
|
|
|
* 1. A facade does not forbid you the access to the sub-system
|
2013-05-11 16:20:55 +02:00
|
|
|
* 2. You can (you should) have multiple facades for one sub-system
|
2013-05-11 16:13:43 +02:00
|
|
|
*
|
|
|
|
* That's why a good facade has no "new" in it. If there are multiple creations
|
|
|
|
* for each method, it is not a Facade, it's a Builder or a
|
2013-05-11 16:20:55 +02:00
|
|
|
* [Abstract|Static|Simple] Factory [Method].
|
2013-05-11 16:13:43 +02:00
|
|
|
*
|
2013-05-11 16:20:55 +02:00
|
|
|
* The best facade has no new and a constructor with interface-type-hinted parameters.
|
|
|
|
* If you need creation of new instances, use Factory as argument.
|
|
|
|
*
|
2013-05-11 16:13:43 +02:00
|
|
|
*/
|
2013-05-11 16:43:14 +02:00
|
|
|
class Facade
|
2013-05-11 16:13:43 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
protected $opsys;
|
|
|
|
protected $bios;
|
|
|
|
|
2013-05-11 16:20:55 +02:00
|
|
|
/**
|
|
|
|
* This is the perfect time to use a dependency injection container
|
|
|
|
* to creaate an instance of this class
|
|
|
|
*/
|
2013-05-11 16:13:43 +02:00
|
|
|
public function __construct(BiosInterface $bios, OsInterface $os)
|
|
|
|
{
|
|
|
|
$this->bios = $bios;
|
|
|
|
$this->opsys = $os;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function turnOn()
|
|
|
|
{
|
|
|
|
$this->bios->execute();
|
|
|
|
$this->bios->waitForKeyPress();
|
|
|
|
$this->bios->launch($this->opsys);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function turnOff()
|
|
|
|
{
|
|
|
|
$this->opsys->halt();
|
|
|
|
$this->bios->powerDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|