PHP设计模式系列 - 建造者模式
- 什么是建造者模式
建造者模式主要是为了消除其它对象复杂的创建过程。
- 设计场景
- 有一个用户的UserInfo类,创建这个类,需要创建用户的姓名,年龄,金钱等信息,才能获得用户具体的信息结果。
- 创建一个UserInfoBuilder 用户建造者类,这个类,将UserInfo复杂的创建姓名,年龄,金钱等操作封装起来,简化用户类的创建过程
- 代码:UserInfo类,创建UserInfo类是复杂的,痛苦的。
//建造者模式,目的是消除其它对象复杂的创建过程
/* 描述一个用户的类,包含用户姓名,年龄,金钱 */
class UserInfo {
protected $userName = '';
protected $userAge = '';
protected $userMoney = '';
public function setUserName($userName) {
$this->userName = $userName;
}
public function setUserAge($userAge) {
$this->userAge = $userAge;
}
public function setUserMoney($userMoney) {
$this->userMoney = $userMoney;
}
public function getPeople() {
echo "这个人的姓名是:" . $this->setUserName . ',年龄是:' . $this->userAge . ', 金钱:' . $this->userMoney;
}
}
/* 实例化,并且创建这个用户的时候,是很痛苦的,需要设置用户名,年龄和金钱*/
$peopleInfo = array(
'userName' => 'initphp',
'userAge' => 28,
'userMoney' => '100元'
);
$UserInfo = new UserInfo;
//下面需要一步步的设置用户信息,才能得到用户详细信息,过程纠结而痛苦
$UserInfo->setUserName($peopleInfo['userName']);
$UserInfo->setUserAge($peopleInfo['userAge']);
$UserInfo->setUserMoney($peopleInfo['userMoney']);
$UserInfo->getPeople();
代码:UserInfoBuilder 用户信息建造者类,将UserInfo的创建过程封装掉,开发者使用起来心情舒畅
<?php
//建造者模式,目的是消除其它对象复杂的创建过程
include("UserInfo.php");
class UserInfoBuilder {
protected $obj; public function __construct() {
$this->obj = new UserInfo;
} public function buildPeople($peopleInfo) {
$this->obj->setUserName($peopleInfo['userName']);
$this->obj->setUserAge($peopleInfo['userAge']);
$this->obj->setUserMoney($peopleInfo['userMoney']);
} public function getPeople() {
$this->obj->getPeople();
}
} /* 创建过程被封装了,用户使用简单了 */
$peopleInfo = array(
'userName' => 'initphp',
'userAge' => 28,
'userMoney' => '100元'
);
$UserInfoBuilder = new UserInfoBuilder;
$UserInfoBuilder->buildPeople($peopleInfo); //直接一个build
$UserInfoBuilder->getPeople();
转自:http://blog.csdn.net/initphp/article/details/7677561
PHP设计模式系列 - 建造者模式的更多相关文章
- C#设计模式(5)——建造者模式(Builder Pattern)
一.引言 在软件系统中,有时需要创建一个复杂对象,并且这个复杂对象由其各部分子对象通过一定的步骤组合而成.例如一个采购系统中,如果需要采购员去采购一批电脑时,在这个实际需求中,电脑就是一个复杂的对象, ...
- C#设计模式之四建造者模式(Builder Pattern)【创建型】
一.引言 今天我们要讲讲Builder模式,也就是建造者模式,当然也有叫生成器模式的,英文名称是Builder Pattern.在现实生活中,我们经常会遇到一些构成比较复杂的物品,比如:电脑,它就是一 ...
- Java设计模式之建造者模式(Builder)
前言: 最近一直在学习okHttp,也对其做了一些整理,okHttp和Retrofit结合大大加速我们的开发效率,源码里面采用了很多设计模式,今天我们来学习一下其中的设计模式之一建造者模式. 建造者模 ...
- 【GOF23设计模式】建造者模式
来源:http://www.bjsxt.com/ 一.[GOF23设计模式]建造者模式详解类图关系 建造飞船 package com.test.Builder; public class AirShi ...
- C++设计模式之建造者模式(三)
4.引入钩子方法的建造者模式 建造者模式除了逐步构建一个复杂产品对象外.还能够通过Director类来更加精细地控制产品的创建过程.比如添加一类称之为钩子方法(HookMethod)的特殊方法来控制是 ...
- 乐在其中设计模式(C#) - 建造者模式(Builder Pattern)
原文:乐在其中设计模式(C#) - 建造者模式(Builder Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 建造者模式(Builder Pattern) 作者:webabc ...
- 折腾Java设计模式之建造者模式
博文原址:折腾Java设计模式之建造者模式 建造者模式 Separate the construction of a complex object from its representation, a ...
- PHP设计模式系列 - 外观模式
外观模式 通过在必需的逻辑和方法的集合前创建简单的外观接口,外观设计模式隐藏了调用对象的复杂性. 外观设计模式和建造者模式非常相似,建造者模式一般是简化对象的调用的复杂性,外观模式一般是简化含有很多逻 ...
- Java 设计模式之建造者模式(四)
原文地址:Java 设计模式之建造者模式(四) 博客地址:http://www.extlight.com 一.前言 今天继续介绍 Java 设计模式中的创建型模式--建造者模式.上篇设计模式的主题为 ...
随机推荐
- JavaScript 正则表达式RegExp 和字符串本身的正则表达式
JavaScript 正则表达式 正则表达式(英语:Regular Expression,在代码中常简写为regex.regexp或RE)使用单个字符串来描述.匹配一系列符合某个句法规则的字符串搜索模 ...
- hdu 1043 八数码问题
Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- Q:链表的中间元素
问题:如何得到一个链表的中间元素? 相信,这个问题对于刚入门编程的人来说,都不会难,最自然而然的想法是先遍历一遍链表,统计链表中的元素的个数,之后,再走一遍链表,所走的步长为链表长度的一半.这样就 ...
- xamarin.Android 选择本地图片、拍摄图片、剪裁图片
[Activity(Theme = "@style/MyStyleBottom")] public class SelectPicPopupWindow : Activity, I ...
- oracel存储过程编写 以及plsql存储过程的debug
1.语法: create or replace procedure messagebackup_createTable //此处存储过程名称不能超过30个字符 as tableName ...
- PHP的new self() 与new static()
参考链接:[PHP中new static()与new self()的区别异同分析],[PHP中new self()和new static()的区别探究],[PHP中static和self的区别] 要点 ...
- flutter Row里面元素居中显示
直接上代码: new Expanded( flex: , child: new Row( children: <Widget>[ Expanded( child: new Containe ...
- 项目maven update 后启动项目出现导常:org.springframework.web.context.ContextLoaderListener
导常:org.springframework.web.context.ContextLoaderListener 1. 右键单击工程项目 ->点击 properties2. 选择 Deploym ...
- linux的作业控制(job control)
引用:http://blog.chinaunix.net/u2/68904/showart_1803789.html 把作业放到后台运行:‘ & ’ 举个简单的例子, 我们要将 /etc/ 整 ...
- hue简单介绍
hue定义: HUE=Hadoop User Experience 个人理解:可视图的webui界面,方便大数据技术之间的CRUD操作. 官方定义:Hue是一个能够与Apache Hadoop交互的W ...