This article mainly introduced the PHP Observer pattern application scenario, combined with a complete instance form detailed analysis of the PHP observer pattern specific definition and use of skills, the need for friends can refer to the following
Specific as follows:
<?PHP/** * Observer Mode Scenario Instance * * Disclaimer: This article is only to Kazakhstan ticket network example, the example does not involve the Kazakhstan ticket network any business code, all original, if there is similar, purely coincidental. * * Scene Description: * Kazakhstan ticket to purchase tickets for the core business (this mode is not limited to the business), but around the purchase of tickets will produce different other logic, such as: * 1, after the purchase of a text log * 2, after the purchase of the record database log * 3, after the purchase of the ticket to send a text message * 4, the purchase of the ticket to * * Traditional Solution: * Add the relevant code inside the ticket logic and complete various logic. * * There is a problem: * 1, once a business logic changes, such as the purchase of additional business logic in the ticket business, the need to modify the purchase of the core documents, and even purchase the ticket process. * 2, after a long accumulation of documents lengthy, resulting in subsequent maintenance difficulties. * * The problem is mainly the "tight coupling" of the program, using the observation mode to optimize the current business logic to "loose coupling", to achieve easy maintenance, easy to modify the purpose, * but also in line with the idea of interface-oriented programming. * * Observer Mode TYPICAL implementation: * 1, define 2 interfaces: The Observer (notification) interface, the Observer (subject) interface * 2, define 2 classes, the Observer object implements the observer interface, the subject class implements the viewer interface * 3, the subject class registers itself to notice the Observer * 4, The subject class notifies the Observer object when a business logic occurs, and each observer executes its own business logic. * * Example: The following code * */Date_default_timezone_set (' PRC '); Set China time zone #=================== definition Observer, observer interface ============/** * * Observer Interface (notification interface) * */interface iticketobserver//Viewer interface { function Onbuyticketover ($sender, $args); Method called after notification}/** * * Subject interface * */interface iticketobservable//Observed object interface {function addobserver ($observer);//provide registered Observer method}#===== =============== Theme Class Implementation ========================/** * * Theme Class (Purchase ticket) * */class Hipiaobuy implements iticketobservable {// Implementing the theme interface (The observer) Private $_observers = Array (); Notification Array (Observer) Public Function Buyticket ($ticket)//Buy Ticket core class, process ticket purchase process {//TODO ticket logic//circular notification, call its onbuyticketover to implement different business logic fo Reach ($this->_observers as $obs) $obs->onbuyticketover ($this, $ticket); $this can be used to get the subject class handle, use}//Add notification in the notification public function Addobserver ($observer)//Add n Notifications {$this->_observers [] = $obser Ver }}#========================= define multiple notifications ====================//SMS Log notifications Class HIPIAOMSM implements Iticketobserver {public function Onbuyticketover ($sender, $ticket) {echo (date (' y-m-d h:i:s '). "SMS Logging: Ticket purchase success: $ticket <br>"); }}//text Log notification class Hipiaotxt implements Iticketobserver {public Function onbuyticketover ($sender, $ticket) {echo (date (' y-m-d h:i:s '). "Text logging: Ticket purchase success: $ticket <br>"); }}//deductible Volume giving notice class Hipiaodikou implements Iticketobserver {public Function onbuyticketover ($sender, $ticket) {echo (da Te (' y-m-d h:i:s '). "Complimentary offset roll: Ticket purchase: $ticket 10 yuan to deduct 1. <br> "); }}#========================= = User Purchased ==================== $buy = new Hipiaobuy (), $buy->addobserver (New Hipiaomsm ()); Add various notification $buy->addobserver (new Hipiaotxt ()) According to different business logic, $buy->addobserver (new Hipiaodikou);//Buy Tickets $buy-> Buyticket ("One row, one number"); >
The results of the operation are as follows:
2017-02-03 10:25:45 SMS Logging: Ticket purchase success: A row of the 2017-02-03 10:25:45 text logging: Ticket purchase success: A row of a 2017-02-03 10:25:45 complimentary offset Volume: Ticket success: one row number Complimentary 10 yuan for 1 card.
The above is the whole content of this article, I hope that everyone's study has helped.