Archive for July, 2012

作者:AngryFox 分类: Uncategorized July 22nd, 2012 暂无评论

VISITOR——情人节到了,要给每个MM送一束鲜花和一张卡片,可是每个MM送的花都要针对她个人的特点,每张卡片也要根据个人的特点来挑,我一个人哪搞得清楚,还是找花店老板和礼品店老板做一下Visitor,让花店老板根据MM的特点选一束花,让礼品店老板也根据每个人特点选一张卡,这样就轻松多了;

访问者模式:访问者模式的目的是封装一些施加于某种数据结构元素之上的操作。一旦这些操作需要修改的话,接受这个操作的数据结构可以保持不变。访问者模式适用于数据结构相对未定的系统,它把数据结构和作用于结构上的操作之间的耦合解脱开,使得操作集合可以相对自由的演化。访问者模式使得增加新的操作变的很容易,就是增加一个新的访问者类。访问者模式将有关的行为集中到一个访问者对象中,而不是分散到一个个的节点类中。当使用访问者模式时,要将尽可能多的对象浏览逻辑放在访问者类中,而不是放到它的子类中。访问者模式可以跨过几个类的等级结构访问属于不同的等级结构的成员类。

<?php
/**
* 访问者模式
*
* 表示一个作用于某对象结构中的各元素的操作,可以在不改变
* 各元素的类的前提下定义作用于这些元素的新操作
*
*/
abstract class Visitor
{
    abstract public function visitCroncreteElementA($element);
    abstract public function visitCroncreteElementB($element);
}
class ConcreteVisitor1 extends Visitor
{
    public function visitCroncreteElementA($element)
    {
        echo get_class($element)." visit 1A<br/>";
    }
    public function visitCroncreteElementB($element)
    {
        echo get_class($element)." visit 1B<br/>";
    }
}
class ConcreteVisitor2 extends Visitor
{
    public function visitCroncreteElementA($element)
    {
        echo get_class($element)." visit 2A<br/>";
    }
    public function visitCroncreteElementB($element)
    {
        echo get_class($element)." visit 2B<br/>";
    }
}
abstract class Element
{
    abstract public function accept($visitor);
    }
class ConcreteElementA extends Element
{
    public function accept($visitor)
    {
        $visitor->visitCroncreteElementA($this);
    }
}
class ConcreteElementB extends Element
{
    public function accept($visitor)
    {
        $visitor->visitCroncreteElementB($this);
    }
}
class ObjectStructure
{
    private $_elements = array();
    public function attach($element)
    {
        $this->_elements[] = $element;
    }
    public function detach($element)
    {
        if($key = array_search($element,$this->_elements) !== false)
            unset($this->_elements[$key]);
    }
    public function accept($visitor)
    {
        foreach($this->_elements as $element)
        {
            $element->accept($visitor);
        }
    }
}
//

$objOS = new ObjectStructure();
$objOS->attach(new ConcreteElementA());
$objOS->attach(new ConcreteElementB());
$objCV1 = new ConcreteVisitor1();
$objCV2 = new ConcreteVisitor2();
$objOS->accept($objCV1);
$objOS->accept($objCV2);
作者:AngryFox 分类: Uncategorized July 22nd, 2012 暂无评论

TEMPLATE METHOD——看过《如何说服女生上床》这部经典文章吗?女生从认识到上床的不变的步骤分为巧遇、打破僵局、展开追求、接吻、前戏、动手、爱抚、进去八大步骤(Template method),但每个步骤针对不同的情况,都有不一样的做法,这就要看你随机应变啦(具体实现);

模板方法模式:模板方法模式准备一个抽象类,将部分逻辑以具体方法以及具体构造子的形式实现,然后声明一些抽象方法来迫使子类实现剩余的逻辑。不同的子类可以以不同的方式实现这些抽象方法,从而对剩余的逻辑有不同的实现。先制定一个顶级逻辑框架,而将逻辑的细节留给具体的子类去实现。

<?PHP

abstract class TemplateAbstract {
    //the template method
    //  sets up a general algorithm for the whole class
    public final function showBookTitleInfo($book_in) {
        $title = $book_in->getTitle();
        $author = $book_in->getAuthor();
        $processedTitle = $this->processTitle($title);
        $processedAuthor = $this->processAuthor($author);
        if (NULL == $processedAuthor) {
            $processed_info = $processedTitle;
        } else {
            $processed_info = $processedTitle.' by '.$processedAuthor;
        }
        return $processed_info;
    }
    //the primitive operation
    //  this function must be overridded
    abstract function processTitle($title);
    //the hook operation
    //  this function may be overridden,
    //  but does nothing if it is not
    function processAuthor($author) {
        return NULL;
    }
}

