<?php
* 结构型模式
* 装饰器模式
* 对现有的对象增加功能,动态的将责任附加到对象上。
* 和适配器的区别: 适配器是链接两个接口,装饰器是对现有的对象包装
*/
spl_autoload_register('autoload');
function autoload($class)
{
require dirname($_SERVER['SCRIPT_FILENAME']) . '//..//' . str_replace('\\', '/', $class) . '.php';
}
use decorator\DecoratorPack;
use decorator\ApplePhone;
use decorator\SmartisanPhone;
try {
echo "为加装饰器之前: \n";
$applePhone = new ApplePhone();
$applePhone->product();
$smartisanPhone = new SmartisanPhone();
$smartisanPhone->product();
echo "\n------------\n";
echo "进行包装装饰器: \n";
$decoratorPack1 = new DecoratorPack(new ApplePhone);
$decoratorPack1->pack = '白盒子';
$decoratorPack1->product();
$decoratorPack2 = new DecoratorPack(new SmartisanPhone);
$decoratorPack1->pack = '黑盒子';
$decoratorPack1->product();
} catch (\Exception $e) {
echo $e->getMessage();
}