1

ok, so I am stuck at the observer pattern here, almost all tutorials I read tell the subject class to subscribe the observer(s).

But with encapsulation in mind, how is this not tighlty coupled? They still depend on each other know, don't they?

What I mean to say is that the subject Class must know the observer object to add him to the list of objects to notify.

Therefore a dependency is created, right?

What's the mistake I make?

Thanks!

Thanks everybody for the replies,

Now I have some new questions. If I understand correctly the best way to deal with this is with Interfaces. So I will do that ;)

But, Why are the always talking about delegates and events? events ARE a form of delegates. So why aren't they just saying events?

1
  • It is not a good idea to change the question or ask new questions after 8 answered were already posted. It is difficult to follow the discussion that way. To answer your questions: It is not true, events are not delegates. Event Handler passed to the event are delegates, which are called when the event is fired. You can use interfaces or events to implement the observer pattern. Commented Oct 1, 2010 at 10:15

7 Answers 7

4

When you say "know", you're right that the publisher must know about the observer in order to publish information to it.

However, it doesn't need to "know" about it in the sense that it is hardcoded that:

  • The observer is always going to be this particular class
  • There is always going to be these particular observers available

In its basic form, events are publisher/observer in play, so you can easily do this just with events:

public class Observer { } public class Publisher { public event EventHandler SomethingHappened; } 

You would then make the observer handle that event:

public class Observer { public Observer(Publisher pub) { pub.SomethingHappened += Publisher_SomethingHappened; } private void Publisher_SomethingHappened(object sender, EventArgs e) { } } public class Publisher { public event EventHandler SomethingHappened; } 

Whenever this event is raised from the publisher, the observer is informed about it. Realize that the act of hooking into an event is to "tell" that class about the observer, but the publisher doesn't have any hardcoded information about the publisher, except that there is someone out there listening.

A different way would be using interfaces:

public class Observer : IObserver { public Observer(Publisher pub) { pub.Observers.Add(this); } void IObserver.SomethingHappened() { } } public class Publisher { public List<IObserver> Observers { get; private set; } } public interface IObserver { void SomethingHappened(); } 

Again, the publisher will "know" about the observer in the sense that it has a reference to it, but again it has no hardcoded information about which class or how many instances there will be.

Just a word of warning: The code above is very flawed, at a minimum you should ensure that the observer "unhooks" from the publisher when you're done, otherwise you're going to have leaks in the system. If you don't understand what I mean by that, leave a comment and I'll edit in an example.

Sign up to request clarification or add additional context in comments.

2 Comments

What u mean with unhooks, is in syntax the -= right? Otherwise the same object will be hooked twice and the code will run twice. Am I right about that?
Yes, you need to do the same as when you hooked onto the event, only with the minus sign instead, -=, otherwise there will be left a reference back to your object from the publisher.
2

The class that is Observable can accept interfaces in the observe method. This interface is defined in the library where the subject Class is defined and then implemented by the subscriber. That way, the classes only know what they are supposed to know.

Comments

1

An observable object in C# is an object that declares one or more events. One or more observing classes might or might not subscribe to these events at runtime. The observable part does not know and does not care.

The observed class does not have to maintain a list of objects to be notified. It just has to fire an event and otherwise is totally agnostic about who is listening.

So there's no dependency whatsoever from the observed class to the observing one. Only the observing one has to know about the events that it can observe.

Thomas

Comments

0

I dont really understand you. But if you are worried about Observer pattern in C#/.NET. Then developers at Microsoft already solved all your problems in form of events.

Comments

0

I think you have read this slightly wrong, yes, the subject does subscribe the observer but it doesn't initiate the subscription, i.e. MySubjectClass.Observers += MyObserverClass;

By using an interface to define the contract between the Subject and the Observer you allow the Observer to be any class that implements the interface.

So you can see this is not tightly coupled, i.e. the Subject isn't instantiating concrete Observer classes.

Comments

0

All the observer object and the observed object know is that they are interacting with an IObservable and an IObserver object respectively. The exact type of these objects is irrelevent to them - they only care that they implement the IObserver and IObservable interfaces.

Comments

0

You ask "Whats the mistake I make ?" . Your following line is where I think you went wrong:

What I mean to say is that the subject Class must know the observer object to add him to the list of objects to notify.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.