class TemplateExclaim extends TemplateAbstract {
    function processTitle($title) {
      return Str_replace(' ','!!!',$title);
    }
    function processAuthor($author) {
      return Str_replace(' ','!!!',$author);
    }
}

class TemplateStars extends TemplateAbstract {
    function processTitle($title) {
        return Str_replace(' ','*',$title);
    }
}

class Book {
    private $author;
    private $title;
    function __construct($title_in, $author_in) {
        $this->author = $author_in;
        $this->title  = $title_in;
    }
    function getAuthor() {return $this->author;}
    function getTitle() {return $this->title;}
    function getAuthorAndTitle() {
        return $this->getTitle() . ' by ' . $this->getAuthor();
    }
}

  writeln('BEGIN TESTING TEMPLATE PATTERN');
  writeln('');

  $book = new Book('PHP for Cats','Larry Truett');

  $exclaimTemplate = new TemplateExclaim();
  $starsTemplate = new TemplateStars();

  writeln('test 1 - show exclaim template');
  writeln($exclaimTemplate->showBookTitleInfo($book));
  writeln('');

  writeln('test 2 - show stars template');
  writeln($starsTemplate->showBookTitleInfo($book));
  writeln('');

  writeln('END TESTING TEMPLATE PATTERN');

  function writeln($line_in) {
    echo $line_in."<br/>";
  }
作者:AngryFox 分类: Uncategorized July 22nd, 2012 暂无评论

STRATEGY——跟不同类型的MM约会,要用不同的策略,有的请电影比较好,有的则去吃小吃效果不错,有的去海边浪漫最合适,单目的都是为了得到MM的芳心,我的追MM锦囊中有好多Strategy哦。

策略模式:策略模式针对一组算法,将每一个算法封装到具有共同接口的独立的类中,从而使得它们可以相互替换。策略模式使得算法可以在不影响到客户端的情况下发生变化。策略模式把行为和环境分开。环境类负责维持和查询行为类,各种算法在具体的策略类中提供。由于算法和环境独立开来,算法的增减,修改都不会影响到环境和客户端。

<?PHP

class StrategyContext {
    private $strategy = NULL;
    //bookList is not instantiated at construct time
    public function __construct($strategy_ind_id) {
        switch ($strategy_ind_id) {
            case "C":
                $this->strategy = new StrategyCaps();
            break;
            case "E":
                $this->strategy = new StrategyExclaim();
            break;
            case "S":
                $this->strategy = new StrategyStars();
            break;
        }
    }
    public function showBookTitle($book) {
      return $this->strategy->showTitle($book);
    }
}

interface StrategyInterface {
    public function showTitle($book_in);
}

class StrategyCaps implements StrategyInterface {
    public function showTitle($book_in) {
        $title = $book_in->getTitle();
        $this->titleCount++;
        return strtoupper ($title);
    }
}

class StrategyExclaim implements StrategyInterface {
    public function showTitle($book_in) {
        $title = $book_in->getTitle();
        $this->titleCount++;
        return Str_replace(' ','!',$title);
    }
}

class StrategyStars implements StrategyInterface {
    public function showTitle($book_in) {
        $title = $book_in->getTitle();
        $this->titleCount++;
        return Str_replace(' ','*',$title);
    }
}

class Book {
    private $author;
    private $title;
    function __construct($title_in, $author_in) {
        $this->author = $author_in;
        $this->title  = $title_in;
    }
    function getAuthor() {
        return $this->author;
    }
    function getTitle() {
        return $this->title;
    }
    function getAuthorAndTitle() {
        return $this->getTitle() . ' by ' . $this->getAuthor();
    }
}

  writeln('BEGIN TESTING STRATEGY PATTERN');
  writeln('');

  $book = new Book('PHP for Cats','Larry Truett');

  $strategyContextC = new StrategyContext('C');
  $strategyContextE = new StrategyContext('E');
  $strategyContextS = new StrategyContext('S');

  writeln('test 1 - show name context C');
  writeln($strategyContextC->showBookTitle($book));
  writeln('');

  writeln('test 2 - show name context E');
  writeln($strategyContextE->showBookTitle($book));
  writeln('');

  writeln('test 3 - show name context S');
  writeln($strategyContextS->showBookTitle($book));
  writeln('');

  writeln('END TESTING STRATEGY PATTERN');

  function writeln($line_in) {
    echo $line_in."<br/>";
  }

  /*
     EXPECT:
        BEGIN TESTING STRATEGY PATTERN

        test 1 - show name context C
        PHP FOR CATS

        test 2 - show name context E
        PHP!for!Cats

        test 3 - show name context S
        PHP*for*Cats

        END TESTING STRATEGY PATTERN
  */
