考虑全寿命周期内的健壮性。
解耦>高度内聚的组件>只开发一次>只测试一次>需求不断变更>一直可用
客户端代码调用方式:
$login=new Login();
new RequirementObserver($login);
new AppointmentObserver($login);
$login->dologin();
登陆/注册 观察者模式的实现
<?php
class Login implements SplSubject{
private $storage;
private $status;
function __construct($sta) {
$this->storage = new SplObjectStorage();
}
function attach( SplObserver $observer ) {
$this->storage->attach( $observer );
}
function detach( SplObserver $observer ) {
$this->storage->detach( $observer );
}
function dologin() {
$this->status=function(){
//login codes
};
foreach ( $this->storage as $obs ) {
$obs->update( $this );
}
}
function getStatus() {
return $this->status;
}
}
abstract class LoginObserver implements SplObserver {
private $login;
function __construct( Login $login ) {
$this->login = $login;
$login->attach( $this );
}
function update( SplSubject $subject ) {
if ( $subject === $this->login ) {
$this->doUpdate( $subject );
}
}
abstract function doUpdate( Login $login );
}
class RequirementObserver extends LoginObserver {
function doUpdate( Login $login ) {
$status = $login->getStatus();
if ($status) {}else{}
}
}
class AppointmentObserver extends LoginObserver {
function doUpdate( Login $login ) {
$status = $login->getStatus();
if ($status) {}else{}
}
}