PHP design pattern: Observer Pattern example, php design pattern observer

Source: Internet
Author: User
Tags spl

PHP design pattern: Observer Pattern example, php design pattern observer

First, understand the concept of observer mode: an object is made observability by adding a method (This method allows another object, that is, the observer registers himself. When an observed object changes, the message is sent to the registered observer. The operations performed by these observers using this information are irrelevant to the observed objects. The result is that objects can communicate with each other without understanding the cause. The observer mode is an event system, which means that this mode allows a class to observe the status of another class. When the observed class status changes, the observation class can receive notifications and make corresponding actions. The observer mode allows you to avoid close coupling between components.

UML structure diagram:

Problems solved by the observer Mode

In our development process, we should have encountered more or less some of the code changes that will cause other changes. Obviously, it is impossible to completely avoid this situation, however, we also try to minimize the dependency on other components, and the observer mode is to solve this problem.

For example, we have a post object with the Code as follows:

Class Post {protected $ _ userid = null; protected $ _ ip = null; protected $ _ content = null; function _ construct (){//...} // Posting method public function addPost (){//... posting logic }}

The above is a common post object. As the number of posts and access volume increase, operators start to quit, and the company often receives complaints, because our website has a lot of sensitive content and spam, we need to review the content. First, we need to review the user. Some blacklisted users should be banned from posting; the second is to review IP addresses; the third is to review sensitive words in the content. So our code looks like this:

class Post{protected $_userid = null;protected $_ip = null;protected $_content = null;function __construct(){}public function addPost(){if (!Postscan::checkUserid($tihs->_userid)) {return false;}if (!Postscan::ipUserid($tihs->_ip)) {return false;}if (!Postscan::checkContent($tihs->_content)) {return false;}// ... }}

As more and more fields need to be reviewed, the addPost method becomes longer and longer, and the published object can only be tightly embedded into the system.

Observer mode implementation

The core of the observer mode is to separate the observer from the subject. When the subject knows that an event occurs, the observer needs to be notified. At the same time, we do not want to write the relationship between the subject and the observer to death, let's modify the above Code:

// Interface Observable {public function attach (Observer $ observer); public function detach (Observer $ observer); public function between Y ();} // interface Observer {public function do (Observable $ subject);} class Post implements Observable {protected $ _ userid = null; protected $ _ ip = null; protected $ _ content = null; protected $ _ observerlist = array (); function _ construct () {} public function Attach (Observer $ observer) {$ this-> _ observerlist [] = $ observer;} public function detach (Observer $ observer) {foreach ($ this-> _ observerlist as $ key => $ value) {if ($ observer = $ value) {unset ($ this-> _ observerlist [$ key]) }} public function Policy () {foreach ($ this-> _ observerlist as $ value) {if (! $ Value-> do ($ this) {return false ;}} return true;} public function addPost () {if (! $ This-> Policy () {return false ;}//...}}

Through the above code, we can easily add audit rules.

SPL code

The observer mode is a common and common design mode, so that SPL extensions have encapsulated the corresponding classes and methods for us. The following Code is based on the three elements provided by SPL: SplObserver, code implemented by SplSubject and SplObjectStorage

class Post implements SplSubject{protected $_userid = null;protected $_ip = null;protected $_content = null;protected $_storage = new SplObjectStorage();function __construct(){}public function attach(SplObject $observer){$this->_storage->attach($observer);}public function detach(SplObject $observer){$this->_storage->detach($observer);}public function notify(){foreach ($this->_storage as $value) {if (!$value->update($this)) {return false;}}return true;}public function addPost(){if (!$this->notify()) {return false;}// ... }}

The most important thing is understanding. In this example, we have removed some review methods from the post class, the post object can also be used as another release type.

The implementation of the above content is the observer mode of the PHP design mode introduced by xiaobian. I hope it will help you!

Articles you may be interested in:
  • Observer mode in php
  • Php design mode Observer (Observer Mode)
  • Introduction to observer pattern in PHP design pattern
  • PHP observer mode implementation code
  • Php design pattern: Observer Pattern
  • PHP design pattern-Observer pattern (Observer) detailed introduction and code example
  • Code for implementing the observer mode in ruby, javascript, and php
  • Simple Example of observer mode in php
  • Learning php design pattern php implements Observer pattern (Observer)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.