Observer pattern

M

mem

Class subject
{
private:
List<Observer*> *_observers;
....
};

Why use a pointer, _observers, above?

Is it really necessary to have Subject and Observer base classes in the
pattern? Instead, just pass a pointer of a class into another, and the
latter just informs the former by calling its member function, whenever
needed?

How to assign _observers to a List object?

Thanks!
 
J

John Harrison

mem said:
Class subject
{
private:
List<Observer*> *_observers;
...
};

Why use a pointer, _observers, above?

Why indeed? I can't think of a good reason.
Is it really necessary to have Subject and Observer base classes in the
pattern? Instead, just pass a pointer of a class into another, and the
latter just informs the former by calling its member function, whenever
needed?

How would you deal with pointers to different classes? How you you write a
list that can take an arbitary pointer and call use that pointer to call
specific member functions.
How to assign _observers to a List object?

That question makes no sense to me. Do you mean 'how to add an observer to
the _observers list?'. The answer to that would be use the appropriate
method of the List class.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,696
Latest member
BarbraOLog

Latest Threads

Top