Chain of Responsibility - Part 2
Now that we have the logic needed to create chains, in this lecture will implement the chain design pattern.
Video Transcript
00:00Alright, so let's continue building the chain of responsibility design pattern.
00:05Now that we have the chains ready, I want to talk a little bit further about the concept
00:09of the chain of responsibility.
00:11The idea is to have a decentralized logic making.
00:15Each chain is deciding.
00:18Does it implement itself?
00:19Does it carry through and call the next chain or not?
00:23Each link can be changed as well.
00:25So I could go ahead if I had a chain, imagine a chain of 100 items, I could disconnect the
00:30link and reconnect it in a completely different place.
00:33I could even create a web in many ways with these chains.
00:36I could connect two chains to the same spot where from the same spot it will move to the
00:40same direction.
00:42Chains are incredibly powerful and you can create with them a lot of really interesting
00:46things.
00:47Even a loop can be created with them.
00:49So think about it.
00:50A chain right now we're creating it in a linear way to help see the logic.
00:54But when you start exploring and playing with this, go ahead, create really intricate chains
00:59because that could create very unique animations that will probably be impossible to do with
01:04a centralized design.
01:07So let's jump right into it.
01:08Let's continue building our chain of responsibility.
01:13In the last lecture, we created the next and chain do and added them into our core prototype
01:18shape.
01:19The next step is we want to go ahead into our singleton.
01:23Go ahead and find the create function which is deep, deep inside the create method and it's
01:28somewhere down right about here, beautiful.
01:34So we're inside the create.
01:35What we want to do now is every time we add a new circle in, we want to go to the last
01:39circle that was added in and chain it together.
01:43One way we could do this is by storing a reference to the last item that was added
01:47and chaining them together.
01:48But instead of doing that, what I'm going to go ahead and do is I'm just going to
01:51go ahead and get here a reference to the last index of the last item using the a circle
01:58dot length minus one.
02:03Now if the value is minus one, that means we don't really want to do anything.
02:07So I'm going to go ahead here and just check that if our index equals equals minus one,
02:13well actually if it does not equal minus one, then we're good to go.
02:16So only if it's not equal minus one, what do we want to do?
02:19Let's go ahead and go to that a circle and I'm going to go into that index and I'm going
02:24to go ahead and call that next function that we've just created, the next method, and I'm
02:28just going to assign this new circle we created to it, creating the first key chain.
02:33Now I want to talk about this just really briefly, that a chain right here is very easily
02:38disconnectable.
02:40So if you connected 20 chains and then in the middle just disconnected one of the chains
02:44and decided to move it elsewhere, nothing will happen to the other chains.
02:47You could literally disconnect and reconnect chains very easily.
02:51Where does this become really, really handy?
02:54For example, an ATM that's dispensing cash has a hundred dollar bill sector of $50 or $20
03:01and coins and so forth.
03:02And each sector is responsible for its responsibility.
03:07Instead of having one manager managing all the different sectors, it makes a lot more
03:11sense to bestow the ownership one after each other.
03:14So going to the hundreds, giving all the hundreds, moving to the 20s and down until all the payment
03:20has been completed, making sure that we're always giving the highest bills, alright?
03:24That's a good example of that.
03:26But even more common example, you'll find an animation, a lot of Disney movies and so
03:30on and so forth where you see hair waving in the air.
03:33Not the all time illustrated but 3D animations where you see hair particles moving and it
03:38looks so natural.
03:39Why does it look natural?
03:41Because it's not controlled by one central area.
03:43But instead of that, there are chains of responsibilities.
03:47An animation is called kinetic animation where elements are connected to each other and affecting
03:52each other.
03:53So those effects of one item affecting another creates this ripple which makes it look very
03:58organic and very natural because the items are really reacting to each other in a chain
04:02event, okay?
04:05And so that's it about that, a little bit of theory.
04:07Let's continue down the line of creating our element.
04:10So we went ahead and we made sure that we connected the item only if it was in existence.
04:15The next step for us now that we have a chain is to actually go ahead and create a function
04:20that would actually take advantage of the fact that we have a chain there.
04:23So I'm going to go ahead and create a chain tint function which is going to accept a
04:27count and the idea behind here is to basically tint around the last five elements or whatever
04:32amount of elements the user decides to do.
04:34I'm going to go ahead here and just define an index and to do that, I'm going to go
04:38to my a circle dot length and literally what I want to do is I want to go back the count
04:43amount of time so I could color all the items that we just finished.
04:47But I need to make sure that the value won't be under zero so I'll set here a math dot
04:52max and make sure that my value will be at least zero.
04:55Okay, so now that we have an index, the next thing I want to create is a randomly
04:59assigned color and to do that, I'm going to use strings to create a hex value and
05:04I'm going to go ahead and just take math random, duplicate the random value that I
05:09get by 255 which is the color ranges that I have in RGB, red would have from zero to
05:14255. Now that I have this range, I'm going to just go ahead here and make sure that
05:19I'm getting the floor, the lowest number that is an integer, so the
05:24highest integer number that I will never get 255, I will get a value between zero
05:29and 254 which is exactly what I want. The next step after that I'm going to go
05:34ahead and just turn this into a string by calling it a two string method but I
05:37don't want to get a value that's actually 255 as a string, I want to get
05:41the hex representation of it so I'm going to put it as the parameter 16
05:45inside of my two string. That's all I really needed to do and I'm going to go
05:48ahead here and just duplicate this three times for my red, green, and blue so I'm
05:52going to have completely random values inside of my colors. That's it, I already
05:57have now a color, all that's left for me to do to start to trigger that
06:01chain event is to go to that item in my index that I decided that I'm going to
06:05attack or I'm going to trigger and I'm going to call that chain do and our
06:10chain do expects the first parameter to be what are we trying to achieve, okay so
06:15we just told it. The next is we're going to tell it what color we want and as we
06:20said we're going to send it as an array and then last but not least I'm
06:24going to send the count which is the count that the user defined. That's it,
06:27that's all I really needed to do. All that's left for me to do to test things
06:31out and see that it's working is I need to expose that chain tint in my
06:36singleton. I'm going to go to the return around line 220 and I'm going to go
06:41ahead and expose that method so I could then approach it inside of my
06:45application, inside of my implementation area roughly around
06:50242 and I'm going to go into that click event after we add an item and I
06:55want to go to the CG into my singleton item I'm going to do a chain tint and I'm
06:59just going to say hey I want you to do five which actually means six as we said
07:02previously. So let's go ahead and test things out let's see if we have any
07:05errors first of all so I'm going to go ahead here and just click on refresh
07:08and if I click here we'll see no errors okay that's a good sign and if
07:12as I add items in notice how the colors are changing and once we cross
07:16the six items you'll see that only the last six items are going to animate
07:20each time and change and color. Speaking of animate how about we animate
07:24this really quickly let's just do that very very quickly let's go right
07:28back up there to our chain do and what I want to do instead of calling
07:32this directly I just want to create a very quick animation so I'm going to go
07:36ahead here and set timeout and I'm going to create your function and I'm
07:41going to wrap up everything that we have inside there into this timeout
07:45function and I'm going to go ahead and set it to be a relatively quick
07:51animation of 20 milliseconds but before I send this to this won't work anymore I
07:55could send it as a parameter or I could take advantage that we created in the
07:59last section the binder function so I'm going to go ahead here before we even
08:03call the function I'm going to set a binder say keep the this the way it
08:07was so that way we could continue referring to this inside of our set
08:11timeout and if I go back right into my application and click on refresh this
08:16time around as I add items in they're going to animate their color in which is
08:21kind of groovy you must admit so that's it we're done with this lecture and we're
08:27done with the chain of event and make sure you look at the source file if
08:32anything is unclear please feel free to comments and me emails but we're going to
08:35move on in the next lecture to a completely new design pattern in the
08:39world of behavioral design patterns that's it we're done creating the
08:45chain of responsibility design pattern and it's time for us to move to the next
08:49lecture because in the next lecture we'll meet the observer design pattern
08:53see you there
