02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Composite Design Pattern

The composite design pattern enables us to control multiple elements at the same time (that have the same public API. This is very useful in situations when we need to control a large amount of items and want to have them react together to an action.

Video Transcript

00:00The next design pattern that we're going to be confronting is the composite design pattern and as the composite name implies
00:07It basically is a design pattern then it is intended to use
00:11multiple objects at the same time that share the same API
00:15The idea is when we need to control multiple objects at the same time
00:19We could create a composite that would then control all these different items at the same time
00:25now this is really ideal in scenarios where you have some sort of recursiveness and
00:31that is exactly what jQuery does when you're using a selector and then changing a color and
00:38Selecting a hundred different things as you're changing those colors
00:41So the idea is really to treat everything as if it's the same thing and if they're all coming in at the same time
00:47Then you could then control them all and do the exact same action on all of them
00:51So let's jump right into the composite design pattern
00:55lecture, I can't think of something more fun to create than a composite design pattern now
01:03Let's just do a recap. We have your circles that have the functionality of being colored moved and
01:09Being gotten now so do's does the rectangle have the same thing and probably any other object that we create in the future
01:17So now that we have that established
01:19Wouldn't it be great if we could control multiple elements at the same time?
01:23Just like jQuery does it where if you send into the dollar?
01:27Element there and you want to let's say select every single div or you want to select every single item and do to all
01:33Those items the same thing that's really the idea behind a composite controller and a composite literally as name implies
01:42As a composite is of a lot of objects. So let's go ahead and start building our composite. It's gonna be really fun
01:48So I'm gonna go right in the bottom there right after we create our stage
01:51Just before we start creating our singleton and I'm gonna go ahead here and just create a new function
01:57And I'm gonna call it composite
01:59Controller and it's gonna accept an array now
02:02The array itself is gonna list and hold inside of it all the different items that we're bound to control
02:08And I'm gonna create a really really advanced composite controller instead of creating a composite controller that controls our shapes
02:16I'm gonna create a composite controller that controls anything to do that
02:20I'm gonna add an a prototype method to our composite controller and I'm gonna enable it to basically control anything
02:28but this leaves a lot of
02:30Room for error. So I'm gonna do this even though that I'm doing this even though probably in a real-world scenario
02:36If you're gonna distribute something like this, you wouldn't want users to have so much control
02:41The reason is because they might call actions that don't exist, but I am fine with that
02:46All right
02:47So I'm gonna assume that the user is smart enough to send the actual action they need and that the action exists
02:52Just like we have with maybe jqueries CSS where the user could call different CSS properties
02:58But if they call a CSS property that doesn't exist, well, boo-hoo. It just won't work
03:02Okay
03:03so the next step that I need to do is to be able to go through and loop through every single item in that array and
03:09Call that action that we're creating
03:12Okay, so let's go through that
03:13So I'm gonna go ahead and gonna create a for loop item in this study and what do I want to do?
03:19I want to basically broach that item this study
03:22Item and I want to call the action. So I'm gonna use the bracket
03:27I'm gonna use the brackets to be able to enter the actual name of the function that's being sent
03:31And then I'm gonna go ahead and actually call it now
03:34I can't just call it directly because some actions might have arguments some actions won't
03:39So really what I need to do is I need to gain access to our arguments
03:44Variable that exists inside of every single function. Now, I can't send the arguments
03:51Arguments just as is which it already exists. It has a list
03:54It's a pseudo array a kind of array, but it doesn't have the functionalities of an array that stores
03:59All the arguments are inside of a function
04:01For me to take advantage of this. I need to basically convert this arguments into a real array. So to do that
04:08I'm going to call the array prototype
04:10Slice and I'm gonna call that slice forcing it to basically generate a new array from the arguments method that we have
04:19Returning a real array that has all the functionalities of array and why do I need those functionalities?
04:24That's because I need to get rid of the first element inside of that array shift
04:29And the reason is because the first action is right here already listed
04:33So I want to get rid of that item all together leaving only the arguments that actually exist
04:38Now that I've done that I'm ready to actually go here and call my function
04:42But instead of directly calling it
04:44I'm gonna set an apply here and apply accepts two parameters every function has an apply method attached to it
04:50It accepts two parameters. The first is going to be the scope of the function. I really don't want to change the scope
04:56So I'm gonna go ahead there and just send the actual current scope of that object directly to it as its first parameter and the second
05:04parameter is going to be that args which are going to be the arguments that it might be expecting if the array is empty then it
05:10Just won't send arguments, which is exactly what we would hope to do and I've just created now our first compose it
05:16Controller now this seems all nice and dandy, but let's see it in action
05:21Where does this become really handy to be able to take it and make it really actionable?
05:26We're gonna have to go inside of our inside of our singleton and literally go to our singleton and create one more variable
05:33So I'm gonna go ahead there and I'm gonna call this composite
05:37controller and I'm gonna go ahead and create a new composite controller and send into it our array now because I'm just sending a reference to
05:45The array that array will grow and expand as new items are added and that's exactly what I want to happen
05:51Beautiful. So now that I have a composite controller all that's left for me to do is maybe add another method inside of our
05:58inside of our
06:00Singleton here that would control all elements at the same time. So let's go ahead and do that
06:04I'm just gonna add your another let's do this right after the ad or right after the create
06:09I'm gonna create call a function and I'm gonna tint elements whenever a color is sent all the items should be tinted
06:15Now let's go ahead and do that controller
06:18Action what do we want to call?
06:20We want to call the color and we want to send in that color that we want to perform
06:24Literally what I'm doing here is I'm calling the action
06:27Defining it with the method of color and then sending the property that that color method is expecting
06:33Now that I've done that all that's left for me to do is actually test this out by going into
06:37Let's go ahead into our actual application itself and add maybe another controller besides our keypress of a let's go ahead and add another control
06:46How about we'll add here a T?
06:49So I'm gonna go ahead and add that so let's do that else if eat key equals equals equals T for tint
06:56And let me just make sure that that's equally equals
06:58We don't need it to be a kind of it needs to be exactly that value
07:01That's reducing our which makes our code a little bit more optimized
07:05all that's really left for me to do is to call that our
07:10Circle generator and I'm gonna go ahead there and I'm gonna tint it sending in black
07:14Oh, and one last thing is we have to expose that tint to make it in the public interface
07:18So let me jump right up there and set the tint into the interface
07:23That's being sent out as well inside of our singleton
07:26All right
07:26Now that we've done that it's time for us to test things out
07:28And if I go ahead here and click on a couple of areas here and press on the T
07:33Then they're all going to be tinted to black beautiful
07:36Let's continue on now besides those tints that we've just created what I want to do next is I want to also add a little
07:42Bit more functionality and I'm gonna do it relatively quickly
07:46One thing that more thing that I want to add is in our in our controller right now
07:50We have the position but it's private and do we have a public interface for our position? Let's see. Do we have a public?
07:57There are where are we? No, we there is no public visibility to our
08:03For our position now our position right here only approaches one circle at a time and that's fine
08:08We're gonna keep that private. But what I want to do next is right after that tint
08:12I want to add another thing that would control everything and I'm gonna go to that function and I'm gonna call it
08:17And let's call it move to simulate the same signature that we would expect in our overall arching architecture
08:25Architecture and then we had their left and we had their top. I believe that was the order
08:30Let's double-check. I would actually left and
08:34Was it left and right nose left and top
08:37Left and top. Yeah, there we go left and top. All right, so we create the function
08:41What do we want to do with it? Same kind of deal
08:43So we're basically just gonna call the call the controller ask for it to control all the move
08:49Properties and move on and send out so that left and top positions
08:52that's literally all we really had to do and what we're doing here also by
08:57Explicitly naming it is putting here a little bit more control and then putting that personal ization
09:02Which is probably needed in our case
09:03But then still being able to use that composite controller to any application because this is not specifically
09:10Personal to this specific application. This would work in any composition really and we could use this generically to any place where we want to composite things
09:19Okay. So now that we've added this controller of moving
09:22Let's go ahead and also add that into our library into the public interface
09:26I'm gonna call it move and the move will actually move every single thing
09:31So let's go ahead and add a few more else if conditions here
09:34So I'm gonna just paste that right there and the next key that we want to approach is
09:39We're gonna approach the keyboard arrow
09:42Right and in the case of arrow, right? What do we want to do? We want to call the move
09:47well, we want to call the move command inside of our
09:51inside of our circle generator and
09:55Besides do that. What do we want to do? We want to send the positions of the top and the left
09:59So the first one is the left and we want to push it to the right by 5 pixels
10:03So I'm gonna go ahead and add here a jQuery command, which will push the item by 5 pixels and I'm gonna
10:10Plus 0 px just to make sure that my item does not move on this side at all
10:15and I'm gonna go ahead and also do the exact same thing for the left arrow and
10:23We'll make that a minus instead of a plus and hopefully if everything worked out if I go ahead here and click on
10:29Refresh we should see that when I add the items now if I click on T
10:34They're gonna be tinted and I'm late
10:35Let me add a couple more after the tint happened
10:37And if I click on my keyboard on to the left and right arrows
10:41You'll see that my items are gonna move right and they're gonna move left and it would be a great idea if you also
10:46Add the up and down controls and I'm gonna leave that to you and that is our composite design pattern
10:52I want to leave you off with a little bit of homework
10:54The composite design pattern was built to to be recursive and in our case
10:59We're not really building it completely recursively because if I now go ahead and add with my keyboard the items you'll see notice that my
11:06Object here actually has a circle and a square
11:09We built them together adding a square into our circle
11:12If I go ahead into this item and I try to tint everything notice that only the original items will be tinted
11:18But their children are not going to be tinted
11:21The reason is is because we never really created a composite
11:25Construct when we were creating those rectangles a really long time ago in a previous section
11:31Your job if you choose to take that is to go back into that build circle blue circle rectangle
11:37and maybe get rid of the rectangle that we were adding and instead of that create maybe a circle square or
11:44art exhibit element builder that would take a composite of different items and build them together and
11:50Have a controller within it that will enable whenever we move an item there. It would move all them whatever we're tinting
11:55It would move tint all of them
11:57So that's your homework assignment and it's based on what we just learned right now with the composite design pattern in this lecture
12:06We met the composite design pattern and in the next one
12:09We're gonna visit the decorator and see when would that be actually useful?

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!