WEBVTT 1 00:00:01.360 --> 00:00:04.060 in the last video we saw how composition 2 00:00:04.060 --> 00:00:06.520 is a powerful technique for combining in the last video we saw how composition 3 00:00:06.520 --> 00:00:08.590 objects to create something more complex is a powerful technique for combining 4 00:00:08.590 --> 00:00:11.530 and we built up a HTML document from objects to create something more complex 5 00:00:11.530 --> 00:00:13.660 the three classes that it's composed of and we built up a HTML document from 6 00:00:13.660 --> 00:00:17.620 the doctype a head element and a body now the three classes that it's composed of 7 00:00:17.620 --> 00:00:20.290 aggregation is very similar and is often the doctype a head element and a body now 8 00:00:20.290 --> 00:00:22.840 described as a weak form of composition aggregation is very similar and is often 9 00:00:22.840 --> 00:00:25.960 now with composition the objects that another described as a weak form of composition 10 00:00:25.960 --> 00:00:28.450 object is composed of don't exist now with composition the objects that another 11 00:00:28.450 --> 00:00:32.320 outside of their container so in our object is composed of don't exist 12 00:00:32.320 --> 00:00:36.100 example looking down here on line 50 outside of their container so in our 13 00:00:36.100 --> 00:00:39.610 the init method for HTML doc there example looking down here on line 50 14 00:00:39.610 --> 00:00:42.460 instances of a doctype which is created the init method for HTML doc there 15 00:00:42.460 --> 00:00:45.070 in a HTML doc init method on line 51 as i instances of a doctype which is created 16 00:00:45.070 --> 00:00:47.829 mentioned and a head and a body created in a HTML doc init method on line 51 as i 17 00:00:47.829 --> 00:00:50.980 on the next two lines so instances of mentioned and a head and a body created 18 00:00:50.980 --> 00:00:53.559 all three classes exist but they only on the next two lines so instances of 19 00:00:53.559 --> 00:00:56.800 exist within the context of our HTML all three classes exist but they only 20 00:00:56.800 --> 00:01:00.700 doc object my_page now if we exist within the context of our HTML 21 00:01:00.700 --> 00:01:02.920 delete my_page then those doc object my_page now if we 22 00:01:02.920 --> 00:01:04.989 three instances will no longer exist delete my_page then those 23 00:01:04.989 --> 00:01:08.080 either now in our program we've three instances will no longer exist 24 00:01:08.080 --> 00:01:09.940 indicated at the three instance either now in our program we've 25 00:01:09.940 --> 00:01:12.729 variables self dot underscored doc _ indicated at the three instance 26 00:01:12.729 --> 00:01:15.760 type self._head and variables self dot underscored doc _ 27 00:01:15.760 --> 00:01:18.250 self dot _ body are quote unqote type self._head and 28 00:01:18.250 --> 00:01:19.479 private self dot _ body are quote unqote 29 00:01:19.479 --> 00:01:22.840 now as we learned previously this is python private 30 00:01:22.840 --> 00:01:24.369 and so they're not really private as now as we learned previously this is python 31 00:01:24.369 --> 00:01:27.010 such but there should be treated as if and so they're not really private as 32 00:01:27.010 --> 00:01:29.500 they are now this program was written in such but there should be treated as if 33 00:01:29.500 --> 00:01:31.720 java for example there'd be no way to they are now this program was written in 34 00:01:31.720 --> 00:01:33.939 change those fields from outside the java for example there'd be no way to 35 00:01:33.939 --> 00:01:36.760 HTML dot class now I mention that change those fields from outside the 36 00:01:36.760 --> 00:01:38.530 because we can change this program to HTML dot class now I mention that 37 00:01:38.530 --> 00:01:41.320 use aggregation without changing the because we can change this program to 38 00:01:41.320 --> 00:01:44.320 HTML dot class in Java for example the use aggregation without changing the 39 00:01:44.320 --> 00:01:45.970 code will have to be written a bit HTML dot class in Java for example the 40 00:01:45.970 --> 00:01:48.820 differently so in Python the distinction code will have to be written a bit 41 00:01:48.820 --> 00:01:51.220 between aggregation and composition can differently so in Python the distinction 42 00:01:51.220 --> 00:01:54.909 blur slightly in Python focus more on between aggregation and composition can 43 00:01:54.909 --> 00:01:57.220 the intent rather than what's possible blur slightly in Python focus more on 44 00:01:57.220 --> 00:01:59.470 with language that's because in Python the intent rather than what's possible 45 00:01:59.470 --> 00:02:01.630 just about anything is possible with language that's because in Python 46 00:02:01.630 --> 00:02:04.509 alright so let's use aggregation now just about anything is possible 47 00:02:04.509 --> 00:02:07.150 to get our documents a new content then alright so let's use aggregation now 48 00:02:07.150 --> 00:02:09.429 will go ahead and rewrite the class so to get our documents a new content then 49 00:02:09.429 --> 00:02:11.680 that it uses aggregation rather than will go ahead and rewrite the class so 50 00:02:11.680 --> 00:02:13.020 composition that it uses aggregation rather than 51 00:02:13.020 --> 00:02:14.250 and then we're going to add some more composition 52 00:02:14.250 --> 00:02:16.770 code at the end of the program here so and then we're going to add some more 53 00:02:16.770 --> 00:02:20.670 right down to the bottom and let's code at the end of the program here so 54 00:02:20.670 --> 00:02:22.440 start here on line 74 we're going to type right down to the bottom and let's 55 00:02:22.440 --> 00:02:27.300 new.... start here on line 74 we're going to type 56 00:02:27.300 --> 00:02:31.140 .... new.... 57 00:02:31.140 --> 00:02:34.500 ... .... 58 00:02:34.500 --> 00:02:48.630 ... ... 59 00:02:48.630 --> 00:02:50.880 .... ... 60 00:02:50.880 --> 00:02:54.180 ... .... 61 00:02:54.180 --> 00:03:17.910 ... ... 62 00:03:17.910 --> 00:03:19.560 .... ... 63 00:03:19.560 --> 00:03:22.770 ... .... 64 00:03:22.770 --> 00:05:12.340 ... ... 65 00:05:12.340 --> 00:05:14.020 alright so you can see what we've done ... 66 00:05:14.020 --> 00:05:16.660 there we created a new instance of the alright so you can see what we've done 67 00:05:16.660 --> 00:05:18.880 body class called new_body there we created a new instance of the 68 00:05:18.880 --> 00:05:21.580 then we added to several tags to it we body class called new_body 69 00:05:21.580 --> 00:05:23.950 then assign the new_body object to our then we added to several tags to it we 70 00:05:23.950 --> 00:05:26.020 documents_body attributed then assign the new_body object to our 71 00:05:26.020 --> 00:05:28.540 directly if you run this program now documents_body attributed 72 00:05:28.540 --> 00:05:36.190 you've see we got a test 2 . HTML showing directly if you run this program now 73 00:05:36.190 --> 00:05:38.320 their now and if I right click that now you've see we got a test 2 . HTML showing 74 00:05:38.320 --> 00:05:41.500 and open in browser and select a their now and if I right click that now 75 00:05:41.500 --> 00:05:43.570 chrome in my case you select whichever and open in browser and select a 76 00:05:43.570 --> 00:05:47.080 browsers relevant you can see that we've chrome in my case you select whichever 77 00:05:47.080 --> 00:05:48.430 got the new output showing their browsers relevant you can see that we've 78 00:05:48.430 --> 00:05:52.510 let's just go back to the code now so got the new output showing their 79 00:05:52.510 --> 00:05:54.640 the point here is that if the my_ let's just go back to the code now so 80 00:05:54.640 --> 00:05:56.890 page object is deleted then the the point here is that if the my_ 81 00:05:56.890 --> 00:05:58.900 new _ body object continues to page object is deleted then the 82 00:05:58.900 --> 00:06:01.150 exist and could actually be used in new _ body object continues to 83 00:06:01.150 --> 00:06:02.980 another document if we wanted to do that exist and could actually be used in 84 00:06:02.980 --> 00:06:05.620 now as I said in python the another document if we wanted to do that 85 00:06:05.620 --> 00:06:07.780 distinction can get a little bit blurred now as I said in python the 86 00:06:07.780 --> 00:06:10.660 so what I'm going to do is change the HTML dot distinction can get a little bit blurred 87 00:06:10.660 --> 00:06:12.670 class so that it's written to use so what I'm going to do is change the HTML dot 88 00:06:12.670 --> 00:06:14.980 aggregation rather than composition class so that it's written to use 89 00:06:14.980 --> 00:06:17.020 which is what you'd have to do in aggregation rather than composition 90 00:06:17.020 --> 00:06:18.940 languages like Java if you wanted an which is what you'd have to do in 91 00:06:18.940 --> 00:06:21.040 aggregate class rather than a composite languages like Java if you wanted an 92 00:06:21.040 --> 00:06:25.000 one we go back up to the HTML . class aggregate class rather than a composite 93 00:06:25.000 --> 00:06:27.220 we defined here and what we're one we go back up to the HTML . class 94 00:06:27.220 --> 00:06:28.390 going to do first is we're going to we defined here and what we're 95 00:06:28.390 --> 00:06:33.550 remove the title and we're going to add going to do first is we're going to 96 00:06:33.550 --> 00:06:34.720 some parameters going to add the remove the title and we're going to add 97 00:06:34.720 --> 00:06:39.940 doctype the head and the body and then some parameters going to add the 98 00:06:39.940 --> 00:06:42.040 we're going to initialize them so we're doctype the head and the body and then 99 00:06:42.040 --> 00:06:44.260 going to put... we're going to initialize them so we're 100 00:06:44.260 --> 00:06:46.780 .... going to put... 101 00:06:46.780 --> 00:06:48.580 ... .... 102 00:06:48.580 --> 00:06:51.360 .... ... 103 00:06:51.360 --> 00:06:53.069 ... .... 104 00:06:53.069 --> 00:06:59.159 ... ... 105 00:06:59.159 --> 00:07:03.569 ....so now that we've done that in our HTML ... 106 00:07:03.569 --> 00:07:06.210 . class we need to provide instances ....so now that we've done that in our HTML 107 00:07:06.210 --> 00:07:09.000 of all three classes when we create a . class we need to provide instances 108 00:07:09.000 --> 00:07:12.360 new html doc object now my of all three classes when we create a 109 00:07:12.360 --> 00:07:13.830 original code that we have defined down new html doc object now my 110 00:07:13.830 --> 00:07:15.780 here for my page my _ page original code that we have defined down 111 00:07:15.780 --> 00:07:17.969 won't work now so I'm going to actually here for my page my _ page 112 00:07:17.969 --> 00:07:20.310 comment that out will comment out the won't work now so I'm going to actually 113 00:07:20.310 --> 00:07:25.080 entire bit of code there and by doing comment that out will comment out the 114 00:07:25.080 --> 00:07:26.699 that you can see these errors further on entire bit of code there and by doing 115 00:07:26.699 --> 00:07:27.779 down in the code but we can ignore that you can see these errors further on 116 00:07:27.779 --> 00:07:29.669 them for now because i'm going to go down in the code but we can ignore 117 00:07:29.669 --> 00:07:32.279 once we've created a new document them for now because i'm going to go 118 00:07:32.279 --> 00:07:35.460 so below that now i'm going to type in... once we've created a new document 119 00:07:35.460 --> 00:07:39.719 .... so below that now i'm going to type in... 120 00:07:39.719 --> 00:07:44.009 .. .... 121 00:07:44.009 --> 00:07:45.930 ... .. 122 00:07:45.930 --> 00:07:50.159 ... ... 123 00:07:50.159 --> 00:07:53.250 ...so the are only going to be ... 124 00:07:53.250 --> 00:07:55.919 executed if the if that we defined ...so the are only going to be 125 00:07:55.919 --> 00:07:59.789 on line 66 returns true but what we do need to executed if the if that we defined 126 00:07:59.789 --> 00:08:02.099 do after the new body the adding of the on line 66 returns true but what we do need to 127 00:08:02.099 --> 00:08:05.550 three tags is we need to assign new doc do after the new body the adding of the 128 00:08:05.550 --> 00:08:06.839 type so let's go ahead and do that so.... three tags is we need to assign new doc 129 00:08:06.839 --> 00:08:11.520 ... type so let's go ahead and do that so.... 130 00:08:11.520 --> 00:08:25.169 ... ... 131 00:08:25.169 --> 00:08:31.050 ...so what we should probably should ... 132 00:08:31.050 --> 00:08:32.070 have done with all these code is have ...so what we should probably should 133 00:08:32.070 --> 00:08:34.560 it indented anyway so I'm going to just go have done with all these code is have 134 00:08:34.560 --> 00:08:35.640 ahead and do that so we're going to it indented anyway so I'm going to just go 135 00:08:35.640 --> 00:08:39.329 indent to that level and ahead and do that so we're going to 136 00:08:39.329 --> 00:08:40.500 will delete the comment because we no longer indent to that level and 137 00:08:40.500 --> 00:08:42.300 need that anymore will delete the comment because we no longer 138 00:08:42.300 --> 00:08:44.760 delete the my page that's because we're need that anymore 139 00:08:44.760 --> 00:08:45.990 going to change the definition of that delete the my page that's because we're 140 00:08:45.990 --> 00:08:50.519 my page will become... going to change the definition of that 141 00:08:50.519 --> 00:08:54.449 ... my page will become... 142 00:08:54.449 --> 00:09:02.380 ... ... 143 00:09:02.380 --> 00:09:04.960 we're going to leave the width open ... 144 00:09:04.960 --> 00:09:07.300 there because that's now valid because we're going to leave the width open 145 00:09:07.300 --> 00:09:10.120 we've recreated our my_page there because that's now valid because 146 00:09:10.120 --> 00:09:12.160 variable but we will do is just change we've recreated our my_page 147 00:09:12.160 --> 00:09:13.810 the filename so its test three . variable but we will do is just change 148 00:09:13.810 --> 00:09:14.680 HTML the filename so its test three . 149 00:09:14.680 --> 00:09:16.840 it's not trying to override the previous one HTML 150 00:09:16.840 --> 00:09:19.720 we've created right so let's just open it's not trying to override the previous one 151 00:09:19.720 --> 00:09:21.340 the project pane and run to we've created right so let's just open 152 00:09:21.340 --> 00:09:26.200 confirm that it does work works ok and the project pane and run to 153 00:09:26.200 --> 00:09:29.170 got test three dot HTML so let's open in confirm that it does work works ok and 154 00:09:29.170 --> 00:09:33.250 browser open in chrome and we've got got test three dot HTML so let's open in 155 00:09:33.250 --> 00:09:36.010 working as you can see again we browser open in chrome and we've got 156 00:09:36.010 --> 00:09:37.570 don't see the title bar but we just go working as you can see again we 157 00:09:37.570 --> 00:09:38.680 back in there don't see the title bar but we just go 158 00:09:38.680 --> 00:09:40.690 view page source you can see that the back in there 159 00:09:40.690 --> 00:09:42.940 title is actually showing in the HTML view page source you can see that the 160 00:09:42.940 --> 00:09:44.650 and depending on your browser you may title is actually showing in the HTML 161 00:09:44.650 --> 00:09:46.840 see that title appearing for you but for and depending on your browser you may 162 00:09:46.840 --> 00:09:47.830 me the way that I've configured see that title appearing for you but for 163 00:09:47.830 --> 00:09:52.780 chrome its not showing ok so the me the way that I've configured 164 00:09:52.780 --> 00:09:54.490 difference now is that the objects are chrome its not showing ok so the 165 00:09:54.490 --> 00:09:57.700 created independently of my _ page difference now is that the objects are 166 00:09:57.700 --> 00:10:00.250 but then passed to the constructor to created independently of my _ page 167 00:10:00.250 --> 00:10:02.410 create the new document now when it's but then passed to the constructor to 168 00:10:02.410 --> 00:10:03.400 done that way create the new document now when it's 169 00:10:03.400 --> 00:10:04.630 in other words when the class is done that way 170 00:10:04.630 --> 00:10:07.000 rewritten to use aggregation rather than in other words when the class is 171 00:10:07.000 --> 00:10:09.160 composition then the difference between rewritten to use aggregation rather than 172 00:10:09.160 --> 00:10:11.680 aggregation and Composition becomes more composition then the difference between 173 00:10:11.680 --> 00:10:14.440 obvious note that in both cases we aggregation and Composition becomes more 174 00:10:14.440 --> 00:10:17.230 still got has a relationships our new obvious note that in both cases we 175 00:10:17.230 --> 00:10:19.450 my_page object still has a still got has a relationships our new 176 00:10:19.450 --> 00:10:22.030 doctype has a head element and has a my_page object still has a 177 00:10:22.030 --> 00:10:24.460 body it's just what owns those objects doctype has a head element and has a 178 00:10:24.460 --> 00:10:26.890 that's changed now if we delete the body it's just what owns those objects 179 00:10:26.890 --> 00:10:28.990 my_page variable the other that's changed now if we delete the 180 00:10:28.990 --> 00:10:30.640 three objects obviously continue to my_page variable the other 181 00:10:30.640 --> 00:10:32.740 exist and can be used elsewhere in the three objects obviously continue to 182 00:10:32.740 --> 00:10:35.980 program and that's obviously because exist and can be used elsewhere in the 183 00:10:35.980 --> 00:10:37.690 we've actually defined them as program and that's obviously because 184 00:10:37.690 --> 00:10:43.540 separately here on line 74 line 81 and line 82 so we've actually defined them as 185 00:10:43.540 --> 00:10:45.190 that's aggregation compared to separately here on line 74 line 81 and line 82 so 186 00:10:45.190 --> 00:10:47.770 composition so the question now is which that's aggregation compared to 187 00:10:47.770 --> 00:10:50.260 one should you use and when and the composition so the question now is which 188 00:10:50.260 --> 00:10:52.210 answer to that is that it depends one should you use and when and the 189 00:10:52.210 --> 00:10:54.370 so in this example you might want the answer to that is that it depends 190 00:10:54.370 --> 00:10:55.960 ability to swap out the documents so in this example you might want the 191 00:10:55.960 --> 00:10:58.930 contents that can be useful and web ability to swap out the documents 192 00:10:58.930 --> 00:11:01.390 pages often swap out content in response contents that can be useful and web 193 00:11:01.390 --> 00:11:04.780 to selections for menus for example now pages often swap out content in response 194 00:11:04.780 --> 00:11:07.000 admittedly they tend to work on div tags to selections for menus for example now 195 00:11:07.000 --> 00:11:08.650 rather than swapping the entire body admittedly they tend to work on div tags 196 00:11:08.650 --> 00:11:11.120 section but the principles the same rather than swapping the entire body 197 00:11:11.120 --> 00:11:13.010 now it's very unlikely that you want to section but the principles the same 198 00:11:13.010 --> 00:11:15.380 change the doctype so there's no real now it's very unlikely that you want to 199 00:11:15.380 --> 00:11:17.210 reason that you couldn't use composition change the doctype so there's no real 200 00:11:17.210 --> 00:11:19.160 for the doc type and maybe even the head reason that you couldn't use composition 201 00:11:19.160 --> 00:11:21.470 element while using aggregation for the for the doc type and maybe even the head 202 00:11:21.470 --> 00:11:24.620 body design a structure of the classes element while using aggregation for the 203 00:11:24.620 --> 00:11:27.020 that make up a program is arguably the body design a structure of the classes 204 00:11:27.020 --> 00:11:28.640 hardest part of object-oriented that make up a program is arguably the 205 00:11:28.640 --> 00:11:30.830 programming and you have to think hardest part of object-oriented 206 00:11:30.830 --> 00:11:32.900 carefully about how the various classes programming and you have to think 207 00:11:32.900 --> 00:11:35.300 will work together and also what sort of carefully about how the various classes 208 00:11:35.300 --> 00:11:38.089 things you want to do with them now will work together and also what sort of 209 00:11:38.089 --> 00:11:40.190 sometimes the relationship is obvious things you want to do with them now 210 00:11:40.190 --> 00:11:42.020 thinking back to our player class in the sometimes the relationship is obvious 211 00:11:42.020 --> 00:11:44.089 game program we probably want to give thinking back to our player class in the 212 00:11:44.089 --> 00:11:46.520 the players weapons of some sort game program we probably want to give 213 00:11:46.520 --> 00:11:47.900 now there's an obvious has a the players weapons of some sort 214 00:11:47.900 --> 00:11:50.360 relationship there a player will have a now there's an obvious has a 215 00:11:50.360 --> 00:11:52.580 weapon now the weapons will probably be relationship there a player will have a 216 00:11:52.580 --> 00:11:54.800 strung across the dungeons waiting to be weapon now the weapons will probably be 217 00:11:54.800 --> 00:11:57.080 found by the players so they need to exist strung across the dungeons waiting to be 218 00:11:57.080 --> 00:12:00.080 independently of the players the players found by the players so they need to exist 219 00:12:00.080 --> 00:12:02.089 will also exchanged their weapons when independently of the players the players 220 00:12:02.089 --> 00:12:03.860 they stumble across better ones will also exchanged their weapons when 221 00:12:03.860 --> 00:12:06.380 so in that scenario you'd probably be they stumble across better ones 222 00:12:06.380 --> 00:12:08.990 better off choosing aggregation rather so in that scenario you'd probably be 223 00:12:08.990 --> 00:12:11.540 than composition but other times such as better off choosing aggregation rather 224 00:12:11.540 --> 00:12:13.730 our document here the relationship is less than composition but other times such as 225 00:12:13.730 --> 00:12:16.160 clear you could make a case for either our document here the relationship is less 226 00:12:16.160 --> 00:12:18.230 aggregation or composition when clear you could make a case for either 227 00:12:18.230 --> 00:12:20.510 considering the body of a document it aggregation or composition when 228 00:12:20.510 --> 00:12:21.860 really does depend on what you want to considering the body of a document it 229 00:12:21.860 --> 00:12:24.080 do with it as we saw changing the really does depend on what you want to 230 00:12:24.080 --> 00:12:25.970 composition relationship to an do with it as we saw changing the 231 00:12:25.970 --> 00:12:27.740 aggregation one isn't really that composition relationship to an 232 00:12:27.740 --> 00:12:28.820 difficult aggregation one isn't really that 233 00:12:28.820 --> 00:12:31.670 we created the test 2 HTML document by difficult 234 00:12:31.670 --> 00:12:33.709 accessing the quote-unquote private body we created the test 2 HTML document by 235 00:12:33.709 --> 00:12:36.620 attribute and replacing it if you found accessing the quote-unquote private body 236 00:12:36.620 --> 00:12:38.480 your class was used like that a lot attribute and replacing it if you found 237 00:12:38.480 --> 00:12:40.370 then you could add a property so that your class was used like that a lot 238 00:12:40.370 --> 00:12:42.020 the body attribute was no longer hidden then you could add a property so that 239 00:12:42.020 --> 00:12:44.029 and we saw how to do that a little bit the body attribute was no longer hidden 240 00:12:44.029 --> 00:12:45.770 earlier in this section and we saw how to do that a little bit 241 00:12:45.770 --> 00:12:47.330 alright so let's move on to the next earlier in this section 242 00:12:47.330 --> 00:12:52.750 video alright so let's move on to the next