views:

39

answers:

2

It is said here http://msdn.microsoft.com/en-us/library/ee817669.aspx#observerpattern_topic3a

This permits a subject to notify a potentially infinite number of observers of state changes, rather than just one.

Why can't I just use

list<Observer> 

in the subject class instead of delegating to a container ?

+2  A: 

Your list<Observer> collection is the container that the article refers to. It is an implementation detail whether you chose a list, a map, a set, etc. to hold the set of Observers. It really is up to you what to use. The pattern won't change depending on the type of container used.

rsp
Typically and unless the OP has very special needs, in Java observers are indeed done using a *List<Observer>* whose not-so-detail implementation is typically a "COWAL" (CopyOnWriteArrayList). It's not that much a detail for obvious synchronization and performance reasons (as explained in *Java Concurrency In Practice*).
Webinator
+1  A: 

Your List data structure is a container :)

That sentence is simply distinguishing between maintaining a List<Observer> data structure (for example) and a single Observer reference.

EDIT: Looks like I was beaten to the punch

Nathan
Well I thought container was meaning something more sophisticated :)