File tree Expand file tree Collapse file tree 2 files changed +93
-0
lines changed Expand file tree Collapse file tree 2 files changed +93
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ /**
4
+ * Design pattern "Adapter/Wrapper" (Structural)
5
+ * This is demo code
6
+ * See for details: http://maxsite.org/page/php-patterns
7
+ */
8
+
9
+ /**
10
+ * ClassA
11
+ */
12
+ class ClassA
13
+ {
14
+ public function methodA ()
15
+ {
16
+ echo 'ClassA methodA<br> ' ;
17
+ }
18
+ }
19
+
20
+ /**
21
+ * ClassB
22
+ */
23
+ class ClassB
24
+ {
25
+ public function methodB ()
26
+ {
27
+ echo 'ClassB methodB<br> ' ;
28
+ }
29
+ }
30
+
31
+ /**
32
+ * Base Adapter Interface
33
+ */
34
+ interface AdapterInterface
35
+ {
36
+ public function commonMethod ();
37
+ }
38
+
39
+ /**
40
+ * Adapter for ClassA
41
+ */
42
+ class AdapterA implements AdapterInterface
43
+ {
44
+ protected $ class ;
45
+
46
+ public function __construct ()
47
+ {
48
+ $ this ->class = new ClassA ();
49
+ }
50
+
51
+ public function commonMethod ()
52
+ {
53
+ return $ this ->class ->methodA ();
54
+ }
55
+ }
56
+
57
+ /**
58
+ * Adapter for ClassB
59
+ */
60
+ class AdapterB implements AdapterInterface
61
+ {
62
+ protected $ class ;
63
+
64
+ public function __construct ()
65
+ {
66
+ $ this ->class = new ClassB ();
67
+ }
68
+
69
+ public function commonMethod ()
70
+ {
71
+ return $ this ->class ->methodB ();
72
+ }
73
+ }
74
+
75
+ /**
76
+ * demo
77
+ */
78
+
79
+ /**
80
+ * create adapter's
81
+ */
82
+ $ adapter1 = new AdapterA ();
83
+ $ adapter2 = new AdapterB ();
84
+
85
+ /**
86
+ * run
87
+ */
88
+ $ adapter1 ->commonMethod (); // ClassA methodA
89
+ $ adapter2 ->commonMethod (); // ClassB methodB
90
+
91
+
92
+ # end of file
Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ See details on http://maxsite.org/page/php-patterns
12
12
* "Composite" (Structural)
13
13
* "Builder" (Creational)
14
14
* "Strategy" (Behavioral)
15
+ * "Adapter" (Structural)
15
16
16
17
17
18
(c) MaxSite.org, 2019, http://maxsite.org/
You can’t perform that action at this time.
0 commit comments