PHP Design Pattern Learning (singleton mode and observer mode)

Source: Internet
Author: User

1. Single Case mode

A few days ago with a friend to discuss the time, a friend raised a question, why all classes do not use single-case mode, all using singleton mode is really the best?

Whether the new object or the static variable in PHP is valid only for this request, all objects and variables will be destroyed when the request is completed.

There is no difference between singleton mode and direct new when an object is used only once per request.

On the other hand, when an object involves more property modifications or settings and needs to be used more than once, many times we may prefer to initialize each time rather than using an object that does not know what is being done.
In conclusion, the singleton model is more suitable for objects that require multiple uses of a single request and do not involve excessive property modification.

2. Observer mode

The Observer pattern is useful when a state changes, and several other places need to react differently to this change. It conforms to the principle of interface isolation and realizes loose coupling between objects.

PHP SPL has already provided the Splsubject and Sqloberver interfaces, respectively, to write both the Observer class and the observer class to implement both interfaces.

Below is a demo:

    Class Subject implements Splsubject {private $observers;                  Public function Attach (Splobserver $observer) {if (!in_array ($observer, $this->observers)) {              $this->observers[] = $observer; }} Public Function detach (Splobserver $observer) {if (false! = ($index = a              Rray_search ($observer, $this->observers)) {unset ($this->observers[$index]); }} Private Function Notify () {foreach ($this->observers as $observer              ) {$observer->update ($this);          }} Public Function SetCount ($count) {echo "Data volume Plus". $count;          } Public Function Setintegral ($integral) {echo "bonus". $integral; }} class Observer1 implements Splobserver {public function Update ($subject) {$subject, setcount (1);              }} class Observer2 implements Splobserver {public Function update ($subject) {          Setintegral, $subject (10);               }} class Client {public function test () {$subject = new subject ();              $subject->attach (New Observer1 ());              $subject->attach (New Observer2 ());   $subject->notify ();//output: Amount of data plus 1 points plus 10}}

The Observer pattern is more suitable for situations where a subject needs to dynamically add attributes or other related items, such as a swich operation, when the switch needs to increase the variety frequently, and you do not want to affect the original code, you can consider the observer pattern (an inappropriate example). And as the subject changes, you want the subject-related objects to make changes accordingly, you can use the Observer pattern

Events are more like the inverse of the Observer pattern: different operations in multiple places need to trigger the same action event, and the event is buried multiple points, triggering a point. And the observer is burying a point, triggering a multi-point change.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

PHP Design Pattern Learning (singleton mode and observer mode)

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.