标签:mp3 new 建造者 职责 his wav str 简单 接口
通过在必需的逻辑和方法的集合前创建简单的外观接口,来隐藏调用对象的复杂性.
class A { public function song($songList) { foreach($songList as $val) { echo $val . ‘.mp3‘; } } } class B { public function song($songList) { foreach($songList as $val) { echo $val . ‘.wav‘; } } } class Facade { private $a; private $b; public function create() { $this->a = new A(); $this->B = new B(); } public function method($songList) { $this->a->song($songList); $this->b->song($songList); } } 特点: "在必需的逻辑和方法的集合前"也就意味着外观模式调用了不止一个对象.如果是调用一个对象的多个方法,就成了建造者模式了.
定义了处理其他对象的复杂构建的对象设计.
比如如下类: class Person { private $name, $age; public function setName($name){ $this->name = $name; } public function setAge($age) { $this->age = $name; } } 如果我们要使该类"完整",必须调用setName和setAge.为了体现类的单一职责原则,使类看起来清晰明了.这时候我们可以使用建造者模式. class Builder { private $person; private $configs public function __construct($configs) { $this->person = new Person(); $this->configs = $configs; } // 将person的多个方法都放在此方法中执行. public function build() { $this->person->setName($config[‘name‘]); $this->person->setAge($config[‘age‘]); } // 既然是建造者模式,建造完,还是要返回被建造对象,否则也算不上建造者模式. public function getPerson() { return $this->person; } } // 调用 $build = new Build(array(‘name‘ => ‘php‘, ‘age‘ => 10)); $build->build(); $person = $person->getPerson();
标签:mp3 new 建造者 职责 his wav str 简单 接口
原文地址:http://www.cnblogs.com/itfenqing/p/7750572.html