搜尋
首頁後端開發php教程PHP組合模式詳解及案例

PHP組合模式詳解及案例

May 16, 2018 pm 02:29 PM
php案例詳解

本篇主要介紹PHP組合模式詳解及案例,有興趣的朋友參考下,希望對大家有幫助。

這個模式理解起來會有些歧義,特別是某些書上面那些難懂的闡述。先來說說組合模式的幾個特點:

1、必須存在不可分割基本元素。

2、組合後的物體可以被組合。

舉個通俗的例子,原子是化學反應的基本微粒,它在化學反應中不可分割。現在有 C(碳)、H(氫)、O(氧)、N(氮)4種原子,它們可以隨機組合成無數種分子,可以是蛋白質,也可以是脂肪,蛋白質和脂肪就是組合。由蛋白質和脂肪又可以一起組合成肉、大豆等等。

回到主題,現在有一個需求,客戶需要建立一個葉子,可以設定葉子大小和顏色,可以為葉子命名。

abstract class tree{  
    abstract function create();  
}  
class createLeaf extends tree{  
      
    private $name;  
    private $size;  
    private $color;  
    private $leaf=array();  
      
    public function __construct($name,$size,$color){  
        $this->name=$name;  
        $this->size=$size;  
        $this->color=$color;  
    }  
      
    public function create(){  
        $this->leaf[$this->name]=array(  
            'size'=>$this->size,  
            'color'=>$this->color  
        );  
        return $this->leaf;  
    }  
      
}  
  
$leaf=new createLeaf('大红树叶','大','红');  
  
print_r($leaf->create());  
  
运行以上代码将得到:  
  
  
Array  
(  
    [大红树叶] => Array  
        (  
            [size] => 大  
            [color] => 红  
        )  
  
)

我們的設計完美的實現了客戶的需求,但是,現在客戶的新要求來了,不僅要可以創建葉子,還要可以創建樹枝,而且,可以把葉子安插在樹枝上,也可以把安插好的葉子從樹枝上拆下來。他們最終想要結果是,樹枝上可以安插其他的樹枝,從而構建出一顆枝繁葉茂的大樹


分析:創建葉子和創建樹枝都擁有創建操作,所以它們都可以對抽象tree類別進行實現,但創建樹枝的類別還需要安插和拆除的操作,所以我們暫且在tree類別中加上兩個抽象方法combination() 和separation()。

abstract class tree{  
    abstract function create();//创建  
    abstract function combination(tree $item);//组合  
    abstract function separation(tree $item);//分离  
}  
  
class createLeaf extends tree{  
      
    private $name;  
    private $size;  
    private $color;  
    private $leaf=array();  
      
    public function __construct($name,$size,$color){  
        $this->name=$name;  
        $this->size=$size;  
        $this->color=$color;  
    }  
      
    public function create(){  
        $this->leaf[$this->name]=array(  
            'size'=>$this->size,  
            'color'=>$this->color  
        );  
        return $this->leaf;  
    }  
  
    //由于创建叶子类不需要组合和分离的操作,我们将这两个方法投掷出错误警告。      
    public function combination(tree $item){  
        throw new Exception("本类不支持组合操作");  
    }  
      
    public function separation(tree $item){  
        throw new Exception("本类不支持分离操作");  
    }  
      
}  
  
class createBranch extends tree{  
  
    private $name;  
    private $branch=array();  
    private $items=array();//树枝可能被安插叶子,该变量用于存放叶子对象  
      
    public function __construct($name){  
        $this->name=$name;  
    }  
      
    //我们已经知道$items内的对象都包含创建操作,所以只要依次执行各对象的创建操作,收集结果便可  
    public function create(){  
        foreach($this->items as $item){  
            $arr=$item->create();  
            $this->branch[$this->name][]=$arr;  
        }  
        if(empty($this->branch)){  
            $this->branch[$this->name]=array();  
        }  
        return $this->branch;  
    }  
      
    public function combination(tree $item){  
        $this->items[]=$item;  
    }  
      
    public function separation(tree $item){  
        $key=array_search($item,$this->items);  
        if($key!==false){  
            unset($this->items[$key]);  
        }  
    }  
  
}  
  
$leaf_1=new createLeaf('大红树叶','大','红');  
$leaf_2=new createLeaf('大绿树叶','大','绿');  
$leaf_3=new createLeaf('大黄树叶','大','黄');  
  
