03.09.2015 Views

Design Patterns

Download - Assembla

Download - Assembla

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 15 ■ THE OBSERVER PATTERN 223<br />

When Should the Observer Pattern Be Used?<br />

The observer pattern should be used in situations where you want to abstract human behavior<br />

from application behavior. It’s best not to implement something that is tied to user interaction and<br />

originates from the browser, such as basic DOM events like click, mouseover, or keypress. None of<br />

these events are useful pieces of information to an implementer who simply wants to know when<br />

an animation begins, or when a word is spelled incorrectly in a spell-check application.<br />

Let’s say for example that when a user clicks a tab in a navigation system, a menu with more<br />

information about the tab is toggled. Granted, you could simply just listen for the click event, but<br />

this requires knowing which element to listen for. There is another downside: you’ve now tied<br />

your implementation directly to the click event. Instead of listening for the click event, it would<br />

be better to simply create an onTabChange observable object and allow observers to be notified<br />

when the particular event occurs. Since these tabs could in fact be toggled on mouseover or even<br />

on focus, this is something that the observable object would take care of for you.<br />

Benefits of the Observer Pattern<br />

The observer pattern is basically a great way to maintain your action-based applications in<br />

large architectures. In any given application you may have tens, hundreds, or even thousands<br />

of events happening sporadically throughout a browser session. Furthermore, you can cut<br />

back on event attachment and allow your observable objects to handle the actions for you<br />

through one event listener and delegate the information to all its subscribers, thus reducing<br />

memory and speeding up interaction performance. This way you don’t have to constantly add<br />

new listeners to the same elements, which can become costly and unmaintainable.<br />

Drawbacks of the Observer Pattern<br />

One downside to using this particular observer interface is the cost in load time when setting<br />

up the observable objects. You can mitigate this by using a technique called lazy loading,<br />

which basically allows you to put off instantiating new observable objects until delivery time.<br />

This way, subscribers can begin subscribing to an event that has yet to be created, avoiding<br />

slowing down the initial load time of the application.<br />

Summary<br />

The observer pattern is a great way to abstract your applications. You can broadcast events and<br />

allow any developer to take advantage of subscribing to those events without ever having to dig into<br />

the other developers’ implementation code. Five people can subscribe to the same event, and five<br />

separate events can all be delivered to the same subscriber. In an interaction environment such as<br />

a browser, this is ideal. As newer, larger web applications are being built, adding observables into<br />

your code base is a great way to keep your code maintainable and squeaky clean. They discourage<br />

third-party developers and coworkers from digging into the guts of your application and possibly<br />

messing things up. Impress your friends and managers by putting the observer into practice.<br />

In the publisher utility, we developed a “push” system where the publishers broadcast an<br />

event by pushing data to each of their subscribers. Try seeing if you can write a utility that allows<br />

each subscriber to “pull” data from each of its publishers. Hint: You can try starting with a pull<br />

function as a subscriber method that takes in a Publisher object as an argument.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!