tutorial on the observer design pattern

17
Observer Design Pattern

Upload: prodigyview

Post on 28-Jun-2015

1.226 views

Category:

Technology


1 download

DESCRIPTION

Learn how to implement the observer design pattern.

TRANSCRIPT

Page 1: Tutorial On The Observer Design Pattern

Observer Design Pattern

Page 2: Tutorial On The Observer Design Pattern

OverviewObjective

Learn how to use the observer design pattern.

Requirements

Basics of ProdigyView

Estimated Time

8 minutes

www.prodigyview.com

Page 3: Tutorial On The Observer Design Pattern

Follow Along With Code Example

1. 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

Page 4: Tutorial On The Observer Design Pattern

What Are Observers

Observers are a design pattern, most commonly used in event driven programming, where an object has a list of observers and the observers are notified of a state change.

A simpler explanation would be when a function is executed, a list of other functions is automatically executed.

Page 5: Tutorial On The Observer Design Pattern

Observers Visual

Method doSomething executes. Notify

observers execution has taken place.

MyObject::doSomething

Output

Is aware that doSomething has

executed

Is aware that doSomething has

executed

Is aware that doSomething has

executed

DifferentObject1

DifferentObject2

DifferentObject3

Page 6: Tutorial On The Observer Design Pattern

PVPatterns and PVStaticPatterns

The classes that contain the methods for using observers is the PVPatterns and PVStaticPatterns classes.

PVPatterns is for instances and PVStaticPatterns is for static methods.

Both PVObject and PVStaticObject extend the pattern classes.

Page 7: Tutorial On The Observer Design Pattern

Let's BeginIn 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

Page 8: Tutorial On The Observer Design Pattern

Messenger ObjectNow we are going create a class that has the ability to send messages to objects that are observing this method.

Extends PVObject

The name of the event that observrs look for Value passed to objects that are observing

Implements the ability to call observers

Page 9: Tutorial On The Observer Design Pattern

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.

Page 10: Tutorial On The Observer Design Pattern

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.

Page 11: Tutorial On The Observer Design Pattern

Result

The result here will be very simple.

Page 12: Tutorial On The Observer Design Pattern

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 =>

Page 13: Tutorial On The Observer Design Pattern

Attach the Observers

Event Name Class to call Method in class to callCall an instance of an object

Event Name Class to call Method in class to call

Page 14: Tutorial On The Observer Design Pattern

And the output….

Page 15: Tutorial On The Observer Design Pattern

Challenge!

Below is an optional challenge to perform that is designed to help you gain 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::writeMethod

5. Execute PVSession:writeCookie() or PVSession::writeMethod and print out the results in the class you created.

Page 16: Tutorial On The Observer Design Pattern

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 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.

Page 17: Tutorial On The Observer Design Pattern

API ReferenceFor a better understanding of the Collections and the Iterator, check out the api at the two links below.

PVStaticPatterns

PVPatterns

www.prodigyview.com

More Tutorials

For more tutorials, please visit:

http://www.prodigyview.com/tutorials