From f698221882d42a07df3c5d7156e65ed7511771a4 Mon Sep 17 00:00:00 2001 From: cloud Date: Sat, 16 Apr 2022 01:02:39 +0800 Subject: [PATCH] =?UTF-8?q?=E9=97=A8=E9=9D=A2=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/Structural/Facade/BiosInterface.php | 29 ++++++++++++ demo/Structural/Facade/Facade.php | 49 +++++++++++++++++++++ demo/Structural/Facade/OsInterface.php | 19 ++++++++ demo/Structural/Facade/Run.php | 41 +++++++++++++++++ demo/Structural/Facade/Tests/FacadeTest.php | 37 ++++++++++++++++ 5 files changed, 175 insertions(+) create mode 100644 demo/Structural/Facade/BiosInterface.php create mode 100644 demo/Structural/Facade/Facade.php create mode 100644 demo/Structural/Facade/OsInterface.php create mode 100644 demo/Structural/Facade/Run.php create mode 100644 demo/Structural/Facade/Tests/FacadeTest.php diff --git a/demo/Structural/Facade/BiosInterface.php b/demo/Structural/Facade/BiosInterface.php new file mode 100644 index 0000000..36be334 --- /dev/null +++ b/demo/Structural/Facade/BiosInterface.php @@ -0,0 +1,29 @@ +bios = $bios; + $this->os = $os; + } + + /** + * 构建基础输入输出系统执行启动方法。 + */ + public function turnOn() + { + $this->bios->execute(); + $this->bios->waitForKeyPress(); + $this->bios->launch($this->os); + } + + /** + * 构建系统关闭方法。 + */ + public function turnOff() + { + $this->os->halt(); + $this->bios->powerDown(); + } +} \ No newline at end of file diff --git a/demo/Structural/Facade/OsInterface.php b/demo/Structural/Facade/OsInterface.php new file mode 100644 index 0000000..b03c299 --- /dev/null +++ b/demo/Structural/Facade/OsInterface.php @@ -0,0 +1,19 @@ +createMock('DesignPatterns\Structural\Facade\OsInterface'); + + $os->method('getName') + ->will($this->returnValue('Linux')); + + $bios = $this->getMockBuilder('DesignPatterns\Structural\Facade\BiosInterface') + ->setMethods(['launch', 'execute', 'waitForKeyPress']) + ->disableAutoload() + ->getMock(); + + $bios->expects($this->once()) + ->method('launch') + ->with($os); + + $facade = new Facade($bios, $os); + + // 门面接口很简单。 + $facade->turnOn(); + + // 但你也可以访问底层组件。 + $this->assertEquals('Linux', $os->getName()); + } +} \ No newline at end of file