具有嵌套类的装饰器模式

时间:2016-05-13 19:00:03

标签: php decorator

我有类的结构

class engine {
    function show() {
        return "some html code";
    }
}

class car {
    private $engine;
    function __construct() {
        //some logic
        $this->engine = new engine();
    }

    function show() {
        $code = "****";
        $code .= $this->engine->show();
        return $code;
    }
}

每个类都有html,svg,txt的输出。我尝试使用装饰模式来制作htmlCar,htmlEngine,svgCar,svgEngine并添加更多输出而无需编辑引擎和汽车类。但是如何在不改变$ this-> engine = new engine()的情况下做到这一点; to $ this-> engine = new htmlEngine(); ?

0 个答案:

没有答案