Skip to content

Commit 3a74e9f

Browse files
committed
Adapter
1 parent c882d8d commit 3a74e9f

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

Adapter/index.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ See details on http://maxsite.org/page/php-patterns
1212
* "Composite" (Structural)
1313
* "Builder" (Creational)
1414
* "Strategy" (Behavioral)
15+
* "Adapter" (Structural)
1516

1617

1718
(c) MaxSite.org, 2019, http://maxsite.org/

0 commit comments

Comments
 (0)