Chain of Responsibility pattern php (The chain of responsibility pattern)

He sent his family returned from the train station, the continuation code.

<?php
/*
The chain of responsibility pattern decouples the sender of a request from its
receiver, by enabling more than one object to handle requests, in a chain manner.
Various types of handling objects can be added dynamically to the chain. Using a
recursive composition chain allows for an unlimited number of handling objects.
*/

abstract class SocialNotifier {
    private $notifyNext = null;
    
    public function notifyNext(SocialNotifier $notifyNext) {
        $this->notifyNext = $notifyNext;
        return $this->notifyNext;
    }
    
    final public function push($message) {
        $this->publish($message);
        
        if ($this->notifyNext !== null) {
            $this->notifyNext->push($message);
        }
    }
    
    abstract protected function publish($message);
}

class TwitterSocialNotifier extends SocialNotifier {
    public function publish($message) {
        echo 'TwitterSocialNotifier_publish' . $message . '<br/>'; 
    }
}

class FacebookSocialNotifier extends SocialNotifier {
    protected function publish($message) {
        echo 'FacebookSocialNotifier_publish' . $message . '<br/>'; 
    }
}

class PinterestSocialNotifier extends SocialNotifier {
    protected function publish($message) {
        echo 'PinterestSocialNotifierr_publish' . $message . '<br/>'; 
    }
}

$notifier = new TwitterSocialNotifier();

$notifier->notifyNext(new FacebookSocialNotifier())
    ->notifyNext(new PinterestSocialNotifier());
$notifier->push('Awesome new product availiable.')


?>

Guess you like

Origin www.cnblogs.com/aguncn/p/11184318.html