mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-12 19:06:24 +02:00
more abstract example
This commit is contained in:
52
Facade/Computer.php
Normal file
52
Facade/Computer.php
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
<?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.
|
||||||
|
*
|
||||||
|
* The first goal is to reduce coupling and folow the Law of Demeter.
|
||||||
|
*
|
||||||
|
* A Facade is meant to decouple a client and a sub-system with
|
||||||
|
* 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
|
||||||
|
* 2. You can (you shoud) have multiple facades for one sub-system
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
* [Abstract|Static|Simple] Factory [Method]
|
||||||
|
*
|
||||||
|
* The best facade has no new and a constructor with interface-type-hinted parameters
|
||||||
|
*/
|
||||||
|
class Computer
|
||||||
|
{
|
||||||
|
|
||||||
|
protected $opsys;
|
||||||
|
protected $bios;
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user