PHP design pattern-Observer pattern (Observer) Detailed introduction and code example _ php instance

Source: Internet
Author: User
Tags php example
This article mainly introduces the Observer mode (Observer) of PHP design mode in detail and code examples. if you need it, refer [Intention]

Defines a one-to-many dependency between objects. when the status of an object changes, all objects dependent on it are notified and automatically updated. [GOF95] is also called the Publish-Subscribe mode and Model-View mode) mode, Source-Listener mode, or Dependents mode

[Observer Mode structure diagram]


[Main role in Observer Mode]

1. abstract topic role: the topic role stores all references to the Observer object in a collection. each topic can have any number of observers. Abstract topics provide interfaces for adding and deleting observer objects.
2. abstract Observer role: defines an interface for all specific observers and updates themselves when the observed topic changes.
3. ConcreteSubject role: stores the relevant status to a specific observer object. when the internal status of a specific topic changes, a notification is sent to all registered observers. A topic role is usually implemented by a specific subclass.
4. concretedObserver role: stores a specific topic object, stores related states, and implements the update interface required by the abstract observer role to ensure that its own status is consistent with that of the topic.

[Advantages and disadvantages of observer mode]

Advantages of observer mode:

1. the coupling between the observer and the topic is small;
2. supports broadcast communication;

Disadvantages of observer mode:

Since the observer does not know the existence of other observers, it may have no idea about the final cost of changing the target. This may cause unexpected updates.


[Observer mode applicable scenarios]

When an abstract model has two aspects, one of which depends on the other.
When changing an object, you need to change other objects at the same time without knowing how many objects are to be changed.
When an object must notify other objects, it cannot assume who the other objects are. In other words, you do not want these objects to be tightly coupled.

[Observer mode and other mode]

1. Mediator mode: by encapsulating complex update semantics, ChangeManager acts as the intermediary between the target and the Observer.
2. singleton mode: ChangeManager can use Singleton mode to ensure that it is unique and globally accessible.


[Observer mode PHP example]

The code is as follows:


/**
* Observer Mode
* @ Package design pattern
*/

/**
* Abstract topic role
*/
Interface Subject {

/**
* Adds a new observer object.
* @ Param Observer $ observer
*/
Public function attach (Observer $ observer );

/**
* Delete a registered observer object
* @ Param Observer $ observer
*/
Public function detach (Observer $ observer );

/**
* Notify all registered observer objects
*/
Public function yyobservers ();
}

/**
* Specific topic roles
*/
Class ConcreteSubject implements Subject {

Private $ _ observers;

Public function _ construct (){
$ This-> _ observers = array ();
}

/**
* Adds a new observer object.
* @ Param Observer $ observer
*/
Public function attach (Observer $ observer ){
Return array_push ($ this-> _ observers, $ observer );
}

/**
* Delete a registered observer object
* @ Param Observer $ observer
*/
Public function detach (Observer $ observer ){
$ Index = array_search ($ observer, $ this-> _ observers );
If ($ index = FALSE |! Array_key_exists ($ index, $ this-> _ observers )){
Return FALSE;
}

Unset ($ this-> _ observers [$ index]);
Return TRUE;
}

/**
* Notify all registered observer objects
*/
Public function policyobservers (){
If (! Is_array ($ this-> _ observers )){
Return FALSE;
}

Foreach ($ this-> _ observers as $ observer ){
$ Observer-> update ();
}

Return TRUE;
}

}

/**
* Abstract observer role
*/
Interface Observer {

/**
* Update method
*/
Public function update ();
}

Class ConcreteObserver implements Observer {

/**
* Observer name
* @ Var
*/
Private $ _ name;

Public function _ construct ($ name ){
$ This-> _ name = $ name;
}

/**
* Update method
*/
Public function update (){
Echo 'observer', $ this-> _ name, 'has notified.
';
}

}
Instantiation class:
$ Subject = new ConcreteSubject ();

/* Add the first observer */
$ Observer1 = new ConcreteObserver ('Martin ');
$ Subject-> attach ($ observer1 );