作者:AngryFox 分类: Uncategorized July 22nd, 2012 暂无评论
<?php
/**
 *
 *
 * Allow an object to alter its behavior when its internal state changes.
 * The object will appear to change its class.
 */

class BookContext {
    private $book = NULL;
    private $bookTitleState = NULL;
    //bookList is not instantiated at construct time
    public function __construct($book_in) {
      $this->book = $book_in;
      $this->setTitleState(new BookTitleStateStars());
    }
    public function getBookTitle() {
      return $this->bookTitleState->showTitle($this);
    }
    public function getBook() {
      return $this->book;
    }
    public function setTitleState($titleState_in) {
      $this->bookTitleState = $titleState_in;
    }
}

interface BookTitleStateInterface {
    public function showTitle($context_in);
}

class BookTitleStateExclaim implements BookTitleStateInterface {
    private $titleCount = 0;
    public function showTitle($context_in) {
      $title = $context_in->getBook()->getTitle();
      $this->titleCount++;
      $context_in->setTitleState(new BookTitleStateStars());
      return Str_replace(' ','!',$title);
    }
}

class BookTitleStateStars implements BookTitleStateInterface {
    private $titleCount = 0;
    public function showTitle($context_in) {
      $title = $context_in->getBook()->getTitle();
      $this->titleCount++;
      if (1 < $this->titleCount) {
        $context_in->setTitleState(new BookTitleStateExclaim);
      }
      return Str_replace(' ','*',$title);
    }
}

class Book {
    private $author;
    private $title;
    function __construct($title_in, $author_in) {
      $this->author = $author_in;
      $this->title  = $title_in;
    }
    function getAuthor() {return $this->author;}
    function getTitle() {return $this->title;}
    function getAuthorAndTitle() {
      return $this->getTitle() . ' by ' . $this->getAuthor();
    }
}

  writeln('BEGIN TESTING STATE PATTERN');
  writeln('');

  $book = new Book('PHP for Cats','Larry Truett');;
  $context = new bookContext($book);

  writeln('test 1 - show name');
  writeln($context->getBookTitle());
  writeln('');

  writeln('test 2 - show name');
  writeln($context->getBookTitle());
  writeln('');

  writeln('test 3 - show name');
  writeln($context->getBookTitle());
  writeln('');

  writeln('test 4 - show name');
  writeln($context->getBookTitle());
  writeln('');

  writeln('END TESTING STATE PATTERN');

  function writeln($line_in) {
    echo $line_in."<br/>";
  }

?>
作者:AngryFox 分类: Uncategorized July 22nd, 2012 暂无评论

STATE———跟MM交往时,一定要注意她的状态哦,在不同的状态时她的行为会有不同,比如你约她今天晚上去看电影,对你没兴趣的MM就会说“有事情啦”,对你不讨厌但还没喜欢上的MM就会说“好啊,不过可以带上我同事么?”,已经喜欢上你的MM就会说“几点钟?看完电影再去泡吧怎么样?”,当然你看电影过程中表现良好的话,也可以把MM的状态从不讨厌不喜欢变成喜欢哦。

状态模式:状态模式允许一个对象在其内部状态改变的时候改变行为。这个对象看上去象是改变了它的类一样。状态模式把所研究的对象的行为包装在不同的状态对象里,每一个状态对象都属于一个抽象状态类的一个子类。状态模式的意图是让一个对象在其内部状态改变的时候,其行为也随之改变。状态模式需要对每一个系统可能取得的状态创立一个状态类的子类。当系统的状态变化时,系统便改变所选的子类。

<?php
interface State
{
    /**
     * @param string $input     '1' or '0'
     * @return State            next State
     */
    public function parse($input);

