标签:
建造者模式:将一个复杂的对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
建造者模式结构图:
php实现:
<?php class Product{ public $part1; public $part2; public function setPart1($v){ $this->part1 = $v; } public function setPart2($v){ $this->part2 = $v; } } abstract class Builder{ public abstract function BuildPart1($v); public abstract function BuildPart2($v); } class concreteBuilder extends Builder{ private $product; public function __construct(){ $this->product = new Product(); } function BuildPart1($v1){ $this->product->setPart1($v1); } function BuildPart2($v2){ $this->product->setPart2($v2);; } function getProduct(){ return $this->product; } } class Director{ private $builde; public function __construct(){ $this->builder = new concreteBuilder(); } function getProductA(){ $this->builder->BuildPart1("a"); $this->builder->BuildPart2("aa"); return $this->builder->getProduct(); } function getProductB(){ $this->builder->BuildPart1("b"); $this->builder->BuildPart2("bb"); return $this->builder->getProduct(); } } class Client{ function run(){ $director = new Director(); $productA = $director->getProductA(); echo $productA->part1; } } Client::run();
标签:
原文地址:http://www.cnblogs.com/zhutianpeng/p/4238648.html