Echo'
The First sector Y:
';
$ Subject-> policyobservers ();

/* Add the second observer */
$ Observer2 = new ConcreteObserver ('phppany ');
$ Subject-> attach ($ observer2 );

Echo'
The Second policy:
';
$ Subject-> policyobservers ();

/* Delete the first observer */
$ Subject-> detach ($ observer1 );

Echo'
The Third policy:
';
$ Subject-> policyobservers ();
Case study:

/**
* 3.1php design mode-Observer Mode
* 3.1.1 concept: in fact, the Observer Mode is an easy-to-understand mode. it is an event system, meaning
* This mode allows a class to observe the status of another class. when the observed class status changes,
* The Observer class can receive notifications and make corresponding actions. The Observer mode allows you to avoid inter-component
* Another method of tight coupling
* 3.1.2 Key points:
* 1. observer-> append observer;-> one observer;-> notify observer when the condition is met;-> observation condition
* 2. observer-> accept observation method
* 3.1.3 Disadvantages:
* 3.1.4 Application of the observer mode in PHP: There are many observer applications in web development.
* Typical: user registration (verification email, activation of user information), email/SMS notification when a shopping website places an order
* 3.1.5php internal support
* SplSubject interface, which represents the observed object,
* Structure:
* Interface SplSubject
*{
* Public function attach (SplObserver $ observer );
* Public function detach (SplObserver $ observer );
* Public function Y ();
*}
* SplObserver interface, which represents the object that acts as the Observer,
* Structure:
* Interface SplObserver
*{
* Public function update (SplSubject $ subject );
*}
*/

/**
* User login-interpreting observer mode
*/
Class User implements SplSubject {
// Register the Observer
Public $ observers = array ();

// Action type
CONST OBSERVER_TYPE_REGISTER = 1; // register
CONST OBSERVER_TYPE_EDIT = 2; // Edit

/**
* Append the observer.
* @ Param SplObserver $ observer
* @ Param int $ type observation type
*/
Public function attach (SplObserver $ observer, $ type)
{
$ This-> observers [$ type] [] = $ observer;
}

/**
* Remove the Observer
* @ Param SplObserver $ observer
* @ Param int $ type observation type
*/
Public function detach (SplObserver $ observer, $ type)
{
If ($ idx = array_search ($ observer, $ this-> observers [$ type], true ))
{
Unset ($ this-> observers [$ type] [$ idx]);
}
}

/**
* Notify the observer when conditions are met
* @ Param int $ type observation type
*/
Public function notify ($ type)
{
If (! Empty ($ this-> observers [$ type])
{
Foreach ($ this-> observers [$ type] as $ observer)
{
$ Observer-> update ($ this );
}
}
}

/**
* Add a user
* @ Param str $ username
* @ Param str $ password
* @ Param str $ email
* @ Return bool
*/
Public function addUser ()
{

// Execute SQL

// The database is successfully inserted.
$ Res = true;

// Call the notification Observer
$ This-> Policy (self: OBSERVER_TYPE_REGISTER );

Return $ res;
}

/**
* Edit user information
* @ Param str $ username
* @ Param str $ password
* @ Param str $ email
* @ Return bool
*/
Public function editUser ()
{

// Execute SQL

// The database is updated successfully.
$ Res = true;

// Call the notification Observer
$ This-> Policy (self: OBSERVER_TYPE_EDIT );

Return $ res;
}
}

/**
* Observer-send email
*/
Class Send_Mail implements SplObserver
{
/**
* Change information of the Observer
* @ Param SplSubject $ subject
*/
Public function update (SplSubject $ subject)
{
$ This-> sendMail ($ subject-> email, $ title, $ content );
}

/**
* Send an email
* @ Param str $ email address
* @ Param str $ title: email title
* @ Param str $ content
*/
Public function sendEmail ($ email, $ title, $ content)
{
// Call the Mail interface to send emails
}
}
?>

Related Article

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.