    /**
     * @return boolean          whether a binary string that
     *                          brought to this State is valid
     */
    public function valid();
}

/**
 * A ConcreteState. The machine is in this state
 * when the number of 1 read is even (valid string).
 */
class EvenOnesState implements State
{
    public function parse($input) {
        if ($input == 1) {
            return new OddOnesState;
        } else {
            return $this;
        }
    }

    public function valid()
    {
        return true;
    }
}

/**
 * A ConcreteState. The machine is in this state
 * when the number of 1 read is odd (invalid string).
 */
class OddOnesState implements State
{
    public function parse($input) {
        if ($input == 1) {
            return new EvenOnesState;
        } else {
            return $this;
        }
    }

    public function valid()
    {
        return false;
    }
}

class ParityBitValidator
{
    protected $_state;

    /**
     * @param State $initialState   the state at the reset
     */
    public function __construct(State $initialState)
    {
        $this->_state = $initialState;
    }

    /**
     * @param string $bits
     * @return boolean
     */
    public function isValid($bits)
    {
        for ($i = 0; $i < strlen($bits); $i++) {
            $bit = $bits{$i};

            $this->_state = $this->_state->parse($bit);
        }
        return $this->_state->valid();
    }
}

$validator = new ParityBitValidator(new EvenOnesState);
var_dump($validator->isValid('10101001101'));    // true
var_dump($validator->isValid('101010011011'));   // false
作者:AngryFox 分类: Uncategorized July 22nd, 2012 暂无评论
<?php
 /**
  * 观察者模式(被观察者)
  *
  * @author Administrator
  *
 */
 class Subject implements SplSubject {

     /**
      * 设置的参数
      *
      * @var mixed
 */
     protected $_value;

     /**
      *
      *
      * @var SplObjectStorage
 */
     protected $_observers;

     /**
      * 构造函数
      *
      * @return void
 */
     public function __construct() {
         $this->_observers = new SplObjectStorage();
     }

     /**
      * (non-PHPdoc)
      * @see SplSubject::attach()
 */
     public function attach(SplObserver $observer) {
         $this->_observers->attach($observer);
     }

     /**
      * (non-PHPdoc)
      * @see SplSubject::detach()
 */
     public function detach(SplObserver $observer) {
         $this->_observers->detach($observer);
     }

     /**
      * (non-PHPdoc)
      * @see SplSubject::notify()
 */
     public function notify() {
         foreach ($this->_observers as $observer) {
             $observer->update($this);
         }
     }

     /**
      * 设置最新值
      *
      * @param mixed $value
      * @return
 */
     public function setValue($value) {
         $this->_value = $value;
         $this->notify();
     }

     /**
      * 获取最后更新的值
      *
      * @return mixed
 */
     public function getValue() {
         return $this->_value;
     }
 }

 /**
  * 观察者模式(观察者)
  *
  * @author Administrator
  *
 */
 class Observer implements SplObserver {

     /**
      * (non-PHPdoc)
      * @see SplObserver::update()
 */
     public function update(SplSubject $subject) {
         var_dump('最后更新时间为:' . $subject->getValue());
     }
 }

 date_default_timezone_set('Asia/Shanghai');
 error_reporting(E_ALL);
 $subject = new Subject();
 $observer = new Observer();
 $subject->attach($observer);
 $subject->setValue(date('Y-m-d H:i:s', time()));
作者:AngryFox 分类: Uncategorized July 22nd, 2012 暂无评论

[PHP] /**
*/

interface Message {
static function getType();
};

interface Observer {
function notifyMsg(Message $msg);
};

class Subject {
private $observers = Array();

public function registerObserver(Observer $observer, $msgType) {
$this->observers[$msgType][] = $observer;
}

private function notifyMsg(Message $msg) {
@$observers = $this->observers[$msg->getType()];
if (!$observers) {
return;
}

foreach ($observers as $observer) {
$observer->notifyMsg($msg);
}
}

public function someMethod() {
sleep(1);
$this->notifyMsg(new HelloMessage(“Michael”));
}

}

class HelloMessage implements Message {
private $name;

public function __construct($name) {
$this->name = $name;
}

public function getMsg() {
return “Hello,$this->name!”;
}

static function getType() {
return “HELLO_TYPE”;
}
}

class SubObserver implements Observer {
public function notifyMsg(Message $msg) {
if ($msg instanceof HelloMessage) {
echo $msg->getMsg();
}
}
}

