-
Notifications
You must be signed in to change notification settings - Fork 4
/
proxy.php
53 lines (45 loc) · 1.08 KB
/
proxy.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
45
46
47
48
49
50
51
52
53
<?php
/**
* Proxy pattern example
*
* @author Christian Bergau <[email protected]>
* @copyright Free for all
* @link http://en.wikipedia.org/wiki/Proxy_pattern
*/
interface ProductInterface
{
public function action();
}
class Product implements ProductInterface
{
protected $actionCalls = 0;
public function action()
{
$this->actionCalls++;
}
public function getActionCalls()
{
return $this->actionCalls;
}
}
class ProductProxy implements ProductInterface
{
protected $product;
const ACTION_CALL_LIMITS = 3;
public function __construct(ProductInterface $product)
{
$this->product = $product;
}
public function action()
{
if ($this->product->getActionCalls() >= self::ACTION_CALL_LIMITS) {
throw new Exception('Can not call action more than ' . self::ACTION_CALL_LIMITS);
}
$this->product->action();
}
}
$product = new ProductProxy(new Product());
$product->action();
$product->action();
$product->action();
$product->action(); // Exception will be thrown here