This article is about PHP (of course, language doesn't matter. Attention should be paid to ideas, rather than implementation tools) project, observer design pattern application case analysis. In this case, I will share with you my understanding of the observer (concept), why we should use the Observer Model (Application Scenario), and some nagging (expansion ). Oh, right. This article
This article is about PHP (of course, language doesn't matter. Attention should be paid to ideas, rather than implementation tools) project, observer design pattern application case analysis. In this case, I will share with you my understanding of the observer (concept), why we should use the Observer Model (Application Scenario), and some nagging (expansion ). Oh, right. This article
This article is about PHP (of course, language doesn't matter. Attention should be paid to ideas, rather than implementation tools) project, observer design pattern application case analysis. In this case, I will share with you my understanding of the observer (concept), why we should use the Observer Model (Application Scenario), and some nagging (expansion ). Oh, right. The Code provided in this article is sample code, which is part of the source code in the project. It is slightly changed and cannot be reused directly.
Concept:
Maybe, you will have something like "hook", "trigger", "event-driven", and "callback function. In fact, these ideas are more or less related to the observer design patterns I want to talk about.
If you Understand Java, you will see such code (event listening ):
this.addWindowListener(this);
If you know Javascript, you will see such code (callback function ):
$.get(“http://www.ihuxu.com”,function() {// code...});
If you have neither heard of those terms nor seen the above similar code. OK. Let me talk about the observer's thoughts.
It is very simple. It is nothing more than implementing a program based on the expected behavior when a specified event occurs. To put it simply, for example, there is A game, A and B. If A performs A hop, B should make A smile. If A does, B should make A cry ...). This is the observer thought. I think you understand it. If you don't understand it, I am B ).
The following two terms are explained: Obsever and Obsevable ). B is the observer, and A is the observer (fully explained, understood by yourself ).
Application scenarios:
L foreground Summary
Now, I just joined this team. Our project is a platform-based application (of course, there are also websites ). Among them, the team member ML has implemented the active push at the end and adopts the template message interface (the event source is the user's posting or participating in some of the functions we developed ). The problem arises. At this time, the boss said, "I am responsible for using directmail. I simply asked about the ML push mechanism and used a procedural method. This leads to poor expansion and maintenance of new functions. Extension means that my functions cannot be added to the original program because there is no standard interface for me to use. Maintenance means that even though I add the directmail function, this will make our code more redundant and make it unable to be updated.
Therefore, I proposed to ML to adopt the observer design model to reach an agreement.
L code
Since there are no unified standards for poor scalability, we need to provide a set of standards:
PushObsevable. php (Observer Interface)
/*** Push by (acceptable) Observer Interface ** Created by GenialX at 2014.11.24 **/Interface PushObservable {// register (ADD) Observer Public function addObserver (PushObserver $ pObserver ); // remove the observer Public function removeObserver (PushObserver $ pObserver); // notify the observer Public function using Y ();}
PushObserver (Observer Interface)
/*** Push observer Interface ** Created by GenialX at 2014.11.24 **/Interface PushObserver {// return the name Public function getName (); // update Public function update ();}
Standard interfaces are available, but you do not want to directly declare classes to directly implement these interfaces, because this is an unfavorable implementation, as a result, the same implementation needs to be re-written every time a class is declared to inherit this interface. Therefore, two abstract classes are written to inherit and implement the member methods in these interfaces.
AbstractPushObservable (Observer abstract class)
/*** Abstract class of the observer ** Created by GenialX at 2014.11.24 **/require_once 'pushobservable. php'; Abstract Class Implements actpushobservable Implements PushObservable {Private $ _ observers = array (); // observer container // register (ADD) Observer Public function addObserver (PushObserver $ pObserver) {if (! Isset ($ this-> _ observers [$ pObserver-> getName ()]) {$ this-> _ observers [$ pObserver-> getName ()] = & $ pObserver; return TRUE;} return FALSE;} // remove the Public function removeObserver (PushObserver $ pObserver) of the observer) {if (isset ($ this-> _ observers [$ pObserver-> getName ()]) {$ this-> _ observers [$ pObserver-> getName ()] = NULL; unset ($ this-> _ observers [$ pObserver-> getName ()]); return TRUE;} return FALSE ;} // obtain the unique identifier of the observer // $ name. The InterfacePush constant Public function getObserver ($ name) {if (isset ($ this-> _ observers [$ name]) can be obtained. {return $ this-> _ observers [$ name];} return FALSE;} // notify the observer Public function using Y () {foreach ($ this-> _ observers as $ observer) {$ observer-> update ();}}}
AbstractPushObserver (Observer abstract class)
/*** Observer abstract class ** Created by GenialX at 2014.11.24 **/require_once 'pushobserver. php'; Abstract Class Implements actpushobserver Implements PushObserver {Private $ _ name; // name (unique flag) Public function _ construct ($ name = NULL) {if (is_null ($ name) {$ this-> _ name = 'your actpushobserver '. rand ();} else {$ this-> _ name = $ name ;}// return the Public function getName () {return $ this-> _ name ;} // update Public function update () {// rewrite method ...}}
With the abstract class, can I directly declare that the class inherits these abstract classes? Yes. However, in our project, direct declaration classes are not used to inherit these abstract classes. Because there are still some common implementations of template message push and directmail. Therefore, public implementation classes are written respectively.
CommonPushObservable (public push observer class)
/*** The public push observer class ** Created by GenialX at 2014.11.24 **/require_once 'implements actpushobservable. php'; Class CommonPushObservable Extends actpushobservable {Public function _ construct (){}}
CommonPushObserver (public push observer class)
/*** Public push observer class ** Created by GenialX at 2014.11.25 **/require_once 'using actpushobserver. php'; class CommonPushObserver extends actpushobserver {private static $ weixinUsers = array (); // the push-compliant user information is private static $ renrenUsers = array (); // public static $ CI; Public function _ construct ($ name = NULL) {parent :__ construct ($ name); CommonPushObserver :: $ CI = & get_instance (); CommonPushObserver: $ CI-> load-> library ('distance ');} /** obtain the user information that meets the push conditions based on the geographical location ** @ param float $ lon * @ param float $ lat * @ param string $ fromUser **/Public Static function getUsers ($ lon, $ lat, $ fromUser ){//...}
At this time, the push mechanism standards have been designed. You only need to implement different functions based on this standard. In this way, you do not have to worry about expansion and maintenance. Because there is a standard.
Let's take a look at the call code (this is so elegant ):
$ CPO = new CommonPushObservable (); // instantiate the observer $ EP = new EmailPush (EmailPush: PUSH_EMAIL_OBJECT); // instantiate the observer (Mail push observer class, with code below) $ WP = new WechatPush (WechatPush: PUSH_EMAIL_OBJECT); // instantiate the observer (message template observer class) $ EP-> setPushType ($ EP: PUSH_TYPE_WISH_LIST ); // set the push type $ EP-> setPushType ($ WP: PUSH_TYPE_WISH_LIST); // set the push type $ EP-> id ('12 ') -> email ('genialx @ 163.com ')-> text ('Hello World! '); // Set the message body $ EP-> openid ('openid')-> text ('Hello World! '); // Set the message body $ EP-> getUsers ($ lon, $ lat, $ fromUser); // obtain the user $ CPO-> addObserver ($ EP ); // register the observer $ CPO-> addObserver ($ WP); // register the observer $ CPO-> notify y (); // notify the observer to push messages and mail messages
Extension:
1. If the push type and message body settings are repeated, will you think of encapsulation?
2. Is the existence of constants similar to PUSH_TYPE_WISH_LIST better?
The answer is yes!
PHP observer mode http://www.ihuxu.com/php-design-mode-observer.html