【案例】
黑棗設計公司開發一套流程圖開發工具。使用這套開發工具在畫一個箭頭時,用戶先畫一個三角,再畫一個堅杠就可以了。不過用戶反饋畫箭頭太麻煩,希望能精簡操作。
<代碼模擬>
class base
{
public function draw($char = '*', $num = 1)
{
echo str_repeat($char, $num);
}
}
class Triangle extends base
{
public function __construct()
{
$this->draw(' ', 10);
$this->draw('*', 1);
$this->draw("\n", 1);
$this->draw(' ', 9);
$this->draw('*', 3);
$this->draw("\n", 1);
$this->draw(' ', 8);
$this->draw('*', 5);
$this->draw("\n", 1);
$this->draw(' ', 7);
$this->draw('*', 7);
$this->draw("\n", 1);
$this->draw(' ', 6);
$this->draw('*', 9);
$this->draw("\n", 1);
}
}
class CaineStick extends base
{
public function __construct()
{
$this->draw(' ', 9);
$this->draw('*', 3);
$this->draw("\n", 1);
$this->draw(' ', 9);
$this->draw('*', 3);
$this->draw("\n", 1);
$this->draw(' ', 9);
$this->draw('*', 3);
$this->draw("\n", 1);
$this->draw(' ', 9);
$this->draw('*', 3);
$this->draw("\n", 1);
$this->draw(' ', 9);
$this->draw('*', 3);
$this->draw("\n", 1);
$this->draw(' ', 9);
$this->draw('*', 3);
$this->draw("\n", 1);
}
}
class testDriver
{
public function run()
{
$step1 = new Triangle();
$step2 = new CaineStick();
}
}
$test = new testDriver();
$test->run();
【分析OOA】
在應用進程中的一個步驟抱很許多復雜的邏輯步驟和方法調用時,創建一個外觀模式的對象,使用外觀模式隱藏了自調用對象的復雜性問題。我們可以新建一個箭頭類,箭頭類將代碼中的step1\step2封裝起來。對於用戶來說,不用先畫三角再畫豎杠,直接調用箭頭類就可以了。
【設計OOD】
<UML>
<說明>
外觀類FacedeArrow:這個外觀類為用戶提供一個畫箭頭的簡單接口,該類依賴於Triangle三角及CaineStick豎杠類
【編程 OOP】
<代碼>
class base
{
public function draw($char = '*', $num = 1)
{
echo str_repeat($char, $num);
}
}
class Triangle extends base
{
public function __construct()
{
$this->draw(' ', 10);
$this->draw('*', 1);
$this->draw("\n", 1);
$this->draw(' ', 9);
$this->draw('*', 3);
$this->draw("\n", 1);
$this->draw(' ', 8);
$this->draw('*', 5);
$this->draw("\n", 1);
$this->draw(' ', 7);
$this->draw('*', 7);
$this->draw("\n", 1);
$this->draw(' ', 6);
$this->draw('*', 9);
$this->draw("\n", 1);
}
}
class CaineStick extends base
{
public function __construct()
{
$this->draw(' ', 9);
$this->draw('*', 3);
$this->draw("\n", 1);
$this->draw(' ', 9);
$this->draw('*', 3);
$this->draw("\n", 1);
$this->draw(' ', 9);
$this->draw('*', 3);
$this->draw("\n", 1);
$this->draw(' ', 9);
$this->draw('*', 3);
$this->draw("\n", 1);
$this->draw(' ', 9);
$this->draw('*', 3);
$this->draw("\n", 1);
$this->draw(' ', 9);
$this->draw('*', 3);
$this->draw("\n", 1);
}
}
//箭頭類
class FacadeArrow extends base
{
public function __construct(){
$step1 = new Triangle();
$step2 = new CaineStick();
}
}
【測試用例Test Case】
<代碼>
class testDriver
{
public function run()
{
$step1 = new FacadeArrow();
}
}
$test = new testDriver();
$test->run();
【輸出】
小結:
【模式實現要點】
外觀模式是軟件工程中常用的一種軟件設計模式。它為子系統中的一組接口提供一個統一的高層接口。使用子系統更容易使用。
【優點】
外觀模式作為結構型模式中的一個簡單又實用的模式,外觀模式通過封裝細節來提供大粒度的調用,直接的好處就是,封裝細節,提供了應用寫程序的可維護性和易用性。
【適用場景】
外觀模式一般應用在系統架構的服務層中,當我們是多個不同類型的客戶端應用程序時,比如一個系統既可以在通過Web的形式訪問,也可以通過客戶端應用程序的形式時,可能通過外觀模式來提供遠程服務,讓應用程序進行遠程調用,這樣通過外觀形式提供服務,那么不管是什么樣的客戶端都訪問一致的外觀服務,那么以后就算是我們的應用服務發生變化,那么我們不需要修改沒一個客戶端應用的調用,只需要修改相應的外觀應用即可。
********************************************
* 作者:葉文濤
* 標題:Php設計模式之【外觀模式[Facade Pattern】
* 參考:
*《設計模式:可復用面向對象軟件基礎 》(美)Erich Gamma 等著
*《Head First設計模式》Eric Freeman等著
* http://www.cnblogs.com/hegezhou_hot/archive/2010/12/06/1897398.html
******************轉載請注明網址來源 ***************
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。