分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

建造者模式 - 设计模式 - PHP版

发布时间:2023-09-06 01:20责任编辑:董明明关键词:PHP
 1 <?php 2 /* 3 ?* 建造者模式 4 ?* ?5 ?* 参考:http://blog.csdn.net/jhq0113/article/details/45268743 6 ?* ?7 ?*/ 8 /* * 具体产品角色 ?鸟类 ?9 ?* Class Bird 10 ?*/11 class Bird {12 ????public $_head;13 ????public $_wing;14 ????public $_foot;15 ????function show() {16 ????????echo "头的颜色:{$this->_head}<br/>";17 ????????echo "翅膀的颜色:{$this->_wing}<br/>";18 ????????echo "脚的颜色:{$this->_foot}<br/>";19 ????}20 }21 /* * 抽象鸟的建造者(生成器) 22 ?* Class BirdBuilder 23 ?*/24 abstract class BirdBuilder {25 ????protected $_bird;26 ????function __construct() {27 ????????$this->_bird = new Bird();28 ????}29 ????abstract function BuildHead();30 ????abstract function BuildWing();31 ????abstract function BuildFoot();32 ????abstract function GetBird();33 }34 /* * 具体鸟的建造者(生成器) ??蓝鸟 35 ?* Class BlueBird 36 ?*/37 class BlueBird extends BirdBuilder {38 ????function BuildHead() {39 ????????// TODO: Implement BuilderHead() method. ?40 ????????$this->_bird->_head = "Blue";41 ????}42 ????function BuildWing() {43 ????????// TODO: Implement BuilderWing() method. ?44 ????????$this->_bird->_wing = "Blue";45 ????}46 ????function BuildFoot() {47 ????????// TODO: Implement BuilderFoot() method. ?48 ????????$this->_bird->_foot = "Blue";49 ????}50 ????function GetBird() {51 ????????// TODO: Implement GetBird() method. ?52 ????????return $this->_bird;53 ????}54 }55 /* * 玫瑰鸟 56 ?* Class RoseBird 57 ?*/58 class RoseBird extends BirdBuilder {59 ????function BuildHead() {60 ????????// TODO: Implement BuildHead() method. ?61 ????????$this->_bird->_head = "Red";62 ????}63 ????function BuildWing() {64 ????????// TODO: Implement BuildWing() method. ?65 ????????$this->_bird->_wing = "Black";66 ????}67 ????function BuildFoot() {68 ????????// TODO: Implement BuildFoot() method. ?69 ????????$this->_bird->_foot = "Green";70 ????}71 ????function GetBird() {72 ????????// TODO: Implement GetBird() method. ?73 ????????return $this->_bird;74 ????}75 }76 /* * 指挥者 77 ?* Class Director 78 ?*/79 class Director {80 ????/**81 ?????* @param $_builder ?????建造者 82 ?????* @return mixed ????????产品类:鸟 83 ?????*/84 ????function Construct($_builder) {85 ????????$_builder->BuildHead();86 ????????$_builder->BuildWing();87 ????????$_builder->BuildFoot();88 ????????return $_builder->GetBird();89 ????}90 }91 $director = new Director();92 echo "蓝鸟的组成:<hr/>";93 $blue_bird = $director->Construct(new BlueBird());94 $blue_bird->Show();95 echo "<br/>Rose鸟的组成:<hr/>";96 $rose_bird = $director->Construct(new RoseBird());97 $rose_bird->Show();

建造者模式 - 设计模式 - PHP版

原文地址:http://www.cnblogs.com/benben7466/p/7736543.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved