[php]php设计模式 Visitor (访问者模式)
1 <?php
2 /**
3 * 访问者模式
4 *
5 * 表示一个作用于某对象结构中的各元素的操作,可以在不改变各元素的类的前提下定义作用于这些元素的新操作
6 *
7 */
8 abstractclass Visitor
9 {
10 abstractpublicfunction visitCroncreteElementA($element);
11 abstractpublicfunction visitCroncreteElementB($element);
12 }
13
14 class ConcreteVisitor1 extends Visitor
15 {
16 publicfunction visitCroncreteElementA($element)
17 {
18 echoget_class($element)." visit 1A<br/>";
19 }
20
21 publicfunction visitCroncreteElementB($element)
22 {
23 echoget_class($element)." visit 1B<br/>";
24 }
25 }
26
27 class ConcreteVisitor2 extends Visitor
28 {
29 publicfunction visitCroncreteElementA($element)
30 {
31 echoget_class($element)." visit 2A<br/>";
32 }
33
34 publicfunction visitCroncreteElementB($element)
35 {
36 echoget_class($element)." visit 2B<br/>";
37 }
38 }
39
40 abstractclass Element
41 {
42 abstractpublicfunction accept($visitor);
43 }
44
45 class ConcreteElementA extends Element
46 {
47 publicfunction accept($visitor)
48 {
49 $visitor->visitCroncreteElementA($this);
50 }
51 }
52
53 class ConcreteElementB extends Element
54 {
55 publicfunction accept($visitor)
56 {
57 $visitor->visitCroncreteElementB($this);
58 }
59 }
60
61 class ObjectStructure
62 {
63 private$_elements=array();
64
65 publicfunction attach($element)
66 {
67 $this->_elements[] =$element;
68 }
69
70 publicfunction detach($element)
71 {
72 if($key=array_search($element,$this->_elements) !==false) unset($this->_elements[$key]);
73 }
74
75 publicfunction accept($visitor)
76 {
77 foreach($this->_elements as$element)
78 {
79 $element->accept($visitor);
80 }
81 }
82 }
83
84 //
85 $objOS=new ObjectStructure();
86 $objOS->attach(new ConcreteElementA());
87 $objOS->attach(new ConcreteElementB());
88
89 $objCV1=new ConcreteVisitor1();
90 $objCV2=new ConcreteVisitor2();
91
92 $objOS->accept($objCV1);
93 $objOS->accept($objCV2);
2 /**
3 * 访问者模式
4 *
5 * 表示一个作用于某对象结构中的各元素的操作,可以在不改变各元素的类的前提下定义作用于这些元素的新操作
6 *
7 */
8 abstractclass Visitor
9 {
10 abstractpublicfunction visitCroncreteElementA($element);
11 abstractpublicfunction visitCroncreteElementB($element);
12 }
13
14 class ConcreteVisitor1 extends Visitor
15 {
16 publicfunction visitCroncreteElementA($element)
17 {
18 echoget_class($element)." visit 1A<br/>";
19 }
20
21 publicfunction visitCroncreteElementB($element)
22 {
23 echoget_class($element)." visit 1B<br/>";
24 }
25 }
26
27 class ConcreteVisitor2 extends Visitor
28 {
29 publicfunction visitCroncreteElementA($element)
30 {
31 echoget_class($element)." visit 2A<br/>";
32 }
33
34 publicfunction visitCroncreteElementB($element)
35 {
36 echoget_class($element)." visit 2B<br/>";
37 }
38 }
39
40 abstractclass Element
41 {
42 abstractpublicfunction accept($visitor);
43 }
44
45 class ConcreteElementA extends Element
46 {
47 publicfunction accept($visitor)
48 {
49 $visitor->visitCroncreteElementA($this);
50 }
51 }
52
53 class ConcreteElementB extends Element
54 {
55 publicfunction accept($visitor)
56 {
57 $visitor->visitCroncreteElementB($this);
58 }
59 }
60
61 class ObjectStructure
62 {
63 private$_elements=array();
64
65 publicfunction attach($element)
66 {
67 $this->_elements[] =$element;
68 }
69
70 publicfunction detach($element)
71 {
72 if($key=array_search($element,$this->_elements) !==false) unset($this->_elements[$key]);
73 }
74
75 publicfunction accept($visitor)
76 {
77 foreach($this->_elements as$element)
78 {
79 $element->accept($visitor);
80 }
81 }
82 }
83
84 //
85 $objOS=new ObjectStructure();
86 $objOS->attach(new ConcreteElementA());
87 $objOS->attach(new ConcreteElementB());
88
89 $objCV1=new ConcreteVisitor1();
90 $objCV2=new ConcreteVisitor2();
91
92 $objOS->accept($objCV1);
93 $objOS->accept($objCV2);
think in coding