Now we are going to continue our discussion of the observable.
But now what I'm going to do is I'll make a small digression because instead of showing you something
that we've built ourselves what I want to show you is the observable implementation that comes with
the Boost libraries.
So we're going to build another version off person and this person is going to basically implement a
different kind of interface which is based on boost signals so boost signals and specifically boost
signals to is a library which is one of the best libraries specifically for implementing the observed
design pattern via the use of signals and slots.
So this terminology basically means that you have a signal as a variable whose value you can subscribe
to and subsequently this variable can actually fire.
And every single subscriber which in this case is called a slot can get notifications.
So I know this sounds cryptic Let me show you how this would work so that would make a new template
type name struct.
I'm going to call it observable too.
So this is going to be specifically for a boost and instead of building all the infrastructure that
we've built so far you just make a single signal.
So a signal and here you have to specify the function signature.
That's going to be invoked in the signals so here will have a function which returns void and it has
two arguments a reference to the object that changed and once again the name of the field inside an
object that's changed and will code feel changed.
So that is our signal and what we can now do is we can build a new class that's called Person 2 which
is going to implement this observable to all of us too.
And what we can do here is we can reconstruct the age property in a similar fashion but this time around
that we can use something that's ready made.
So here is the private field and then we'll have public getters and setters actually generate them as
before.
And here I will go ahead and I will once again modify the set or so if this age is equal to age then
return.
And then this age equals page.
And here is the interesting part so here is where we can invoke on that signal.
So here we have a signal called field changed and we can notify all the subscribers all the slots in
the terminology of signals and slots all the slots will get notification that a field has changed.
To do this we just called field changed.
We specify a reference to the object could change in this case that's us and the name of the field is
age here.
So the question is Well how do you subscribe to this pair of them.
How do you actually start monitoring the changes in here.
Let's take a look at that.
So let me get rid of everything here and now make person to P2.
And then I will make an observer.
Now notice I did not make a new observer interface I made an observable but I didn't make an observer.
And the reason for that is that you don't really need it.
Once you have the signal what you can do is you can subscribe directly to that single with a lambda
function for example so that's what we're going to do.
So I will have auto icon and that's the connection the subscription.
In other words equal to P2 dog field.
Daut connect.
So this is how you can actually create your own slot so your own subscription to whatever event is actually
being generated.
So here you can provide a lambda I'll just provide Lamda which takes person to reference called P and
the field name that's changed.
And here is the body of the lambda.
So here I'm just going to see out that field name has changed.
That's all I really want to do here.
And then having constructed this connection what we can do is we can start changing the age and seeing
the notification so I can say P2 that set age to 20 for example.
Now another interesting thing about using Boost signals is you have this connection object which kind
of describes the subscripted that you've just made and what you can do is you can disconnect from it.
So here I can say con Daut disconnect and this this is the equivalent of unsubscribing to the particular
observable.
So that's actually run this and see how a boost allows us to implement the observable design pattern.
All right so as you can see if we get another notification here that h has changed so that's precisely
what we wanted.
And this basically shows that instead of building your own observable you can use one that's already
packaged in a library.