Академический Документы
Профессиональный Документы
Культура Документы
Overview
Objective Learn how to use the observer design pattern.
Requirements
Basics of ProdigyView
Estimated Time
8 minutes
www.prodigyview.com
3. Proceed to examples/design/Observers.php
Observers Visual
MyObject::doSomething
Output
Let's Begin
In our example, we are going to pretend you want to integrate a function with social media. So lets start by making two social media classes. Keep note that in our example, one of our classes has a static method.
The argument passed from the method observed
Messenger Object
Now we are going create a class that has the ability to send messages to objects that are observing this method.
Extends PVObject Implements the ability to call observers
Take Notice!
1. Our class extends PVObject. PVObject extends
PVPatterns which has our methods needed to use the observer. The method that will notify other methods than an action has occurred is _notify().
Round 1
The first test we are going to do is just sending a message without adding an observer. So lets initialize the object and send a message.
Result
The result here will be very simple.
Round 2
Now lets attach our observers. At minimum, the observer requires 3 arguments. The first argument is the name of the event. Our event name has to match to event name set in the notifier, which is 'new_message'. The second argument is the class to be called and the third is the method in the class to be called when the event is executed The last is options, namely for if our method is not static, apply the 'instance option here. Code example on next slide =>
Event Name
Class to call
Challenge!
Below is an optional challenge to perform that is designed to help you gain a better understanding of the design pattern.
3. Create a class that accepts the same parameters that the method
in the Session class outputs.
Summary
1. Add _notify to a function and set the event name. Add
as many parameters as you need after the event name. Parameters will be passed to the functions thats are listening.
API Reference
For a better understanding of the Collections and the Iterator, check out the api at the two links below.
PVStaticPatterns
PVPatterns
More Tutorials
For more tutorials, please visit: http://www.prodigyview.com/tutorials
www.prodigyview.com