$subject = new Subject();
$observer = new SubObserver();
$subject->registerObserver($observer, HelloMessage::getType());
$subject->someMethod();
/**
* 用途总结:观察者模式可以将某个状态的变化立即通知所有相关的对象,并调用对方的处理方法。
* 实现总结:需要一个观察者类来处理变化,被观察的对象需要实现通知所有观察者的方法。*
*/
?>[/PHP]

作者:AngryFox 分类: Uncategorized July 22nd, 2012 暂无评论

OBSERVER——想知道咱们公司最新MM情报吗?加入公司的MM情报邮件组就行了,tom负责搜集情报,他发现的新情报不用一个一个通知我们,直接发布给邮件组,我们作为订阅者(观察者)就可以及时收到情报啦

观察者模式:观察者模式定义了一种一队多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态上发生变化时,会通知所有观察者对象,使他们能够自动更新自己。

<?php
/**
 * 可以粗略的理解为,一个类(我们称之为被观察者),这个类继承了一个被观察者容器的借口
 * 该接口中的方法有:添加观察者,踢出观察者,提醒观察者,和观察条件
 * 添加观察者即相当于把一些类注册到这个容器之中,当然这些类的前提是需要有一个相应被观察者反应信息的方法,
 * 被观察者容器通过接受不同的观察条件参数来判断是否需要提醒所有本容器中的已经注册过的观察者是否相应
 * 一旦满足提醒条件,提醒容器内所有观察者让其调用相应方法并且实现对应观察者实例的变化
 * 暂且理解到此!
 */

 interface Subject
{
    public function Attach($Observer); //添加观察者
    public function Detach($Observer); //踢出观察者
    public function Notify(); //满足条件时通知观察者
    public function SubjectState($Subject); //观察条件
}
class Boss Implements Subject
{
    public $_action;
    private $_Observer;
    public function Attach($Observer)
    {
        $this->_Observer[] = $Observer;
    }
    public function Detach($Observer)
    {
        $ObserverKey = array_search($Observer, $this->_Observer);
        if($ObserverKey !== false)
        {
            unset($this->_Observer[$ObserverKey]);
        }
    }
    public function Notify()
    {
        foreach($this->_Observer as $value )
        {
            $value->Update();
        }
    }
    public function SubjectState($Subject)
    {
        $this->_action = $Subject;
    }
}
abstract class Observer
{
    protected $_UserName;
    protected $_Sub;
    public function __construct($Name,$Sub)
    {
        $this->_UserName = $Name;
        $this->_Sub = $Sub;
    }
    public abstract function Update(); //接收通过方法
}
class StockObserver extends Observer
{
    public function __construct($name,$sub)
    {
        parent::__construct($name,$sub);
    }
    public function Update()
    {
        echo $this->_Sub->_action .$this->_UserName." YOU JUST RUNNING...";
    }
}
$huhansan = new Boss(); //被观察者
$gongshil = new StockObserver("sanmao ",$huhansan); //初始化观察者
$huhansan->Attach($gongshil); //添加一个观察者
$huhansan->Attach($gongshil); //添加一个相同的观察者
$huhansan->Detach($gongshil); //踢出基中一个观察者
$huhansan->SubjectState("the Poloce is coming..."); //达到满足的条件
$huhansan->Notify(); //通过所有有效的观察者
作者:AngryFox 分类: Uncategorized July 21st, 2012 暂无评论

MEMENTO——同时跟几个MM聊天时,一定要记清楚刚才跟MM说了些什么话,不然MM发现了会不高兴的哦,幸亏我有个备忘录,刚才与哪个MM说了什么话我都拷贝一份放到备忘录里面保存,这样可以随时察看以前的记录啦。

备忘录模式:备忘录对象是一个用来存储另外一个对象内部状态的快照的对象。备忘录模式的用意是在不破坏封装的条件下,将一个对象的状态捉住,并外部化,存储起来,从而可以在将来合适的时候把这个对象还原到存储起来的状态。

<?php
/**
 * The Memento pattern is implemented with two objects: the originator and a caretaker.
 * The originator is an object that has an internal state. The
 * caretaker performs an operation on the originator, but can
 * undo the change when it needed. The caretaker requests memento object from the
 * originator. Then it applies the operation. The caretaker can roll back
 *  the originator to the initial state returning the memento object to
 * the originator.
 */
