Php Tutorial: Introduction to php design patterns-observation patterns

Source: Internet
Author: User
Chapter 9 of the introduction to PHP design patterns Chapter 9 observation patterns some object-oriented programming methods provide a capability to build complex network interconnection between objects. When objects are connected together, they can provide services and information to each other. Generally, when the status of an object changes, you still need to be able to communicate with each other. But for various reasons, you may

Chapter 9 of the introduction to PHP design patterns Chapter 9 observation patterns some object-oriented programming methods provide a capability to build complex network interconnection between objects. When objects are connected together, they can provide services and information to each other. Generally, when the status of an object changes, you still need to be able to communicate with each other. But for various reasons, you may

Chapter 9 observation modes of PHP design patterns

Some object-oriented programming methods provide a capability to build complex network interconnection between objects. When objects are connected together, they can provide services and information to each other.

Generally, when the status of an object changes, you still need to be able to communicate with each other. But for various reasons, you may not be willing to make major changes to the Code because of changes in the Code environment. Maybe you just want to improve the communication code based on your specific application environment. Or, you just want to re-construct the communication code to avoid the dependency and dependency between classes.

Problem

How do you notify other objects when the status of an object changes? Do you need a dynamic solution ?? Is it a solution that allows a free connection like a script execution?

Solution

The observation mode allows an object to focus on the states of other objects. In addition, the observation mode provides an observation structure, or a subject and an object, for the observed object. The subject, that is, the observer, can be used to contact all the observers who observe it. The object, that is, the observer, used to accept changes to the status of the subject.

Observation is the collaboration between a class that can be observed (that is, the topic) and one or more classes that observe it (that is, the object. No matter when the status of the observed object changes, all registered observers will be notified.

The observation mode is separated by the observer (subject) from the observer (object. In this way, each observer can perform their own operations based on the changes of the subject. (The observation mode is the same as the Publish/Subscribe mode, which effectively describes the interaction between objects .)

The observation mode is flexible and powerful. For the observer, the additional resource overhead of the classes that need their own status information to be queried and used each time does not exist. In addition, an observer can register and cancel the registration at any appropriate time. You can also define multiple observation classes to perform different operations in actual applications.

Instance code

For example, you can use the observation mode to create a more flexible handle for recording errors for your PHP script. Because the default error record handle may only display some error information on the screen, but the enhanced handle can also write the error information into a log file, you can also write the error information into the system log, send the error information by email, or report the error information by voice. You can even construct a type of error reporting solution that only allows reporting to observers who have registered specific error information. From general warning information to serious error information such as database failure information can be reported.

Next, we will use the observation mode to create a series of classes for PHP to implement the functions just mentioned. Create a new class named ErrorHandler, which is the subject of the observation mode, that is, the observer. Create another two classes named FileErrorLogger and EmailErrorLogger, which are the object of observation (I .e. the observer ). The FileErrorLogger class writes error information to the log file, and the EmailErrorLogger class sends error information by email. In UML, it can be expressed as follows:


Click to zoom in

[1] [2] [3] [4]

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.