php facade design pattern mode (Facade)

Appearance mode (the Facade) belongs to a structural design mode, also known facade mode.

Facade mode hides the complexity of the system, and provides one or more interfaces for the client can access the system,
decoupling the client from the system, to reduce complexity.

Facade pattern defines a high-level interface that makes the subsystem easier to use, users only care to use interface, without having to be concerned about how the subsystem is implemented by the facade of the complex relationships subsystem model to address it.

Facade pattern like: You asked me Linux system boot process is like? I know where ah, I just press the power button, the computer opens. Computer power button is provided to interface to the user, the user does not have to be concerned about in the end is how to start a computer, and the computer itself to control Bios hardware self-test, the boot loader, kernel loads and other operations.

For Chestnut:

<?php
/**
 * Created by PhpStorm.
 * User: ClassmateLin
 * Date: 2019/9/8
 * Time: 8:52 下午
 * Desc: 外观模式
 */

class Bios {
    public function check()
    {
        echo '硬件自检' . PHP_EOL;
    }

    public function selectStarterDisc()
    {
        echo '选择启动盘' . PHP_EOL;
    }
}

class Grub {
    public function loader()
    {
        echo '加载引导' . PHP_EOL;
    }
}

class Kernel {
    public function loader()
    {
        echo '加载内核' . PHP_EOL;
    }
}

class Init {
    public function init()
    {
        echo 'os初始化' . PHP_EOL;
    }
}

class RunLevel {
    public function start()
    {
        echo '启动指定级别任务' . PHP_EOL;
    }
}



class Computer {
    private $bios;
    private $grub;
    private $kernel;
    private $init;
    private $runLevel;

    public function __construct()
    {
        $this->bios = new Bios();
        $this->grub = new Grub();
        $this->kernel = new Kernel();
        $this->init = new Init();
        $this->runLevel = new RunLevel();
    }

    public function start()
    {
        $this->bios->check();
        $this->bios->selectStarterDisc();
        $this->grub->loader();
        $this->init->init();
        $this->runLevel->start();
    }

    public function program()
    {
        echo "echo 'hello world'";
    }
}

$computer = new Computer();
$computer->start();
$computer->program();
Published 60 original articles · won praise 0 · Views 1423

Guess you like

Origin blog.csdn.net/ClassmateLin/article/details/100641974