-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapter.php
More file actions
71 lines (56 loc) · 1.44 KB
/
adapter.php
File metadata and controls
71 lines (56 loc) · 1.44 KB
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
<?php
/*
Adapter pattern:
adapting between classes and objects
real-world example include memory-card reader adapters, poweradapters, etc.
software: 3rd party frameworks to adapt to local environment
legacy code, updated method names that need to be mapped to new version
*/
// these are the third party libraries, no-touching
class FacebookApi {
public function getEmailFromUser() {
print "I am the Facebook Email\n";
}
}
class GoogleApi {
public function getUserEmail() {
print "i am the Google Email\n";
}
}
// end of third-party library
interface ApiAdapter {
public function getUserEmail();
}
class GoogleApiAdapter implements ApiAdapter {
private $googleApi;
public function __construct() {
$this->googleApi = new GoogleApi;
}
public function getUserEmail() {
$this->googleApi->getUserEmail();
}
}
class FacebookApiAdapter implements ApiAdapter {
private $api;
public function __construct() {
$this->api = new FacebookApi;
}
public function getUserEmail() {
$this->api->getEmailFromUser();
}
}
class Adapter {
private $adapter;
public function setAdapter(ApiAdapter $value) {
$this->adapter = $value;
}
public function getUserEmail() {
$this->adapter->getUserEmail();
}
}
$adapter = new Adapter;
$adapter->setAdapter(new FacebookApiAdapter);
$adapter->getUserEmail();
$adapter->setAdapter(new GoogleApiAdapter);
$adapter->getUserEmail();
?>