$leaf_4=new createLeaf('小红树叶','小','红');  
$leaf_5=new createLeaf('小绿树叶','小','绿');  
$leaf_6=new createLeaf('小黄树叶','小','黄');  
  
$branch_1=new createBranch('树枝1号');  
$branch_1->combination($leaf_1);  
$branch_1->combination($leaf_2);  
$branch_1->combination($leaf_3);  
  
$branch_2=new createBranch('树枝2号');  
$branch_2->combination($leaf_4);  
$branch_2->combination($leaf_5);  
$branch_2->combination($leaf_6);  
  
$branch=new createBranch('树干');  
$branch->combination($branch_1);  
$branch->combination($branch_2);  
  
print_r($branch->create());  
  
运行以上代码将得到:  
  
Array  
(  
    [树干] => Array  
        (  
            [0] => Array  
                (  
                    [树枝1号] => Array  
                        (  
                            [0] => Array  
                                (  
                                    [大红树叶] => Array  
                                        (  
                                            [size] => 大  
                                            [color] => 红  
                                        )  
  
                                )  
  
                            [1] => Array  
                                (  
                                    [大绿树叶] => Array  
                                        (  
                                            [size] => 大  
                                            [color] => 绿  
                                        )  
  
                                )  
  
                            [2] => Array  
                                (  
                                    [大黄树叶] => Array  
                                        (  
                                            [size] => 大  
                                            [color] => 黄  
                                        )  
  
                                )  
  
                        )  
  
                )  
  
            [1] => Array  
                (  
                    [树枝2号] => Array  
                        (  
                            [0] => Array  
                                (  
                                    [小红树叶] => Array  
                                        (  
                                            [size] => 小  
                                            [color] => 红  
                                        )  
  
                                )  
  
                            [1] => Array  
                                (  
                                    [小绿树叶] => Array  
                                        (  
                                            [size] => 小  
                                            [color] => 绿  
                                        )  
  
                                )  
  
                            [2] => Array  
                                (  
                                    [小黄树叶] => Array  
                                        (  
                                            [size] => 小  
                                            [color] => 黄  
                                        )  
  
                                )  
  
                        )  
  
                )  
  
        )  
  
)

我們漂亮的完成了這個需求,一顆蒼天大樹被我們創建,但這裡有一個問題,創建樹葉操作只需要create() 操作,並不需要combination() 和separation() ,我們為何不把抽象類別tree拆分為兩個類別呢?

abstract class tree{  
    abstract function create();  
}  
  
//拆分出的树干抽象类,由于继承自tree,必须将create()实现,但实现create()又会造成代码重复,所以将此类也申明为抽象类  
abstract class branch extends tree{  
    abstract function combination(tree $item);  
    abstract function separation(tree $item);  
}  
  
class createLeaf extends tree{  
      
    private $name;  
    private $size;  
    private $color;  
    private $leaf=array();  
      
    public function __construct($name,$size,$color){  
        $this->name=$name;  
        $this->size=$size;  
        $this->color=$color;  
    }  
      
    public function create(){  
        $this->leaf[$this->name]=array(  
            'size'=>$this->size,  
            'color'=>$this->color  
        );  
        return $this->leaf;  
    }  
      
    public function combination(tree $item){  
        throw new Exception("本类不支持组合操作");  
    }  
      
    public function separation(tree $item){  
        throw new Exception("本类不支持分离操作");  
    }  
      
}  
  
class createBranch extends branch{  
  
    private $name;  
    private $branch=array();  
    private $items=array();  
      
    public function __construct($name){  
        $this->name=$name;  
    }  
      
    public function create(){  
        foreach($this->items as $item){  
            $arr=$item->create();  
            $this->branch[$this->name][]=$arr;  
        }  
        if(empty($this->branch)){  
            $this->branch[$this->name]=array();  
        }  
        return $this->branch;  
    }  
      
    public function combination(tree $item){  
        $this->items[]=$item;  
    }  
      
    public function separation(tree $item){  
        $key=array_search($item,$this->items);  
        if($key!==false){  
            unset($this->items[$key]);  
        }  
    }  
  
}  
  
$leaf_1=new createLeaf('大红树叶','大','红');  
$leaf_2=new createLeaf('大绿树叶','大','绿');  
$leaf_3=new createLeaf('大黄树叶','大','黄');  
  
