UML图
定义:装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。
使用场景:当一个对象可以根据选择动态的加上其他功能,动态的意思是可变的和不确定的。比如:购买汽车系统,我们可以给不同的汽车型号加上不同的功能,比如:GPS功能,雷达功能。也可以不加,这个时候哪个型号加跟不加这些功能是动态的,是根据需要来决定的。但是每加一个功能是需要增加价钱的。这里是有两个纬度是不定的,型号和功能。这个就可以考虑使用装饰模式。
具体代码实现:
//汽车型号接口类 Car.php
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/3/23
* Time: 9:42
*/
abstract class Car
{
protected $cost;
protected $description;
public abstract function getCost();
public abstract function getDescription();
}
//A1型号具体实现类 A1Car.php
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/3/23
* Time: 10:00
*/
class A1Car extends Car
{
public $cost = 100000;
public $description = 'Its A1';
public function getCost()
{
return $this->cost;
}
public function getDescription()
{
return $this->description;
}
}
//A4型号具体实现类 A4Car.php
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/3/23
* Time: 10:00
*/
class A4Car extends Car
{
public $cost = 120000;
public $description = 'Its A2';
public function getCost()
{
return $this->cost;
}
public function getDescription()
{
return $this->description;
}
}
//功能装饰接口类 Decoration.php
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/3/23
* Time: 10:07
*/
class Decoration extends Car
{
protected $car;
protected $decorationCost;
protected $decorationDescription;
protected $cost;
protected $description;
public function __construct(Car $car)
{
$this->car = $car;
$this->cost = $car->getCost();
$this->description = $car->getDescription();
}
public function getCost()
{
return $this->cost + $this->decorationCost;
}
public function getDescription()
{
return $this->description . $this->decorationDescription;
}
}
//GPS功能具体实现类 GPS.php
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/3/23
* Time: 10:14
*/
class GPS extends Decoration
{
protected $decorationCost;
protected $decorationDescription;
public function __construct(Car $car)
{
$this->decorationCost = 4000;
$this->decorationDescription = "--GPS";
parent::__construct($car);
}
}
//雷达功能具体实现类 Radar.php
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/3/23
* Time: 10:14
*/
class Radar extends Decoration
{
protected $decorationCost;
protected $decorationDescription;
public function __construct(Car $car)
{
$this->decorationCost = 5000;
$this->decorationDescription = "--Radar";
parent::__construct($car);
}
}
//入口文件类 Main.php
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/3/23
* Time: 10:16
*/
require_once 'AutoLoader.php';
spl_autoload_register('AutoLoader::AutoLoadClass');
$car = new A1Car();
$gps = new GPS($car);
echo $gps->getCost()."\n";
echo $gps->getDescription()."\n";
//echo "<pre>";
//var_dump($gps);
$gpsRadar = new Radar($gps);
//var_dump($gpsRadar);
echo $gpsRadar->getCost()."\n";
echo $gpsRadar->getDescription()."\n";
//类自动加载类 AutoLoader.php
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/3/23
* Time: 10:19
*/
class AutoLoader
{
public static $classMap = [];
public static function AutoLoadClass($class)
{
if(isset(self::$classMap[$class])) {
return true;
}
$file = $class.'.php';
if(file_exists($file)) {
include $file;
self::$classMap[$class] = $class;
}
}
}