-
Notifications
You must be signed in to change notification settings - Fork 700
/
test.php
45 lines (37 loc) · 1.08 KB
/
test.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
<?php
/**
* 结构型模式
*
* php装饰器模式
* 对现有的对象增加功能
* 和适配器的区别:适配器是连接两个接口,装饰器是对现有的对象包装
*
* @author TIGERB <https://github.com/TIGERB>
* @example 运行 php test.php
*/
// 注册自加载
spl_autoload_register('autoload');
function autoload($class)
{
require dirname($_SERVER['SCRIPT_FILENAME']) . '//..//' . str_replace('\\', '/', $class) . '.php';
}
/************************************* test *************************************/
use decorator\DecoratorBrand;
use decorator\ShoesSport;
use decorator\ShoesSkateboard;
try {
echo "未加装饰器之前:\n";
// 生产运动鞋
$shoesSport = new ShoesSport();
$shoesSport->product();
echo "\n--------------------\n";
//-----------------------------------
echo "加贴标装饰器:\n";
// 初始化一个贴商标适配器
$DecoratorBrand = new DecoratorBrand(new ShoesSport());
$DecoratorBrand->_brand = 'nike';
// 生产nike牌运动鞋
$DecoratorBrand->product();
} catch (\Exception $e) {
echo $e->getMessage();
}