FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Design-patterns/visitor.php at master · LaughingMeMe/Design-patterns · GitHub
LaughingMeMe
/
Design-patterns
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
Design-patterns
/
visitor.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
94 lines (78 loc) · 2.23 KB
Breadcrumbs
Design-patterns
/
visitor.php
Copy path
File metadata and controls
94 lines (78 loc) · 2.23 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
//访问者模式
//行为型:访问者表示一个作用于某对象结构中各元素的操作,它可以在不修改各元素类的情况下,定义作用于这些元素的新操作,即动态的添加访问者角色
interface
visitor{
public
function
visitConcreteElementA
(
ConcreteElementA
$
concreteElementA
);
public
function
visitConcreteElementB
(
ConcreteElementB
$
concreteElementB
);
}
interface
Element{
public
function
accept
(
visitor
$
visitor
);
}
class
ConcreteElementA
implements
Element{
public
$
name
;
public
function
__construct
(
$
name
){
$
this
->
name
=
$
name
;
}
public
function
accept
(
visitor
$
visitor
){
$
visitor
->
visitConcreteElementA
(
$
this
);
}
}
class
ConcreteElementB
implements
Element{
public
$
name
;
public
function
__construct
(
$
name
){
$
this
->
name
=
$
name
;
}
public
function
accept
(
visitor
$
visitor
){
$
visitor
->
visitConcreteElementB
(
$
this
);
}
}
class
visitorA
implements
visitor{
public
function
visitConcreteElementA
(
ConcreteElementA
$
concreteElementA
){
echo
'
1
'
;
}
public
function
visitConcreteElementB
(
ConcreteElementB
$
concreteElementB
){
echo
'
2
'
;
}
}
class
visitorB
implements
visitor{
public
function
visitConcreteElementA
(
ConcreteElementA
$
concreteElementA
){
echo
'
3
'
;
}
public
function
visitConcreteElementB
(
ConcreteElementB
$
concreteElementB
){
echo
'
4
'
;
}
}
class
ObjectStructure{
private
$
_elements
=
array
();
public
function
__construct
(){
}
public
function
attach
(
$
element
){
array_push
(
$
this
->
_elements
,
$
element
);
}
public
function
detach
(
$
element
){
$
index
=
array_search
(
$
element
,
$
this
->
_elements
);
if
(
false
!==
$
index
){
unset(
$
this
->
_elements
[
$
index
]);
return
true
;
}
return
false
;
}
public
function
accept
(
$
visitor
){
foreach
(
$
this
->
_elements
as
$
key
=>
$
value
) {
$
value
->
accept
(
$
visitor
);
}
}
}
$
structure
=
new
ObjectStructure
();
$
concreteElementA
=
new
ConcreteElementA
(
'
A
'
);
$
concreteElementB
=
new
ConcreteElementB
(
'
B
'
);
$
concreteElementB2
=
new
ConcreteElementB
(
'
B2
'
);
$
visitorA
=
new
visitorA
();
$
visitorB
=
new
visitorB
();
$
structure
->
attach
(
$
concreteElementA
);
$
structure
->
attach
(
$
concreteElementB
);
$
structure
->
attach
(
$
concreteElementB2
);
$
structure
->
detach
(
$
concreteElementB2
);
$
structure
->
accept
(
$
visitorA
);
$
structure
->
accept
(
$
visitorB
);
?>
Back
|
FazBrowse Home
|
New Git URL