$leaf_4=new createLeaf('小红树叶','小','红');  
$leaf_5=new createLeaf('小绿树叶','小','绿');  
$leaf_6=new createLeaf('小黄树叶','小','黄');  
  
$branch_1=new createBranch('树枝1号');  
$branch_1->combination($leaf_1);  
$branch_1->combination($leaf_2);  
$branch_1->combination($leaf_3);  
  
$branch_2=new createBranch('树枝2号');  
$branch_2->combination($leaf_4);  
$branch_2->combination($leaf_5);  
$branch_2->combination($leaf_6);  
  
$branch=new createBranch('树干');  
$branch->combination($branch_1);  
$branch->combination($branch_2);  
  
print_r($branch->create());

這樣,我們總算是漂亮的完成了這個需求。但必須注意的是,由於組合模式的彈性,許多人喜歡不假思索的使用組合類別。事實上,組合類別存在著「過於靈活」、「開銷大」的缺陷。我們試想一下,一個元素或組合在整個系統中可能被調用非常多次,但一旦某個元素或組合在系統中的一個節點出現問題,我們將很難排查到那個節點。

再試想一下,若是系統中的某個元素是一條查詢資料庫的sql語句,而且這條sql語句的開銷有些大,一旦它被組合到整個系統的每一個角落,運行系統造成的結果將是災難性的。

相關推薦:

PHP設計模式之 組合模式

PHP設計模式之組合模式與案例分享

PHP訪客模式與組合模式詳解

#

以上是PHP組合模式詳解及案例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
解釋負載平衡如何影響會話管理以及如何解決。解釋負載平衡如何影響會話管理以及如何解決。Apr 29, 2025 am 12:42 AM

負載均衡會影響會話管理,但可以通過會話複製、會話粘性和集中式會話存儲解決。 1.會話複製在服務器間複製會話數據。 2.會話粘性將用戶請求定向到同一服務器。 3.集中式會話存儲使用獨立服務器如Redis存儲會話數據,確保數據共享。

說明會話鎖定的概念。說明會話鎖定的概念。Apr 29, 2025 am 12:39 AM

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

有其他PHP會議的選擇嗎?有其他PHP會議的選擇嗎?Apr 29, 2025 am 12:36 AM

PHP會話的替代方案包括Cookies、Token-basedAuthentication、Database-basedSessions和Redis/Memcached。 1.Cookies通過在客戶端存儲數據來管理會話,簡單但安全性低。 2.Token-basedAuthentication使用令牌驗證用戶,安全性高但需額外邏輯。 3.Database-basedSessions將數據存儲在數據庫中,擴展性好但可能影響性能。 4.Redis/Memcached使用分佈式緩存提高性能和擴展性,但需額外配

在PHP的上下文中定義'會話劫持”一詞。在PHP的上下文中定義'會話劫持”一詞。Apr 29, 2025 am 12:33 AM

Sessionhijacking是指攻擊者通過獲取用戶的sessionID來冒充用戶。防範方法包括:1)使用HTTPS加密通信;2)驗證sessionID的來源;3)使用安全的sessionID生成算法;4)定期更新sessionID。

PHP的完整形式是什麼?PHP的完整形式是什麼?Apr 28, 2025 pm 04:58 PM

文章討論了PHP,詳細介紹了其完整形式,在We​​b開發中的主要用途,與Python和Java的比較以及對初學者的學習便利性。

PHP如何處理形式數據?PHP如何處理形式數據?Apr 28, 2025 pm 04:57 PM

PHP使用$ \ _ post和$ \ _獲取超級全局的php處理數據,並通過驗證,消毒和安全數據庫交互確保安全性。

PHP和ASP.NET有什麼區別?PHP和ASP.NET有什麼區別?Apr 28, 2025 pm 04:56 PM

本文比較了PHP和ASP.NET,重點是它們對大規模Web應用程序,性能差異和安全功能的適用性。兩者對於大型項目都是可行的,但是PHP是開源和無關的,而ASP.NET,

PHP是對病例敏感的語言嗎?PHP是對病例敏感的語言嗎?Apr 28, 2025 pm 04:55 PM

PHP的情況敏感性各不相同:功能不敏感,而變量和類是敏感的。最佳實踐包括一致的命名和使用對案例不敏感的功能進行比較。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具