Lecture thumbnail 0:00 / 0:00 The observer design pattern is all about this idea of being informed when something happens.
So, for example, let’s suppose you have a person, so you have a class called person and a person
might fall ill.
And whenever a person falls ill, you might want to call a doctor.
So what the dot net framework does is it introduces this idea of an event.
And an event is a special construct which you can add to this particular class in order to have the
class inform anybody who’s interested in listening that the person has fallen ill.
So typically, the way you define an event is using the event keyword.
So you say public event, and then you specify the signature of the event handler.
So for example, I can say that I’m going to have an event handler, which just takes an ordinary event
args, and this is going to be an event called Fall Zero.
Okay, so what do we have?
We now have an object called person, which we can create here.
So var person equals new person like so, and then we can say that we want to be informed of situations
where the person falls ill so that we can call a doctor.
So here we can say Person dot falls ill and then we can use the plus equals operator to add an event
handler for this particular event.
So here are a couple of options.
You can create a lambda or you can create a separate method.
So I can go ahead and make a method using Resharper.
And here is my new method.
So whenever a person actually falls ill, then we call this particular method.
I may as well rename it to something more palatable, like call doctor, for example.
Okay, so this is how you actually subscribe to an event.
Now the question is who actually fires this event and where?
So, for example, let’s suppose a person I don’t know catches a cold, for example, catch a cold like
so.
So how do we actually form this event?
How do we actually call on this event to inform everyone who is listening to this event that we’ve now
caught a cold?
Well, we do this by invoking something directly on the event.
So we take falls ill, and then there are a couple of options.
And first of all, you have to realize that if there are no subscribers to the event, then what happens
is you get a null reference exception if you just do a dot here.
So if nobody has subscribed to this event, you’re going to get a null reference exception.
As a result, we typically do safe calls, so we put question mark dot and then we do the invocation.
Now, as you can see, there are lots of ways of actually calling something on an event, and those
ways typically depend on what kind of signature the event actually has.
So in my case, what I can do, for example, is I can call invoke.
Now let’s take a look at the invoke arguments.
Typically there are two arguments and you’re going to see this pattern all over the place.
So whenever you have an event handler, there are typically two arguments.
The first is a reference to who exactly generated this event, the sender.
So if you call invoke on a particular event, the first argument you specify is this because obviously
you are the one who is actually generating the actual event here.
And the second is a set of arguments.
Now the dot net framework kindly asks you that all of the event arguments that you use should inherit
from event args.
So here what I can do is I can say eventargs dot empty, which basically says there is no additional
information about this event.
But what you can do is you can inherit from event args to actually, you know, provide additional information.
So for example, I can say, for example, I’m going to have a class called false ill event args.
And here you might have, for example, the address of the patient so that the doctor knows where to
go.
So it’s going to be a public field here.
So what you would do instead of passing event args now is first of all you modify the signature.
So instead of using event args, you use a descendant of event args like false ill event args false
ill event args.
And then of course you have to have the same thing in here.
And then instead of specifying event args or empty, you can actually instantiate this false event args
like so.
And you can also specify the address.
So for example you can say address equals 123 London Road for example.
Let me put this into a separate line.
Okay, so this is what we have.
Let’s close all the brackets and this is how you would effectively inform somebody that a person has
fallen ill, providing the additional information.
And here inside the handler, what you can do is you can write line, for example, certain information
about the fact that a doctor has been called to this address.
So you say a doctor has been called to and then specify event args dot and let’s actually do string
interpolation event args dot address in here.
So this is how you actually handle the event with the additional argument.
And so what we can do now is we can simulate the fact that a person catches a cold.
So we invoke this method which generates the event and we actually invoke on the event.
So let’s take a look at what this generates.
Hopefully it should be fairly obvious what is the output here because we’re just subscribing to an event
and getting the output the appropriate output.
So as you can see, a doctor has been called to 123 London Road.
Now, similarly, once you’re not interested in listening to events anymore, you can use the minus
equals operator person dot false, ill minus equals call doctor.
Now there are really unpleasant rules if you, for example, plus equal a construct more than once and
then minus equal with this whole approach is not particularly stable.
But the event infrastructure in Dotnet is good enough to handle events generated in UI, for example.
So when you work with Winforms or WPF, you basically subscribe to an event like you have a button and
the button has a clicked event and that’s what you subscribe to to actually handle that particular event.
So I guess what we could summarize this particular clip with is by saying that the observer design pattern
is actually incorporated directly into the infrastructure of the net framework using this idea of events.
So essentially when you want to inform somebody that something is happening in your system, you declare
an event and you specify its signature in terms of what kind of arguments it actually yields.
You give it a name which can subsequently be referred to whenever somebody needs to actually subscribe.
And then of course, when you want to fire, when you want to invoke this event, there are lots of
ways of doing it.
For example, here I’m doing the synchronous invoke call.
There is also begin, invoke and end invoke.
If you want to do it asynchronously, you have to check for Null.
So if there are no subscribers to the event, then you have to check for Null because otherwise you
will get a null reference exception.
But you call false little question mark dot invoke and you specify typically two arguments.
The first argument is who generated this event?
And the second argument is any additional information that you are prepared to offer about this particular
event?
Play Stop Play Play Play Play Play Play Start Play information alert