10.12.2012 Views

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Figure 8-2. Observer pattern class diagram<br />

the observer from the notification process. In fact, every single Subject method references<br />

the Observer. Likewise, the Observer interface, with its single update( ) method,<br />

references the notification process from the subject. The ConcreteSubject provides a<br />

property, stateWork, for holding the state about which the observers are notified. It is<br />

also responsible for notifying the subscribed observers. Finally, through the update( )<br />

method, the ConcreteObserver keeps a state value consistent with the subject, and,<br />

through a state property, stores the current state.<br />

Key OOP Concepts Used with the Observer Pattern<br />

One object-oriented design principle put forth in the original design pattern book by<br />

Gamma et al is to favor object <strong>com</strong>position over class inheritance. That is, when possible,<br />

design your programs using object <strong>com</strong>position instead of beginning with a<br />

superclass and then ac<strong>com</strong>plishing reuse through inheritance. To understand the reason<br />

for this dictum, we need to look at object <strong>com</strong>position.<br />

To appreciate the advantage of object <strong>com</strong>position over inheritance, we need to<br />

understand another principle put forth by Gamma, Helm, Johnson and Vlissides:<br />

Program to an interface, not an implementation.<br />

Let’s first look at why programming to an interface is favored over programming to<br />

an implementation.<br />

Choose the Interface<br />

Subject<br />

‹‹interface››<br />

Subscribe Observer<br />

Unsubscribe Observer<br />

Notify Observer<br />

ConcreteSubject<br />

Subscribe Observer<br />

Unsubscribe Observer<br />

Notify Observer<br />

state work<br />

observers<br />

Observer<br />

‹‹interface››<br />

Update()<br />

ConcreteObserver<br />

Update()<br />

subjects<br />

In a nutshell, according to Gamma et al, programming to an interface is better than<br />

programming to an implementation because it reduces implementation dependencies<br />

between subsystems. This increases software flexibility. As a result, changes can<br />

be made at runtime.<br />

The idea of programming to an interface does not literally mean an <strong>ActionScript</strong> <strong>3.0</strong><br />

construct interface as opposed to a class. That is, you can program to the interface<br />

Key OOP Concepts Used with the Observer Pattern | 285

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

Saved successfully!

Ooh no, something went wrong!