<?php //代理模式 代理模式的做用和父類以及接口和組合的做用相似,都是爲了聚合共用部分,減小公共部分的代碼 //程序猿能寫代碼但是不能運行機器碼,這些得交個計算機 //代碼 class Code { private $_code_txt; public function __construct($code_txt){ $this->_code_txt = $code_txt; } public function getCode(){ return $this->_code_txt; } } //程序猿 class Programer { public static function makeCode(){ $code_txt = "import std.stdio;void main(){writeln(\"hello d language programe\");}"; return new Code($code_txt); } } //運行代碼的機器 class CodeRunner { private $_code; public function runCode($code){ $this->_code = $code; $this->debug(); $this->complie(); $this->run(); } public function debug(){ echo $this->_code->getCode()."debug\n"; } public function complie(){ echo $this->_code->getCode()."編譯\n"; } public function run(){ echo $this->_code->getCode()."運行\n"; } } $cr = new CodeRunner(); $cr->runCode(Programer::makeCode());