[php]php设计模式 Composite (组合模式)
1 <?php
2 /**
3 * 组合模式
4 *
5 * 将对象组合成树形结构以表示"部分-整体"的层次结构,使得客户对单个对象和复合对象的使用具有一致性
6 */
7 abstractclass MenuComponent
8 {
9 publicfunction add($component){}
10 publicfunction remove($component){}
11 publicfunction getName(){}
12 publicfunction getUrl(){}
13 publicfunction display(){}
14 }
15
16 class Menu extends MenuComponent
17 {
18 private$_items=array();
19 private$_name=null;
20
21 publicfunction __construct($name)
22 {
23 $this->_name =$name;
24 }
25
26 publicfunction add($component)
27 {
28 $this->_items[] =$component;
29 }
30
31 publicfunction remove($component)
32 {
33 $key=array_search($component,$this->_items);
34 if($key!==false) unset($this->_items[$key]);
35 }
36
37 publicfunction display()
38 {
39 echo"-- ".$this->_name." ---------<br/>";
40 foreach($this->_items as$item)
41 {
42 $item->display();
43 }
44 }
45 }
46
47 class Item extends MenuComponent
48 {
49 private$_name=null;
50 private$_url=null;
51
52 publicfunction __construct($name,$url)
53 {
54 $this->_name =$name;
55 $this->_url =$url;
56 }
57
58 publicfunction display()
59 {
60 echo$this->_name."#".$this->_url."<br/>";
61 }
62 }
63
64 class Client
65 {
66 private$_menu=null;
67
68 publicfunction __construct($menu)
69 {
70 $this->_menu =$menu;
71 }
72
73 publicfunction setMenu($menu)
74 {
75 $this->_menu =$menu;
76 }
77
78 publicfunction displayMenu()
79 {
80 $this->_menu->display();
81 }
82 }
83
84 // 实例一下
85 // 创建menu
86 $subMenu1=new Menu("sub menu1");
87 $subMenu2=new Menu("sub menu2");
88 $subMenu3=new Menu("sub menu3");
89
90 $item1=new Item("163","www.163.com");
91 $item2=new Item("sina","www.sina.com");
92
93 $subMenu1->add($item1);
94 $subMenu1->add($item2);
95
96 $item3=new Item("baidu","www.baidu.com");
97 $item4=new Item("google","www.google.com");
98 $subMenu2->add($item3);
99 $subMenu2->add($item4);
100
101 $allMenu=new Menu("All Menu");
102 $allMenu->add($subMenu1);
103 $allMenu->add($subMenu2);
104 $allMenu->add($subMenu3);
105
106 $objClient=new Client($allMenu);
107 $objClient->displayMenu();
108
109 $objClient->setMenu($subMenu2);
110 $objClient->displayMenu();
2 /**
3 * 组合模式
4 *
5 * 将对象组合成树形结构以表示"部分-整体"的层次结构,使得客户对单个对象和复合对象的使用具有一致性
6 */
7 abstractclass MenuComponent
8 {
9 publicfunction add($component){}
10 publicfunction remove($component){}
11 publicfunction getName(){}
12 publicfunction getUrl(){}
13 publicfunction display(){}
14 }
15
16 class Menu extends MenuComponent
17 {
18 private$_items=array();
19 private$_name=null;
20
21 publicfunction __construct($name)
22 {
23 $this->_name =$name;
24 }
25
26 publicfunction add($component)
27 {
28 $this->_items[] =$component;
29 }
30
31 publicfunction remove($component)
32 {
33 $key=array_search($component,$this->_items);
34 if($key!==false) unset($this->_items[$key]);
35 }
36
37 publicfunction display()
38 {
39 echo"-- ".$this->_name." ---------<br/>";
40 foreach($this->_items as$item)
41 {
42 $item->display();
43 }
44 }
45 }
46
47 class Item extends MenuComponent
48 {
49 private$_name=null;
50 private$_url=null;
51
52 publicfunction __construct($name,$url)
53 {
54 $this->_name =$name;
55 $this->_url =$url;
56 }
57
58 publicfunction display()
59 {
60 echo$this->_name."#".$this->_url."<br/>";
61 }
62 }
63
64 class Client
65 {
66 private$_menu=null;
67
68 publicfunction __construct($menu)
69 {
70 $this->_menu =$menu;
71 }
72
73 publicfunction setMenu($menu)
74 {
75 $this->_menu =$menu;
76 }
77
78 publicfunction displayMenu()
79 {
80 $this->_menu->display();
81 }
82 }
83
84 // 实例一下
85 // 创建menu
86 $subMenu1=new Menu("sub menu1");
87 $subMenu2=new Menu("sub menu2");
88 $subMenu3=new Menu("sub menu3");
89
90 $item1=new Item("163","www.163.com");
91 $item2=new Item("sina","www.sina.com");
92
93 $subMenu1->add($item1);
94 $subMenu1->add($item2);
95
96 $item3=new Item("baidu","www.baidu.com");
97 $item4=new Item("google","www.google.com");
98 $subMenu2->add($item3);
99 $subMenu2->add($item4);
100
101 $allMenu=new Menu("All Menu");
102 $allMenu->add($subMenu1);
103 $allMenu->add($subMenu2);
104 $allMenu->add($subMenu3);
105
106 $objClient=new Client($allMenu);
107 $objClient->displayMenu();
108
109 $objClient->setMenu($subMenu2);
110 $objClient->displayMenu();
think in coding