-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdapter.php
44 lines (34 loc) · 948 Bytes
/
Adapter.php
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
<?php
/**
* Usage: If you implement a client that consumes webservices for instance and you want to have an easy way
* to react on changes in the server you can use an adapter pattern. So you can guarantee that your clientcode will never change.
* If Twitter changes a method you can simlle change the method in you adapter instead of changing clientcode
*/
interface ITwitter
{
public function post($msg);
}
class Twitter implements ITwitter
{
//this method will often changge
public function post($msg)
{
echo $msg . PHP_EOL;
}
}
class TwitterAdapter implements ITwitter
{
public $twitter;
public function __construct(Twitter $twitter)
{
$this->twitter = $twitter;
}
public function post($msg)
{
//here you can react on the change
$this->twitter->post($msg);
}
}
$cl = new TwitterAdapter(new Twitter());
//this call will never change
$cl->post("asda");