Lecture thumbnail 0:00 / 0:00 So one of the first things that we need to discuss when discussing reactive extensions is the observer
design pattern.
Now, I’m sure many of you are thinking, well, why are we discussing observer?
After all, the c sharp language incorporates the observer design pattern using the event keyword and
the support for events.
So why are we discussing this?
Well, you are correct in the fact that the event keyword is in fact how you implement the simple observer.
But whenever you actually go ahead and you implement the observer design pattern in Dotnet, it becomes
a bit more complicated than you want it to be.
So for example, if you think about, let’s say the market, so you’re simulating some kind of market,
maybe a stock market, that’s something let’s suppose that the market just has a single value to keep
track of.
Let’s suppose that there is some volatility value, so you have private float volatility and that’s
the volatility of the entire market as a whole, so called volatility.
And this value can change.
And as a result, using the observer design pattern, you might want to be informed of situations where
the market volatility changes.
So the implementation of something as simple as this already requires some heavy plumbing, because
in the net framework, the way you do change notifications is using the interface called I notify property
changed and this interface can be automatically generated.
If you’re using writer or resharper, you get your function for actually firing the event for actually
doing the invocation.
In addition, you have the event itself right here.
And of course now what you can do is you can generate a property which maps your field and also does
the change notification.
So in this case, you would go maybe into properties, you would choose volatility here, you would
make sure to notify on property changes and this would get you a property which wraps the field and
also does on property changed in the setter.
So this is how you implement the observer design pattern just for a single property.
And then of course, what you can do is you can make the market.
So you can say var market equals new market.
And then of course you can listen to changes of the volatility.
So you can say, please tell me about all the property changes.
So you can say market dot property change dot or rather plus equals.
And here you can provide a lambda which actually gets informed of properties that are being changed
in the market.
So here you can for example, say if the name of the property is actually volatility.
So if Eventargs dot property name is equal to volatility, then yes, you can perform some sort of actions
depending on the volatility as it’s being changed.
So this is a simple illustration in fact, of how you would track the changes in a single field or a
single property.
But this is of course, not what we’re here to talk about.
We’re here to talk about sequences of values and speaking about the market, you know, the market tracks
prices of different securities.
If you think of a single security, then you may as well have a single sequence of prices that you might
once again want to track changes to.
And the question is, well, how do you do it?
If there is a price which keeps changing, how do you actually track the values as they kind of come
in from somewhere else?
Now here you can go ahead and do the same thing that we’ve done so far, which is just making a list.
So instead of a single value, you would maybe say private list of float prices equals new, do a new
list of float.
And then of course, you would have some sort of method for adding a price to the market.
So you would have float price here and you would simply say prices dot add price.
Now the problem with all of this is, of course, that you need to somehow get notifications of new
prices being added.
So what do you do?
Well, if you are going with the classic net theme, you would make an event, you would go ahead and
you would make a public event event handler of float, maybe called price added.
And then of course, whenever you add a price, you would also invoke this event.
So you would say price added dot invoke, you would specify the sender as this and then the price as
the actual value.
So this gives you a way of telling whoever is consuming the market object that the price has been added.
And then here what you can say is you can say market price added plus equals, and once again, you
can generate a lambda which does something.
In this case, let’s just write line something.
So we got a price of and then F, which happens to be the price.
So this is how you would subscribe to the event.
And then of course if you post a new price on the market 123 then you should be able to use the event
handler and actually see the output in the command line.
And that’s exactly what we see right here.
So it is working.
Everything is fine, but.
There are actually smarter containers inside the.
Net framework which allow you to do away with all of these notifications because the notifications for
those containers are built in.
And one such example is a binding list.
So if I change this from a list to a binding list like so, I no longer need to have an event.
I can get rid of this event altogether because a binding list not only keeps a list of values, but
it also does the notifications automatically so that I don’t have to do them myself, which is really
convenient and I can still subscribe to them.
So let me show you how this is typically done.
I’m going to make this public like so.
And also I’m going to rename this to prices with a capital P now that it’s public.
There we go.
So what I can do here is I can say market prices dot and then you can see that there are lots of things
here because it’s not just an ordinary list, it’s a binding list which can notify us of things.
So here I can say binding list changed and here I can once again handle this particular event.
And this event occurs whenever the list actually changes.
And we when we talk about changes, it’s not just items being added, it’s also items being removed
or items being modified.
So all of these can be handled in this lambda that I’ve made.
And inside this lambda I can first of all check that what I’ve just done is in fact an addition of a
new element.
So I can say if event args dot list change type is equal to and you can see some of the options here.
So you can track items being added, changed, deleted or moved.
I’m going to say add it here.
So if an item has been added, I can get the price that has in fact been added.
So I can say float price equals and then to get the actual price, first of all, I have to cast the
sender to the list of float.
So I have to cast it to a binding list of float like so sender.
But then I can use an indexer and the index of the element that’s just being added is in event args.
So we say event args dot new index and that’s how you get the index of the element that’s just been
inserted effectively.
So here we can once again Console.writeline we can do something else, we can say binding list got a
price of and then add the price here.
And once again, if I execute this I get the correct value.
So this looks pretty good.
It looks like we don’t really have to do anything for implementation of the observer pattern on sequences.
Well, unfortunately there are a bit a few problems here and there.
So for example, we never know when we’re out of items.
So if you think about some external system which is feeding your prices at the moment in the binding
list paradigm, and indeed if you’re using an ordinary list, you would never be able to find out when
you’ve run out of items.
Now the question is, well, when do you run out of items?
Well, in the case of the market, for example, the stock market is actually closed on weekends.
So on weekends you might want to send some signal from the market that there’s going to be no more items
coming in.
And that way, whoever is tracking these items can, for example, perform some processing on all of
the items collected so far.
So that’s one issue that is not addressed in ordinary list containers.
Another issue is what if something goes wrong on the market?
What if the automated trading bots go insane and they crash the market and the market is no longer able
to supply reliable prices?
So it throws an exception.
Well, once again, we cannot support this unless we make additional events.
So of course you can take the market and you can make an event called Market Crash, or you can make
an event called, for example, out of prices or market closed.
And you can listen to all of these events.
But what reactive extensions does is it kind of incorporates information about sequences, including
their termination right inside the concept of observers and observables.
Now, these concepts aren’t particularly difficult because what you have is you have the market, which
is some observable collection, observable, and then you have somebody who is actually investigating
this market.
And this happens to be the observer.
So the Observer subscribes to certain events which happen on the observable and it can actually process
those events.
Now of course, in our example, we’ve been using the event keyword explicitly, we’ve been using binding
list, which also fires off events.
Now in the case of observer and observable, it’s a fairly similar story, not quite the same.
And that’s what we’re going to take a look at.
And in fact, this is what this entire course happens to be all about.
Play Play Play Play Stop Play Play Play Start Play information alert