PHP design pattern Four (Observer prototype pattern iterator mode)

Source: Internet
Author: User

Observer pattern

The Observer pattern (sometimes called the publish-subscribe subscribe> mode, model-view view> mode, source-listener listener> mode, or slave mode) is one of the software design patterns. In this mode, a target object manages all the observer objects that are dependent on it and proactively notifies when its own state changes. This is usually done by calling the methods provided by each observer. This pattern is often used to implement an event-handling system.
When the state of an object changes, all dependent objects are notified and updated automatically. The observer pattern realizes a low-coupling non-intrusive notification and update mechanism.

Example of an observer pattern:
First create an event generator eventgenerator.php

<?phpnamespace Components\observer;abstract class Eventgenerator {Private $obServers = array (); function addobserver (IObserver $obServer) {//Add event $this->observers[] = $obServer;} function Notify () {foreach ($this->observers as $obServer) {//One by one perform update $observer->update ();}}}

Create an Observer interface iobserver.php
<?phpnamespace components\observer;interface iobserver {function update ($event _info = NULL);}

Event Body:
Class Event extends \components\observer\eventgenerator{function Tringger () {echo "<br>Event<br/>"; $ This->notify ();}}

Observed by:
Observer 1class ObServer1 implements \components\observer\iobserver {function update ($event _info = null) {echo ' logic1< Br> ";}} Observer 2class ObServer2 implements \components\observer\iobserver {function update ($event _info = null) {echo ' logic2< Br> ";}}

Trigger:
$event = new Event (), $event->addobserver (New ObServer1 ()), $event->addobserver (New ObServer2 ()), $event Tringger ();



prototype Mode

Similar to Factory mode, used to create objects, the prototype schema creates a good one prototype object, creates a new object through the Clone prototype object, avoids repetitive initialization when the class is created, and is typically used for large object creation, eliminating the repetition of each new consumption, but only the memory copy.
The main problem is the creation of "some complex objects", which often face drastic changes due to the change of requirements, but they have more stable and consistent interfaces.

Example code:

/* Prototype mode */class Bigclass {function init () {;} function exec ($tpye) {echo "prototype$tpye<br>";}}  /** * Prototype user creation of large objects Save resources */$prototype = new Bigclass (); $prototype->init (); $bigClass 1 = Clone $prototype; $bigClass 2 = Clone $prototype; $bigClass 1->exec (1); $bigClass 2->exec (2);


iterator Mode

Iterates through the inner elements of an aggregated object without needing to know the internal implementation. The iterator pattern hides the actions required to traverse elements compared to traditional programming patterns

Iterator Example useriterator.php

<?phpnamespace components\iterator;use components\register;use Components\factory;class UserIterator implements \ Iterator {protected $ids;p rotected $data = Array ();p rotected $index; function __construct () {$db = Register::get (' slave ') ; $result = $db->query (' Select id from users '); $this->ids = $result->fetch_all (MYSQL_ASSOC);} 1function Rewind () {$this->index = 0;} 2function valid () {return $this->index < count ($this->ids);} 3function current () {return Factory::getuser ($this->ids[$this->index][' id ');} 4function Next () {+ + $this->index;} 5function key () {return $this->index;}}

Use:
/* Iterator mode */$users = new Useriterator (), foreach ($users as $user) {var_dump ($user);}


PHP design pattern Four (Observer prototype pattern iterator 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.