ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Key OOP Concepts Used with the Observer Pattern | 285

the observer from the notification process. In fact,every singleSubjectmethod refer-


ences theObserver. Likewise, theObserverinterface, with its singleupdate( )method,


references the notification process from the subject. TheConcreteSubjectprovides a


property,stateWork, for holding the state about which the observers are notified. It is


also responsible for notifying the subscribed observers. Finally, through theupdate( )


method, theConcreteObserverkeeps a state value consistent with the subject, and,


through a state property, stores the current state.


Key OOP Concepts Used with the Observer Pattern


One object-oriented design principle put forth in the original design pattern book by


Gammaet alis tofavor object composition over class inheritance. That is, when possi-


ble, design your programs using object composition instead of beginning with a


superclass and then accomplishing reuse through inheritance. To understand the rea-


son for this dictum, we need to look atobject composition.


To appreciate the advantage of object composition over inheritance, we need to


understand another principle put forth by Gamma, Helm, Johnson and Vlissides:


Program to an interface, not an implementation.

Let’s first look at why programming to an interface is favored over programming to


an implementation.


Choose the Interface


In a nutshell, according to Gammaet al,programming to an interface is better than


programming to an implementation because it reduces implementation dependen-


cies between subsystems. This increases software flexibility. As a result, changes can


be made at runtime.


The idea of programming to an interface does not literally mean an ActionScript 3.0


constructinterfaceas opposed to aclass. That is, you can program to the interface


Figure 8-2. Observer pattern class diagram


Subject
‹‹interface››
Subscribe Observer
Unsubscribe Observer
Notify Observer

Observer
‹‹interface››
Update()
observers

ConcreteSubject
Subscribe Observer
Unsubscribe Observer
Notify Observer

ConcreteObserver
subjectsUpdate()

state work
Free download pdf