标签:
<?php
abstract class IComponent
{
protected $site;
abstract public function getSite();
abstract public function getPrice();
}
<?php
abstract class Decorator extends IComponent
{
/*
任务是维护Component的引用
继承getSite()和getPrice()
因为仍然是抽象类,所以不需要实现父类任何一个抽象方法
*/
}
<?php
class BasicSite extends IComponent
{
public function __construct()
{
$this->site = "Basic Site";
}
public function getSite()
{
return $this->site;
}
public function getPrice()
{
return 1200;
}
}
<?php
class Maintenance extends Decorator
{
public function __construct(IComponent $siteNow)
{
$this->site = $siteNow;
}
public function getSite()
{
$format = "<br /> Maintenance";
return $this->site->getSite() . $format;
}
public function getPrice()
{
return 950 + $this->site->getPrice();
}
}
<?php
class Video extends Decorator
{
public function __construct(IComponent $siteNow)
{
$this->site = $siteNow;
}
public function getSite()
{
$format = "<br /> Video";
return $this->site->getSite() . $format;
}
public function getPrice()
{
return 350 + $this->site->getPrice();
}
}
<?php
class DataBase extends Decorator
{
public function __construct(IComponent $siteNow)
{
$this->site = $siteNow;
}
public function getSite()
{
$format = "<br /> DataBase";
return $this->site->getSite() . $format;
}
public function getPrice()
{
return 800 + $this->site->getPrice();
}
}
<?php
function __autoload($class_name)
{
include $class_name . ‘.php‘;
}
class Client
{
private $basicSite;
public function __construct()
{
$this->basicSite = new BasicSite();
$this->basicSite = $this->WrapComponent($this->basicSite);
$siteShow = $this->basicSite->getSite();
$format = "<br /> <strong>Total= $";
$price = $this->basicSite->getPrice();
echo $siteShow . $format . $price . "</strong>";
}
private function WrapComponent(IComponent $component)
{
$component = new Maintenance($component);
$component = new Video($component);
$component = new DataBase($component);
return $component;
}
}
$worker = new Client();
Basic Site Maintenance Video DataBase Total= $3300
$component = new Maintenance($component);
标签:
原文地址:http://www.cnblogs.com/chenqionghe/p/4782222.html