門面模式

<?php
declare(strict_types=1);

// facade 就是使用一個簡單的接口去執行很多複雜的方法。
// php 技術羣:781742505
/**
 * Class Computer
 */
class Computer
{
    /**
     *
     */
    public function getElectricShock()
    {
        echo "Ouch!";
    }

    /**
     *
     */
    public function makeSound()
    {
        echo "Beep beep!";
    }

    /**
     *
     */
    public function showLoadingScreen()
    {
        echo "Loading..";
    }

    /**
     *
     */
    public function bam()
    {
        echo "Ready to be used!";
    }

    /**
     *
     */
    public function closeEverything()
    {
        echo "Bup bup bup buzzzz!";
    }

    /**
     *
     */
    public function sooth()
    {
        echo "Zzzzz";
    }

    /**
     *
     */
    public function pullCurrent()
    {
        echo "Haaah!";
    }
}

/**
 * Class ComputerFacade
 */
class ComputerFacade
{
    /**
     * @var Computer
     */
    protected $computer;

    /**
     * ComputerFacade constructor.
     *
     * @param Computer $computer
     */
    public function __construct(Computer $computer)
    {
        $this->computer = $computer;
    }

    /**
     *
     */
    public function turnOn()
    {
        $this->computer->getElectricShock();
        $this->computer->makeSound();
        $this->computer->showLoadingScreen();
        $this->computer->bam();
    }

    /**
     *
     */
    public function turnOff()
    {
        $this->computer->closeEverything();
        $this->computer->pullCurrent();
        $this->computer->sooth();
    }
}


$computer = new ComputerFacade(new Computer());
$computer->turnOn(); // Ouch! Beep beep! Loading.. Ready to be used!
$computer->turnOff(); // Bup bup buzzz! Haah! Zzzzz


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章