這個模式理解起來會有些歧義,特別是某些書上面那些難懂的闡述。先來說說組合模式的幾個特點:
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中文網(www.php.cn)!

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP不是在消亡,而是在不斷適應和進化。 1)PHP從1994年起經歷多次版本迭代,適應新技術趨勢。 2)目前廣泛應用於電子商務、內容管理系統等領域。 3)PHP8引入JIT編譯器等功能,提升性能和現代化。 4)使用OPcache和遵循PSR-12標準可優化性能和代碼質量。

PHP的未來將通過適應新技術趨勢和引入創新特性來實現:1)適應云計算、容器化和微服務架構,支持Docker和Kubernetes;2)引入JIT編譯器和枚舉類型,提升性能和數據處理效率;3)持續優化性能和推廣最佳實踐。

在PHP中,trait適用於需要方法復用但不適合使用繼承的情況。 1)trait允許在類中復用方法,避免多重繼承複雜性。 2)使用trait時需注意方法衝突,可通過insteadof和as關鍵字解決。 3)應避免過度使用trait,保持其單一職責,以優化性能和提高代碼可維護性。

依賴注入容器(DIC)是一種管理和提供對象依賴關係的工具,用於PHP項目中。 DIC的主要好處包括:1.解耦,使組件獨立,代碼易維護和測試;2.靈活性,易替換或修改依賴關係;3.可測試性,方便注入mock對象進行單元測試。

SplFixedArray在PHP中是一種固定大小的數組,適用於需要高性能和低內存使用量的場景。 1)它在創建時需指定大小,避免動態調整帶來的開銷。 2)基於C語言數組,直接操作內存,訪問速度快。 3)適合大規模數據處理和內存敏感環境,但需謹慎使用,因其大小固定。

PHP通過$\_FILES變量處理文件上傳,確保安全性的方法包括:1.檢查上傳錯誤,2.驗證文件類型和大小,3.防止文件覆蓋,4.移動文件到永久存儲位置。

JavaScript中處理空值可以使用NullCoalescingOperator(??)和NullCoalescingAssignmentOperator(??=)。 1.??返回第一個非null或非undefined的操作數。 2.??=將變量賦值為右操作數的值,但前提是該變量為null或undefined。這些操作符簡化了代碼邏輯,提高了可讀性和性能。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

Dreamweaver Mac版
視覺化網頁開發工具

記事本++7.3.1
好用且免費的程式碼編輯器