<?php
class Wechat
{
private $subscribers = array();
public function register(Subscriber $subscriber)
{
$this->subscribers[] = $subscriber;
}
public function pushMessage($message)
{
foreach($this->subscribers as $subscriber)
{
$subscriber->getMessage($message);
}
}
}
interface Subscriber
{
public function getMessage($message);
}
class Evelin implements Subscriber
{
public function getMessage($message)
{
echo 'Evelin: '. $message;
}
}
class Anke implements Subscriber
{
public function getMessage($message)
{
echo 'Anke: '. $message;
}
}
$wechat = new Wechat();
$evelin = new Evelin();
$anke = new Anke();
$wechat->register($evelin);
$wechat->register($anke);
$wechat->pushMessage('发新消息啦!');
网友评论