题记
==============================================================================
本php设计模式专辑来源于博客(jymoz.com),现在已经访问不了了,这一系列文章是我找了很久才找到完整的,感谢作者jymoz的辛苦付出哦!

本文地址:http://www.cnblogs.com/davidhhuan/p/4248205.html

==============================================================================

当我们在星际中开地图和几家电脑作战的时候,电脑的几个玩家相当于结盟,一旦我们出兵进攻某一家电脑,其余的电脑会出兵救援。
那么如何让各家电脑知道自己的盟友被攻击了呢?并且自动做出反应?

待解决的问题:一旦某个电脑被我们进攻,其他电脑就获知,并且自动出兵救援。

思路:为电脑设置一些额外的观察系统,由他们去通知其他电脑。

观察者(Observer)模式示例:

<?php
//抽象的结盟类
abstract class abstractAlly
{
//放置观察者的集合,这里以简单的数组来直观演示
public $oberserverCollection; //增加观察者的方法,参数为观察者(也是玩家)的名称
public function addOberserver($oberserverName)
{
//以元素的方式将观察者对象放入观察者的集合
$this->oberserverCollection[] = new oberserver($oberserverName);
} //将被攻击的电脑的名字通知各个观察者
public function notify($beAttackedPlayerName)
{
//把观察者的集合循环
foreach ($this->oberserverCollection as $oberserver)
{
//调用各个观察者的救援函数,参数为被攻击的电脑的名字,if用来排除被攻击的电脑的观察者
if($oberserver->name != $beAttackedPlayerName)
{
$oberserver->help($beAttackedPlayerName);
}
}
} abstract public function beAttacked($beAttackedPlayer);
} //具体的结盟类
class Ally extends abstractAlly
{
//构造函数,将所有电脑玩家的名称的数组作为参数
public function __construct($allPlayerName)
{
//把所有电脑玩家的数组循环
foreach ($allPlayerName as $playerName)
{
//增加观察者,参数为各个电脑玩家的名称
$this->addOberserver($playerName);
}
} //将被攻击的电脑的名字通知各个观察者
public function beAttacked($beAttackedPlayerName)
{
//调用各个观察者的救援函数,参数为被攻击的电脑的名字,if用来排除被攻击的电脑的观察者
$this->notify($beAttackedPlayerName);
}
} //观察者的接口
interface Ioberserver
{
//定义规范救援方法
function help($beAttackedPlayer);
} //具体的观察者类
class oberserver implements Ioberserver
{
//观察者(也是玩家)对象的名字
public $name; //构造函数,参数为观察者(也是玩家)的名称
public function __construct($name)
{
$this->name = $name;
} //观察者进行救援的方法
public help($beAttackedPlayerName)
{
//这里简单的输出,谁去救谁,最后加一个换行,便于显示
echo $this->name." help ".$beAttackedPlayerName."<br>";
} abstract public function beAttacked($beAttackedPlayer);
} //假设我一对三,两家虫族,一家神族
$allComputePlayer = array('Zerg1', 'Protoss2', 'Zerg2'); //新建电脑结盟
$Ally = new Ally($allComputePlayer); //假设我进攻了第二个虫族
$Ally->beAttacked('Zerg2'); ?>

用途总结:观察者模式可以将某个状态的变化立即通知所有相关的对象,并调用对方的处理方法。

实现总结:需要一个观察者类来处理变化,被观察的对象需要实现通知所有观察者的方法。

相关文章:

1. 星际争霸之php面向对象(一)

2. 星际争霸之php面向对象(二)

3. 星际争霸之php设计模式--简单工厂模式

4. 星际争霸之php设计模式--工厂方法模式

5. 星际争霸之php设计模式--抽象工厂模式

6. 星际争霸之php设计模式--建造器模式

7. 星际争霸之php设计模式--中介者模式

8. 星际争霸之php设计模式--享元模式

9. 星际争霸之php设计模式--代理模式

10. 星际争霸之php设计模式--原型模式

11. 星际争霸之php设计模式--备忘模式

12. 星际争霸之php设计模式--模板模式

13. 星际争霸之php设计模式--正面模式

14. 星际争霸之php设计模式--状态模式

15. 星际争霸之php设计模式--策略模式

16. 星际争霸之php设计模式--组合模式

17. 星际争霸之php设计模式--职责链模式

18. 星际争霸之php设计模式--观察者模式

19. 星际争霸之php设计模式--迭代器模式

20. 星际争霸之php设计模式--适配器模式

18. 星际争霸之php设计模式--观察者模式的更多相关文章

  1. 20. 星际争霸之php设计模式--适配器模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  2. 19. 星际争霸之php设计模式--迭代器模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  3. 17. 星际争霸之php设计模式--职责链模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  4. 16. 星际争霸之php设计模式--组合模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  5. 15. 星际争霸之php设计模式--策略模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  6. 14. 星际争霸之php设计模式--状态模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  7. 13. 星际争霸之php设计模式--正面模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  8. 12. 星际争霸之php设计模式--模板模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  9. 11. 星际争霸之php设计模式--备忘模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

随机推荐

  1. ios修改产品名

    在创建项目的时候,会设置一个项目名,以后生成的APP名字也就是这个了,但由于某种原因,我想修改APP名字,也就是屏幕程序图标下面显示的那个,这该怎么办呢? 下面有三种方法都可以: 修改Product ...

  2. RestTemplate实践

    什么是RestTemplate? RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效 ...

  3. 【Go语言】面向对象扩展——接口

    简单地说 Interface是一组Method的组合,可以通过Interface来定义对象的一组行为.如果某个对象实现了某个接口的所有方法,就表示它实现了该借口,无需显式地在该类型上添加接口说明. I ...

  4. How does controller listen to service?

    Polling. The Controller periodically asks the Service for the latest data. IMHO, this option sucks, ...

  5. [LintCode] Plus One 加一运算

    Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...

  6. [LintCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  7. preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead

    由于方法preg_replace()为PHP 5.5.x 中废弃的特性,官方建议需要在代码中将preg_replace()替换为函数preg_replace_callback,可以问题解决. 具体请见 ...

  8. Struts基础详解

    1.web.xml配置: <filter> <filter-name>Struts2</filter-name> <filter-class> org. ...

  9. Socket请求和Http请求的各自特点、区别及适用场景

    Socket实现服务器与客户端之间的物理连接,并进行数据传输.主要有TCP/UDP两个协议.Socket处于网络协议的传输层.TCP:传输控制协议,面向连接的的协议,稳定可靠.当客户和服务器彼此交换数 ...

  10. jquery 绑定事件的方法

    jQuery中提供了四种绑定事件的方法,分别是bind.live.delegate.on,对应的解除监听的函数分别是unbind.die.undelegate.off: 一.on()方法(首选方法) ...