class Memento
{
    private $_originator;
    public function  __construct(&$originator)
    {
        $this->_originator = &$originator;
    }
    public function save()
    {
        $this->_state = $this->_originator->getState();
    }
    public function restore()
    {
        $this->_originator->setState($this->_state);
    }
}
/**
 * Originator
 */
interface Restorable
{
    public function getState();
    public function setState($state);
}
class RestorableIterator implements Iterator, Restorable
{
    private $_data;
    private $_cursor = 0;
    private $_valid = false;

    public function  __construct(array $data = array())
    {
        $this->_data = $data;
        $this->_valid = (bool)count($this->_data);
    }
    public function current()
    {
        return $this->_data[$this->_cursor];
    }
    public function key()
    {
        return $this->_cursor;
    }
    public function getState()
    {
        return $this->_cursor;
    }
    public function setState($state)
    {
        $this->_cursor = $state;
    }
    public function next()
    {
        $max = count($this->_data) - 1;
        $this->_valid = $this->_cursor < $max ? (bool)(++$this->_cursor) : false;
    }
    public function rewind()
    {
        $this->_valid = (bool)count($this->_data);
        $this->_cursor = 0;
    }
    public function valid()
    {
        return $this->_valid;
    }
}
/**
 * Caretaker
 */
$it = new RestorableIterator(array('a', 'b', 'c', 'd'));
$itMemento = new Memento($it);
$it->next();
$itMemento->save();
$it->next();
$it->next();
echo "Current value ", $it->current(), "\n";
$itMemento->restore();
echo "Current value ", $it->current(), "\n";

// Output:
// Current value d
// Current value b
作者:AngryFox 分类: Uncategorized July 21st, 2012 暂无评论

MEDIATOR——四个MM打麻将,相互之间谁应该给谁多少钱算不清楚了,幸亏当时我在旁边,按照各自的筹码数算钱,赚了钱的从我这里拿,赔了钱的也付给我,一切就OK啦,俺得到了四个MM的电话。

调停者模式:调停者模式包装了一系列对象相互作用的方式,使得这些对象不必相互明显作用。从而使他们可以松散偶合。当某些对象之间的作用发生改变时,不会立即影响其他的一些对象之间的作用。保证这些作用可以彼此独立的变化。调停者模式将多对多的相互作用转化为一对多的相互作用。调停者模式将对象的行为和协作抽象化,把对象在小尺度的行为上与其他对象的相互作用分开处理。

通过引入调停者对象(Mediator),可以将系统的网状结构变成以中介者为中心的星形结构

<?php
//中介者模式(调停者模式)
abstract class AbstractColleague {

    protected $number;

    public function getNumber() {
        return $this->number;
    }

    public function setNumber($number, AbstractMediator $am = null) {
        $this->number = $number;
    }

}

class ColleagueA extends AbstractColleague {

    public function setNumber($number, AbstractMediator $am = null) {
        if($am == null) {
            parent::setNumber($number);
            return;
        }
        $this->number = $number;
        $am->AaffectB();
    }

}

class ColleagueB extends AbstractColleague {

    public function setNumber($number, AbstractMediator $am = null) {
        if($am == null) {
            parent::setNumber($number);
            return;
        }
        $this->number = $number;
        $am->BaffectA();
    }

}

abstract class AbstractMediator {

    protected $a;

    protected $b;

    public function __construct(AbstractColleague $a, AbstractColleague $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public abstract function AaffectB();

    public abstract function BaffectA();

}

class Mediator extends AbstractMediator {

    public function __construct(AbstractColleague $a, AbstractColleague $b) {
        parent::__construct($a, $b);
    }

    //处理A对B的影响
    public function AaffectB() {
        $this->b->setNumber($this->a->getNumber()*100);
    }

    //处理B对A的影响
    public function BaffectA() {
        $this->a->setNumber($this->b->getNumber()/100);
    }

}

$collA = new ColleagueA();
$collB = new ColleagueB();

$am = new Mediator($collA, $collB);

$collA->setNumber(1000, $am);
echo '通过设置A影响B<br/>collA的number值为:'.$collA->getNumber().'<br/>collB的unmber值为A的10倍:'.$collB->getNumber();
?>

优点:
把中介者作为一个独立的概念,可使对象之间的关系转为对象和中介者之间的关系, 可站在一个宏观的角度看问题。
缺点:
中介者集中控制关系,使得中介者变得复杂。