【php设计模式】门面模式

时间:2019-06-13
本文章向大家介绍【php设计模式】门面模式,主要包括【php设计模式】门面模式使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

门面模式又叫外观模式,用来隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口。这种类型的设计模式属于结构型模式,它向现有的系统添加一个接口,来隐藏系统的复杂性。

这种模式涉及到一个单一的类,该类提供了客户端请求的简化方法和对现有系统类方法的委托调用。

<?php

interface Shape{
    public function draw();
}

class Circle implements Shape{
    public function draw(){
        echo "画一个圆形\n";
    }
}

class Rectangle implements Shape{
    public function draw(){
        echo "画一个矩形\n";
    }
}

class Square implements Shape{
    public function draw(){
        echo "画一个正方形\n";
    }
}

class ShapeMark{

    public $circle;
    public $rectangle;
    public $square;

    public function __construct(){
        $this->circle = new Circle();
        $this->rectangle = new Rectangle();
        $this->square = new Square();
    }

    public function drawCircle(){
        $this->circle->draw();
    }

    public function drawRectangle(){
        $this->rectangle->draw();
    }

    public function drawSquare(){
        $this->square->draw();
    }
}

$shapemark = new shapemark();
$shapemark->drawCircle();//画一个圆形
$shapemark->drawRectangle();//画一个矩形
$shapemark->drawSquare();//画一个正方形

原文地址:https://www.cnblogs.com/itsuibi/p/11016019.html