OK so having defined our observer interface which is very simple because in this particular case all
we're interested in is getting the notification of changes in fields what we're going to do is we're
going to define yet another interface and that interface is going to be observable.
That's the one that person should implement.
So let's go ahead and do exactly that.
Let's have observable.
So the observable interface is a bit more interesting because we have lots of things to cover here so
let's go through it one by one so the idea of the observable interface is that once again this is going
to be a template
and once again he is the type of object that's being observed.
So what we're going to be doing is using the curiously recurring template pattern.
So class person is going to actually implement observable office and we need the appropriate answer
for this one.
Of course there we go OK.
So coming back to observable that HP What kind of operations do we want on an observable.
First of all we want a list of all the subscribers we want a list of all the observers which are attached
to this particular observable.
So let me include a couple of things here
and I will have a vector.
All this observer off to.
Let's have.
Observers.
So we need a forward declaration.
I don't know if the line is going to offer us.
No.
So let's just add one in here we need a forward declaration for the observer.
But apart from that we are good to go.
So this is the set of subscribers which want to get notifications about some changes in the observable.
And then we will have a couple of public public members which are going to allow us to actually manipulate
the observable.
So first of all we want to implement a function called notify which is going to inform all the observers
that some change has been made.
And here once again we will specify that we need to provide the source of the change as well as the
field name that has in fact changed since we're only concerned about changes in fields at the moment.
So we go through each of the observers
and for each of the observers we actually fire the notification so we say observer field changed we
specify the source as well as the field name.
That's all that we need to do for the notification.
And then of course we want to be able to subscribe and unsubscribe to this particular are observable.
So for the SUBSCRIBE function we will just take observer off t as reference.
I'm just making the interfaces more usable this way and we push back a we have to push back appointer
because we're keeping a vector of pointers as you can see here.
So I'll just take the address of that reference.
So observers got pushed back and then and then there is also the unsubscribe question on Subscribe once
again providing a reference to the observer.
So in order to unsubscribe we'll use the erase remove it.
So I'm going to take all the observers.
I'm going to say observe is the author race.
And then I will remove the observer from observers that begin observers and Observer.
And then here I will observe as Dot.
And so this is the classic phrase remove idiom and it will allow us to remove the observer from the
set of observers.
So having defined this observable interface we can now go back into our main OCP.
And this is where things become valid so we have this observable interface in person and as a consequence
what we can do is we can rewrite the implementation of the Satir to take advantage of what we've just
done.
So essentially we now have in the boat in not just to set the age but to also inform somebody of the
changes.
How would we do this.
Well first of all we will check that we have in fact made a change because if we're not changing the
age if this age is equal to this age there is no need to do anything.
There is no need to perform any notifications.
Certainly So what we'll do here is will say if this age is equal to age simply return then will set
the value of this age equals age and then we'll perform the notifications so notify star this and the
name of the field here is age.
So having said all of this up we can now put the observer and the observable together connect them using
the SUBSCRIBE function and see some results.
Finally OK.
So let's make a person see a 10 and I will make a console plus an observer CPO.
And then what I'm going to do I'll connect them together so I'll those say person dot subscribe and
I will subscribe the console first and observer here.
Let me just check what's going on here.
We don't seem to have a constructor yet.
Let's make a constructor which actually initializes the h.
So constructor we go.
OK so coming back here when are ready to set the age of person and get those notifications.
So person Daut set page 11 and 12.
But we can also do is at some point we can unsubscribe so we can say person.
Unsubscribe I can unsubscribe CPO and now if I said PHN Once again let's say 13 and we shouldn't be
able to get any printouts because we are no longer monitoring that particular object.
So let's actually run all of this and see what kind of output we get here.
All right.
So as you can see we're getting the correct results here so we're getting persons age change to 11 changed
to 12 but we don't get the change to 13 because remember by that time we've unsubscribed from the observable.