Design mode 9.1 - appearance mode

Keywords: PHP

Appearance mode: a unified interface is defined to access a group of interfaces in the subsystem.
Appearance pattern and adapter pattern are similar in implementation, but their application intention is different.
The adapter pattern is intended to change one interface into another, and the facade pattern is intended to simplify a group of interfaces

In the example, we have a subsystem, House, with a group of interfaces (door light air).
When we go home, we have to open the door, turn on the lights and turn on the air conditioner. A home action unifies multiple interfaces.

<?php
interface DoorInterface
{
    public function open();

    public function close();
}

interface LightInterface
{
    public function on();

    public function off();
}

interface AirInterface
{
    public function start();

    public function stop();
}

class Door implements DoorInterface
{
    public function open()
    {
        echo "open the door<br>\n";
    }

    public function close()
    {
        echo "close the door<br>\n";
    }
}

class Light implements LightInterface
{
    public function on()
    {
        echo "turn on the light<br>\n";
    }

    public function off()
    {
        echo "turn off the light<br>\n";
    }
}

class Air implements AirInterface
{
    public function start()
    {
        echo "start the air<br>\n";
    }

    public function stop()
    {
        echo "stop the air<br>\n";
    }
}



class House
{
    private $door;
    private $light;
    private $air;

    public function __construct(DoorInterface $door, LightInterface $light, AirInterface $air)
    {
        $this->door = $door;
        $this->light = $light;
        $this->air = $air;
    }

    public function goBackHome()
    {
        $this->door->open();
        $this->light->on();
        $this->air->start();
    }

    public function leaveHome()
    {
        $this->door->close();
        $this->light->off();
        $this->air->stop();
    }
}


class Test
{
    public function run()
    {
        $door = new Door();
        $light = new Light();
        $air = new Air();


        $house = new House($door, $light, $air);

        $house->goBackHome();
        echo "<hr>";
        $house->leaveHome();
    }
}

$test = new Test();

$test->run();

Posted by rxbanditboy1112 on Fri, 03 Apr 2020 07:55:24 -0700