1
+ <?php
2
+
3
+ /**
4
+ * Design pattern "Bridge" (Structural)
5
+ * This is demo code
6
+ * See for details: http://maxsite.org/page/php-patterns
7
+ */
8
+
9
+ /**
10
+ * Available methods
11
+ */
12
+ interface BridgeInterface
13
+ {
14
+ public function method1 ();
15
+ public function method2 ();
16
+ }
17
+
18
+ /**
19
+ * implementation first
20
+ */
21
+ class Bridge1 implements BridgeInterface
22
+ {
23
+ public function method1 ()
24
+ {
25
+ echo 'Bridge1 method1 <br> ' ;
26
+ }
27
+
28
+ public function method2 ()
29
+ {
30
+ echo 'Bridge1 method2 <br> ' ;
31
+ }
32
+ }
33
+
34
+ /**
35
+ * implementation second
36
+ */
37
+ class Bridge2 implements BridgeInterface
38
+ {
39
+ public function method1 ()
40
+ {
41
+ echo 'Bridge2 method1 <br> ' ;
42
+ }
43
+
44
+ public function method2 ()
45
+ {
46
+ echo 'Bridge2 method2 <br> ' ;
47
+ }
48
+ }
49
+
50
+ /**
51
+ * main application Abstract
52
+ */
53
+ abstract class AppAbstract
54
+ {
55
+ protected $ bridge ;
56
+
57
+ public function __construct (BridgeInterface $ bridge )
58
+ {
59
+ $ this ->bridge = $ bridge ;
60
+ }
61
+
62
+ public function method1 ()
63
+ {
64
+ $ this ->bridge ->method1 ();
65
+ }
66
+
67
+ public function method2 ()
68
+ {
69
+ $ this ->bridge ->method2 ();
70
+ }
71
+ }
72
+
73
+ /**
74
+ * main application
75
+ */
76
+ class App extends AppAbstract
77
+ {
78
+ public function run ()
79
+ {
80
+ $ this ->method1 ();
81
+ $ this ->method2 ();
82
+ }
83
+ }
84
+
85
+ /**
86
+ * demo
87
+ */
88
+
89
+ $ a = new App (new Bridge1 ()); // application for Bridge1
90
+ $ a ->run ();
91
+ /*
92
+ Bridge1 method1
93
+ Bridge1 method2
94
+ */
95
+
96
+ $ b = new App (new Bridge2 ()); // application for Bridge2
97
+ $ b ->run ();
98
+ /*
99
+ Bridge2 method1
100
+ Bridge2 method2
101
+ */
102
+
103
+
104
+ # end of file
0 commit comments