that looks like i already saw that in the package structure
also like our pattern like model view and controller and we
got all this for our little counter exactly so we have an
observer it's of type observer but actually it's a view
okay and we have a countercontroller which is
our controller and then we have the counter and
probably you already noticed while we had the program running
we have something that counts clicks and then
translates the data that is collected by counting these clicks
into the position of the square of the square
and that's basically what the program does so
we define them up here and then in the setup method
we actually get a new instance of all these
of all these classes so we we instantiate the objects here
and what we also see is that we've got some yeah
inheritance between them or some dependencies between them
so our view in this case is an observer
and that our counter so the model is passed to the constructor
of the controller and that the view
also gets passed in this case our main app
this is necessary for processing but it's also gets passed the
actual model so we already see the dependencies here
exactly and this maybe is worth
one more sentence so asRalf already said we passed
TheApp here
a reference to TheApp so that in each of the views
we can we can kind of refer to these methods that come with
processing otherwise that wouldn't be possible
or we would need to kind of extend the PApplet
with each of these views and that's
something we don't want to do we want maintainable code and this
is a way to do it let's step into the
model view controller of thing a little but more in order to better
understand and where should we start i would say let's
start right here with the controller so
whenever somebody clicks on the window
it will call the handleevent method of the controller so
this is pretty general and we can dive right into it so when
we go to the countercontroller and the countercontroller
pretty short so not much things yeah it says counter.setcount
counter.setcount plus one that's not so quite nice i would
rather prefer to move the logic i mean this is basically the
logic that we have here the getcount plus one
i would prefer to move that to the model and we
will do that and in a second
but let's go through it first it basically
just has a reference to the counter and it
manipulates the counter so it interacts with the model
exactly so let's go to the counter and here we see ok
this is a list of observers that we talked about in the we
first see that it's a subject it's a subject
it implements subject as you remember maybe from the from the
observer pattern that we've shown
in one of the videos before in the last video
so what does the subject do i want to see that
probably first i'm curios yeah you can have that actually
you basically you see it already here so i see two of the methods
but is that all? it's all
the subject asks the counter that it has to have at least
the attach method and the notifyallobservers method so with the attach method
we can register also with the notifyallobservers
we can notify all observers and then there might also be
for example a detachment method if we want to get rid of observers
this is also sometimes called observable
as an interface because we normally want to call interfaces
with -able we talked about it in the java course
and then the counter would implement an observable for example
we just named it subject here because
we took that from the design pattern
so what we see here in the counter again so it overrides these
methods and with the attach method it basically just
adds the observer that we pass that's one of our views
to this list of observers
that we have here so it's an arraylist because it needs to continually
grow whenever we have this new thing that is interested in
what we are doing here and wants to observe us
we want to edit and we don't want to have fixed side and
so we're using an arraylist and then we have the
next method that is overwritten and this is the notifyallobservers
can you be just like new through the observer list
and call the update method of the observables and as you probably
might notice we don't as an argument here
so we will use the pull on way of accessing this data and we
will kind of that's one of the things i want to do here is
where we will changed at and kind of switch to a
push method ok later and now we see also the
set count and get congress it's yeah and so z so logic which
is happening there were less well actually the actually logic
as as we just had is happening here in this line in the control
and that's something that we want to do we want to move this
to the model that rather the logic is happening
here because we can set something and if it is set we notify
all observers so yeah exactly so this is the least important
god notify the observers whenever something that
good and finally what did without we have not
seen if you spend about so on
we have to count of you with extensive observers so as i've
probably i would say i want to see the observer hertz
this is
the observer it provides and a reference to the display this
is where we passed the this from the at two different we can
there is all the processing things with
which come with a with a p applet and then we have to construct
of the observe and in the construct of the observer because
the p after that's what we give over here
so we just say meant nothing happened and then we have to subject
and this is actually the thing that is going to be observed
by count up here in this case and we say subject attach this
so this is the attachment that that we just had in the subject
on it called and we attach this also have a to do list
of observers for the model and this is a
pretty much bet whenever you find something like an observer
model it will be constructed like this so if
something is different you should be curious about it but if
it looks like this it's just a plain observer
so and then we have an african method update
and this will be implemented than in the endangered
and this can be in the implemented differently in
several views and seven reveal several reviews can watch the
same thing and rick such observe deserted later
and we already prepared a couple of all lines is always so
i said we want to move the on the logic to the
control to the model and we want to on switch from
pull to push as a first let's see the view not yeah
so we've got the counter we want to see if it's ok and so when
we when we have the constructor we put the counter into
this like we know first we call super and this is so to just
once repeated we already know what super just from that of
course is that we call the constructor authors so so superclass
this is the observer and this is what we just have so long
as the displayed collected that here and then you have to subject
to list in and if we go there so the first
thing if we create a kind of view as we attach
this kind of you to the counter and then we also save a reference
to the counter and then we update
with your salary up through the lesser it's because
we don't have any data from the model yet so we're going to
retrieve it by calling update
what else do we need are we i
that's something that we should talk about
so this display this is kind of the pf that that we've been
passing around her own tell you
and now on this display we can call all
what is it provided by processing so the food method and to
set the color or we can call that background method to set
back on color we can call the wreck method to
to to correcting and here you can see we have
cooling wrecked it's a rectangle we should maybe say that yeah
rectan so and here we pull the data from
the object from from the model to to use it here
and then these are just fix values and then we call the
redrawn method of the display so as we don't
have something in the draw method in the initial problem of
the we need to call this re-draw method so that
we see something you see something that's
that it's ok so what should we do first remove
the kala and get rid of the reference look on and
used to push yeah probably based sourcing yes
so we'll want to we thought that when we click
our own little rectangle moves and this is done
by retrieving the count here and we when we have a higher number
in there we've got a different x position
so therefore the thing moves right yeah
and this is going to stay they regardless of what we're doing
with what the concept is so
yeah so what we will do basically is we factoring so not nothing
in the behavior will change which as we can have we organised ok