PHP design pattern visitor (Visitor pattern)

Source: Internet
Author: User

Introduction: This is a detailed page of the PHP design mode visitor (visitor mode). It introduces PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 339494 'rolling = 'no'>

1 <? PHP
2 /**
3 * visitor Mode
4 *
5 * indicates an operation that acts on each element in an object structure. You can define a new operation that acts on these elements without changing the classes of each element.
6 *
7 */
8 abstract class visitor
9 {
10 Abstract Public Function visitcroncreteelementa ($ element );
11 Abstract Public Function visitcroncreteelementb ($ element );
12}
13
14 class concretevisitor1 extends visitor
15 {
16 public function visitcroncreteelementa ($ element)
17 {
18 echo get_class ($ element). "Visit 1A <br/> ";
19}
20
21 public function visitcroncreteelementb ($ element)
22 {
23 echo get_class ($ element). "Visit 1B <br/> ";
24}
25}
26
27 class concretevisitor2 extends visitor
28 {
29 public function visitcroncreteelementa ($ element)
30 {
31 echo get_class ($ element). "Visit 2a <br/> ";
32}
33
34 public function visitcroncreteelementb ($ element)
35 {
36 echo get_class ($ element). "Visit 2B <br/> ";
37}
38}
39
40 abstract class Element
41 {
42 Abstract Public Function accept ($ visitor );
43}
44
45 class concreteelementa extends Element
46 {
47 Public Function accept ($ visitor)
48 {
49 $ visitor-> visitcroncreteelementa ($ this );
50}
51}
52
53 class concreteelementb extends Element
54 {
55 public function accept ($ visitor)
56 {
57 $ visitor-> visitcroncreteelementb ($ this );
58}
59}
60
61 class objectstructure
62 {
63 private $ _ elements = array ();
64
65 public function attach ($ element)
66 {
67 $ this-> _ elements [] = $ element;
68}
69
70 public function detach ($ element)
71 {
72 if ($ key = array_search ($ element, $ this-> _ elements )! = False) unset ($ this-> _ elements [$ key]);
73}
74
75 public function 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 );

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/339494.html pageno: 8.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.