• --:)欢迎访问锋网源码(:--
  • 首页
  • RSS订阅
  • 常用软件
  • 网页模板
  • 网站运作
  • 锋网学院
  • 繁體中文

  • 学院首页
  • 新闻资讯
  • 网站运营
  • 网站开发
  • 美工设计
  • 数据库类
  • 服务器类
  • 网络应用
  • 操作系统
  • 软件教学
编程开发   认证考试   网络安全   文章搜索: 高级搜索
会员登录/控制面版 您的位置: 学院首页 >> 网站开发 >> PHP教程 >> 文章内容
 

精彩推荐

 
 

本类推荐文章

  • PHP的十个高级技巧
  • PHP找回密码机制流程
  • 解决phpmyadmin 乱码,支持gb..
 
 

本类阅读排行

  • PHP的十个高级技巧
  • PHP找回密码机制流程
  • Windows XP下PHP+MySQL环境搭..
  • PHP链接ACCESS数据库简单的方..
  • 解决phpmyadmin 乱码,支持gb..
  • PHP代码加密解密一法
  • 如何基于discuz开发网站通行..
  • PHP中格式化显示时间
  • 一个简单计数器的源代码
  • 简体中文转换为繁体中文的PH..
  • php命令行脚本多进程并发执行..
  • PHP/MySQL教程 完整教程
  • 一生成柱状图、横柱图、折线..
  • PHP 字符串操作入门教程
  • PHP入门速成
  • 做个自己站内搜索引擎
  • PHP代码:得到客户端的IP
  • 用PHP制作动态计数器
  • 实现统计在线人数/用户列表
  • AJAX在PHP中的简单使用
 
 

php教程:php设计模式介绍之值对象模式

  • 日期:2008-04-11     人气:     出处:phpchina     作者: forest
  • 字体大小:
  • 小
  • 中
  • 大

详细例子:

让我们在一下更加复杂的例子中查看值对象模式的功能。

Webjx.Com

让我们开始实现一个的基于PHP5中Dollar类中的一个Monopoly游戏。

第一个类Monopoly的框架如下:

class Monopoly {
protected $go_amount;
/**
* game constructor
* @return void
*/
public function __construct() {
$this->go_amount = new Dollar(200);
}
/**
* pay a player for passing 揋o?/span>
* @param Player $player the player to pay
* @return void
*/
public function passGo($player) {
$player->collect($this->go_amount);
}
}

目前,Monopoly的功能比较简单。构造器创建一个Dollar类的实例$go_amount,设定为200,实例go_amount常常被passtGo()函数调用,它带着一个player参数,并让对象player的函数collect为player机上200美元.

Webjx.Com

Player类的声明请看下面代码,Monoplay类调用带一个Dollar参数的Player::collect()方法。然后把Dollar的数值加到Player的现金余额上。另外,通过判断Player::getBalance()方法函数返回来的余额,我们可以知道使访问当前Player和Monopoly对象实例是否在工作中。

网页教学网

class Player {
protected $name;
protected $savings;
/**
* constructor
* set name and initial balance
* @param string $name the players name
* @return void
*/
public function __construct($name) {
$this->name = $name;
$this->savings = new Dollar(1500);
}
/**
* receive a payment
* @param Dollar $amount the amount received
* @return void
*/
public function collect($amount) {
$this->savings = $this->savings->add($amount);
}
* return player balance
* @return float
*/
public function getBalance() {
return $this->savings->getAmount();
}
}

上边已经给出了一个Monopoly和Player类,你现在可以根据目前声明的几个类定义进行一些测试了。 网页教学网

MonopolyTestCase的一个测试实例可以像下面这样写:

class MonopolyTestCase extends UnitTestCase {
function TestGame() {
$game = new Monopoly;
$player1 = new Player(‘Jason’);
$this->assertEqual(1500, $player1->getBalance());
$game->passGo($player1);
$this->assertEqual(1700, $player1->getBalance());
$game->passGo($player1);
$this->assertEqual(1900, $player1->getBalance());
}
}

如果你运行MonopolyTestCase这个测试代码,代码的运行是没有问题的。现在可以添加一些新的功能。

[1] [2] [3] [4]
相关文章
  • php教程:经典PHP代码
  • php教程:mysql的常用语句
  • PHP教程:cookie和数组的结合实现购物车
  • php教程:mysql数据库操作的DB类
  • php教程:php设计模式之编程惯用法
  • php教程:php设计模式之前言
  • PHP新手上路(一)
  • 来自PHP.NET的入门教程
  • PHP新手上路(二)
  • PHP新手上路(四)
相关软件

  • 网友评论:
  • 查看所有评论
  • 我要发表评论
 

关于本站 | 广告联系 | 版权声明 | 网站地图 | 加入收藏 | 帮助中心 |

Copyright © 2006-2007 fwvv.net  程序支持:木翼  皖ICP备06004916号