【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();//畫一個正方形
相關文章
相關標籤/搜索