Tutorial On The Observer Design Pattern


Published on

Learn how to implement the observer design pattern.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Tutorial On The Observer Design Pattern

  1. 1. Observer Design Pattern
  2. 2. OverviewObjectiveLearn how to use the observer design pattern.Requirements Basics of ProdigyViewEstimated Time8 minutes www.prodigyview.com
  3. 3. Follow Along With Code Example1. Download a copy of the example code at www.prodigyview.com/source.2. Install the system in an environment you feel comfortable testing in.3. Proceed to examples/design/Observers.php
  4. 4. What Are ObserversObservers are a design pattern, most commonly used inevent driven programming, where an object has a list ofobservers and the observers are notified of a statechange.A simpler explanation would be when a function isexecuted, a list of other functions is automaticallyexecuted.
  5. 5. Observers VisualMyObject::doSomething Method doSomethingexecutes. Notify observersexecution has taken place. Output
  6. 6. PVPatterns and PVStaticPatternsThe classes that contain the methods for using observers is thePVPatterns and PVStaticPatterns classes.PVPatterns is for instances and PVStaticPatterns is for staticmethods.Both PVObject and PVStaticObject extend the pattern classes.
  7. 7. Lets 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
  8. 8. Messenger Object Now we are going create a class that has the ability to send messages to objects that are observing this method. Extends PVObjectImplements the ability to call observersThe name of the event that observrs look for Value passed to objects that are observing
  9. 9. 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().2. The method notify has the parameter new_message. This is the name of the event that is going to cause the notifications. After the event name, we can add as many parameters as we want but in this example we are only adding one, the message.
  10. 10. Round 1The first test we are going to do is just sending a messagewithout adding an observer. So lets initialize the objectand send a message.
  11. 11. ResultThe result here will be very simple.
  12. 12. Round 2Now lets attach our observers. At minimum, the observerrequires 3 arguments. The first argument is the name of theevent. Our event name has to match to event name set inthe notifier, which is new_message. The second argumentis the class to be called and the third is the method in theclass to be called when the event is executed The last isoptions, namely for if our method is not static, apply theinstance’ option here.Code example on next slide =>
  13. 13. Attach the Observers Call an instance ofEvent Name Class to call Method in class to call an objectEvent Name Class to call Method in class to call
  14. 14. And the output….
  15. 15. Challenge!Below is an optional challenge to perform that is designed to help yougain a better understanding of the design pattern.1. Look through ProdigyView’s source code and find the PVSession class.2. Find the event name for either writing a session or writing a cookie.3. Create a class that accepts the same parameters that the method in the Session class outputs.4. Add that class as an observer to PVSession:writeCookie() or PVSession::writeMethod5. Execute PVSession:writeCookie() or PVSession::writeMethod and print out the results in the class you created.
  16. 16. Summary1. 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 that’s are listening.2. Attach an observer with _addObserver. Make sure to specify the name of the event, the class and method the even will be calling.
  17. 17. API ReferenceFor a better understanding of the Collections and theIterator, check out the api at the two links below.PVStaticPatternsPVPatterns More TutorialsFor more tutorials, please visit:http://www.prodigyview.com/tutorials www.prodigyview.com