PHP design pattern: Observer pattern-jerrylsxu

Source: Internet
Author: User
PHP design pattern-Introduction to jerrylsxu

There are two schools. some people suggest using the design mode, while others do not!
This is the same as writing articles. some people like to write articles according to their routines, such as narrative articles, time, place, characters, and events. Some people prefer to write essays or essays, while others prefer to write poems!

Writing code is similar to writing an article in many places, but in some places it requires more skills than writing an article! If you write more articles, you can write excellent articles, and the code is the same. if you write more articles, you can write a lot of code!

Most of the time, when I look at the design patterns, some design patterns just fit my code habits. However, if you stick to it, it is counterproductive. -- Most of the time, you have learned how to use these tricks in your applications without knowing how to use them. but do not stick to the tricks. is the so-called "no choice, no choice, or no choice?

My original intention to learn the design model is to know that there is such a thing? I have such an impression in my mind that I will not create it! If the design pattern does not meet your habits, it is not good for you to read the code!

The observer mode defines the one-to-many dependency of an object. in this way, when an object changes its status, all its dependent persons will receive notifications and update them automatically!

Design principles

In the Observer Mode, the state of the topic and the number of observers are changed. In this mode, you can change objects dependent on the topic state without changing the topic. -- Find out the changing aspects in the program, and then separate them from the fixed ones!

Both the topic and the Observer use interfaces: The Observer uses the topic interface to register with the topic, and the topic uses the observer interface to notify the observer. This allows the two to operate normally and has the advantage of loose coupling! -- For interface programming, not for implementation programming!

.

Observer Mode uses "combination" to combine many observers into the topic. This relationship between objects (observer-topic) is not generated by inheritance, but by using combinations at runtime. -- Multi-purpose combination, less inheritance!

Code

/**

* Observer Mode

* @author: Mac

*/

class Paper{ /* Topic */

private $_observers = array();

public function register($sub){ /* Register the observer */

$this->_observers[] = $sub;

}

public function trigger(){ /* Unified external access */

if(!empty($this->_observers)){

foreach($this->_observers as $observer){

$observer->update();

}

}

}

}

/**

* The interface to be implemented by the Observer

*/

interface Observerable{

public function update();

}

class Subscriber implements Observerable{

public function update(){

echo "Callback\n";

}

}

The following is the test code.

/* Test */

$paper = new Paper();

$paper->register(new Subscriber());

//$paper->register(new Subscriber1());

//$paper->register(new Subscriber2());

$paper->trigger();

Summary

When a new object is to be filled in, you only need to register it in the topic (also called the observer) (there are many registration methods, you can also construct, or register in the interface accessed by the framework), and then implement code directly in the interface of the new object. This reduces the coupling between the subject object and the observer object.

A good design pattern does not directly go into your code, but into your brain.

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.