class SubsystemA { public function operation1(): string { return "SubsystemA: Ready!<br/>"; } // ... public function operationN(): string { return "SubsystemA: Go!<br/>"; } }
class SubsystemB { public function operation1(): string { return "SubsystemB: Get ready!<br/>"; } // ... public function operationZ(): string { return "SubsystemB: Fire!<br/>"; } }now create class Facade add code: Depending on your application's needs, you can provide the Facade with existing subsystem objects or force the Facade to create them on its own. The Facade's methods are convenient shortcuts to the sophisticated functionality of the subsystems. However, clients get only to a fraction of a subsystem's capabilities.
class Facade { protected $subsystem1; protected $subsystem2; public function __construct( SubsystemA $subsystem1 = null, SubsystemB $subsystem2 = null ) { $this->subsystem1 = $subsystem1 ?: new SubsystemA; $this->subsystem2 = $subsystem2 ?: new SubsystemB; } public function operation(): string { $result = "Facade initializes subsystems:<br/>"; $result .= $this->subsystem1->operation1(); $result .= $this->subsystem2->operation1(); $result .= "Facade orders subsystems to perform the action:<br/>"; $result .= $this->subsystem1->operationN(); $result .= $this->subsystem2->operationZ(); return $result; } }last we go to the index page we add includes the file we just created
include_once ('Facade.php'); include_once ('SubsystemA.php'); include_once ('SubsystemB.php');and add code:
function clientCode(Facade $facade) { echo $facade->operation(); } $subsystemA = new SubsystemA; $subsystemB = new SubsystemB; $facade = new Facade($subsystemA, $subsystemB); clientCode($facade);Let's now take a look through a btowser.
Facade initializes subsystems: SubsystemA: Ready! SubsystemB: Get ready! Facade orders subsystems to perform the action: SubsystemA: Go! SubsystemB: Fire!Facade is a structural design pattern that provides a simplified interface to a library, a framework, or any other complex set of classes. I hope you’ve enjoyed this episode Facade design pattern
The Ray Code is AWESOME!!!
wikipedia
Find Ray on:
youtube
The Ray Code
Ray Andrade