<?php
//////////////////////////////////////////////////
// License :MIT
//
// Name: Builder for PHP From DesignPattern
// Coding: Penguin-Works
// LastUpdate:2013/06/18
//////////////////////////////////////////////////
// interface
//
interface IFClient {
function SetRequest();
function GetRequest();
}
interface IFDirector {
function Construct($_builder);
}
interface IFBuilder {
function GetProduct();
function BuildPartA();
function BuildPartB();
function BuildPartC();
};
//////////////////////////////////////////////////
// implement
//
//================================================
// Client
//================================================
class Client implements IFClient {
private $request_result_;
//--------------------------------------------
public function SetRequest() {
$_director = new Director;
$_concreate_bulder = new ConcreateBuilder;
$this -> request_result_ = $_director ->Construct($_concreate_bulder);
}
//--------------------------------------------
public function GetRequest() {
return $this -> request_result_;
}
}
//================================================
// Director
//================================================
class Director {
function Construct($_builder) {
if ($_builder instanceof IFBuilder) {
$_builder -> BuildPartA();
$_builder -> BuildPartB();
$_builder -> BuildPartC();
return $_builder -> GetProduct();
}
return null;
}
}
//================================================
// ConcreateBuilder
//================================================
class ConcreateBuilder implements IFBuilder {
private $Product_;
private function AddPart($_part) {
$this->Product_ .= $_part;
}
function GetProduct() {
return $this->Product_;
}
function BuildPartA() {
$this->AddPart('Header<br>');
}
function BuildPartB() {
$this->AddPart('Content<br>');
}
function BuildPartC() {
$this->AddPart('Footer');
}
}
// TestMain
$Client_ = new Client;
$Client_ ->SetRequest();
print $Client_ -> GetRequest();