1
00:00:05,160 --> 00:00:07,360
in the last video we saw how composition

2
00:00:07,360 --> 00:00:09,820
is a powerful technique for combining

3
00:00:09,820 --> 00:00:11,890
objects to create something more complex

4
00:00:11,890 --> 00:00:14,830
and we built up a HTML document from

5
00:00:14,830 --> 00:00:16,960
the three classes that it's composed of

6
00:00:16,960 --> 00:00:20,920
the doctype a head element and a body now

7
00:00:20,920 --> 00:00:23,590
aggregation is very similar and is often

8
00:00:23,590 --> 00:00:26,140
described as a weak form of composition

9
00:00:26,140 --> 00:00:29,260
now with composition the objects that another

10
00:00:29,260 --> 00:00:31,750
object is composed of don't exist

11
00:00:31,750 --> 00:00:35,620
outside of their container so in our

12
00:00:35,620 --> 00:00:39,400
example looking down here on line 50

13
00:00:39,400 --> 00:00:42,910
the init method for HTML doc there

14
00:00:42,910 --> 00:00:45,760
instances of a doctype which is created

15
00:00:45,760 --> 00:00:48,370
in a HTML doc init method on line 51 as i

16
00:00:48,370 --> 00:00:51,120
mentioned and a head and a body created

17
00:00:51,120 --> 00:00:54,280
on the next two lines so instances of

18
00:00:54,280 --> 00:00:56,850
all three classes exist but they only

19
00:00:56,850 --> 00:01:00,100
exist within the context of our HTML

20
00:01:00,100 --> 00:01:04,000
doc object my_page now if we

21
00:01:04,000 --> 00:01:06,220
delete my_page then those

22
00:01:06,220 --> 00:01:08,280
three instances will no longer exist

23
00:01:08,280 --> 00:01:11,380
either now in our program we've

24
00:01:11,380 --> 00:01:13,240
indicated at the three instance

25
00:01:13,240 --> 00:01:16,020
variables self dot underscored doc _

26
00:01:16,020 --> 00:01:19,060
type self._head and

27
00:01:19,060 --> 00:01:21,550
self dot _ body are quote unqote

28
00:01:21,550 --> 00:01:22,770
private

29
00:01:22,770 --> 00:01:26,140
now as we learned previously this is python

30
00:01:26,140 --> 00:01:27,660
and so they're not really private as

31
00:01:27,660 --> 00:01:30,310
such but there should be treated as if

32
00:01:30,310 --> 00:01:32,800
they are now this program was written in

33
00:01:32,800 --> 00:01:35,020
java for example there'd be no way to

34
00:01:35,020 --> 00:01:37,230
change those fields from outside the

35
00:01:37,230 --> 00:01:40,060
HTML dot class now I mention that

36
00:01:40,060 --> 00:01:41,830
because we can change this program to

37
00:01:41,830 --> 00:01:44,620
use aggregation without changing the

38
00:01:44,620 --> 00:01:47,620
HTML dot class in Java for example the

39
00:01:47,620 --> 00:01:49,270
code will have to be written a bit

40
00:01:49,270 --> 00:01:52,120
differently so in Python the distinction

41
00:01:52,120 --> 00:01:54,520
between aggregation and composition can

42
00:01:54,520 --> 00:01:58,200
blur slightly in Python focus more on

43
00:01:58,200 --> 00:02:00,520
the intent rather than what's possible

44
00:02:00,520 --> 00:02:02,770
with language that's because in Python

45
00:02:02,770 --> 00:02:04,930
just about anything is possible

46
00:02:04,930 --> 00:02:07,800
alright so let's use aggregation now

47
00:02:07,800 --> 00:02:10,449
to get our documents a new content then

48
00:02:10,449 --> 00:02:12,720
will go ahead and rewrite the class so

49
00:02:12,720 --> 00:02:14,980
that it uses aggregation rather than

50
00:02:14,980 --> 00:02:16,320
composition

51
00:02:16,320 --> 00:02:17,550
and then we're going to add some more

52
00:02:17,550 --> 00:02:20,070
code at the end of the program here so

53
00:02:20,070 --> 00:02:23,970
right down to the bottom and let's

54
00:02:23,970 --> 00:02:25,740
start here on line 74 we're going to type

55
00:02:25,740 --> 00:02:30,600
new....

56
00:02:30,600 --> 00:02:34,440
....

57
00:02:34,440 --> 00:02:37,800
...

58
00:02:37,800 --> 00:02:51,930
...

59
00:02:51,930 --> 00:02:54,180
....

60
00:02:54,180 --> 00:02:57,480
...

61
00:02:57,480 --> 00:03:21,210
...

62
00:03:21,210 --> 00:03:22,860
....

63
00:03:22,860 --> 00:03:26,070
...

64
00:03:26,070 --> 00:05:15,640
...

65
00:05:15,640 --> 00:05:17,320
alright so you can see what we've done

66
00:05:17,320 --> 00:05:19,960
there we created a new instance of the

67
00:05:19,960 --> 00:05:22,180
body class called new_body

68
00:05:22,180 --> 00:05:24,880
then we added to several tags to it we

69
00:05:24,880 --> 00:05:27,250
then assign the new_body object to our

70
00:05:27,250 --> 00:05:29,320
documents_body attributed

71
00:05:29,320 --> 00:05:31,840
directly if you run this program now

72
00:05:31,840 --> 00:05:39,490
you've see we got a test 2 . HTML showing

73
00:05:39,490 --> 00:05:41,620
their now and if I right click that now

74
00:05:41,620 --> 00:05:44,800
and open in browser and select a

75
00:05:44,800 --> 00:05:46,870
chrome in my case you select whichever

76
00:05:46,870 --> 00:05:50,380
browsers relevant you can see that we've

77
00:05:50,380 --> 00:05:51,730
got the new output showing their

78
00:05:51,730 --> 00:05:55,810
let's just go back to the code now so

79
00:05:55,810 --> 00:05:57,940
the point here is that if the my_

80
00:05:57,940 --> 00:06:00,190
page object is deleted then the

81
00:06:00,190 --> 00:06:02,200
new _ body object continues to

82
00:06:02,200 --> 00:06:04,450
exist and could actually be used in

83
00:06:04,450 --> 00:06:06,280
another document if we wanted to do that

84
00:06:06,280 --> 00:06:08,920
now as I said in python the

85
00:06:08,920 --> 00:06:11,080
distinction can get a little bit blurred

86
00:06:11,080 --> 00:06:13,960
so what I'm going to do is change the HTML dot

87
00:06:13,960 --> 00:06:15,970
class so that it's written to use

88
00:06:15,970 --> 00:06:18,280
aggregation rather than composition

89
00:06:18,280 --> 00:06:20,320
which is what you'd have to do in

90
00:06:20,320 --> 00:06:22,240
languages like Java if you wanted an

91
00:06:22,240 --> 00:06:24,340
aggregate class rather than a composite

92
00:06:24,340 --> 00:06:28,300
one we go back up to the HTML . class

93
00:06:28,300 --> 00:06:30,520
we defined here and what we're

94
00:06:30,520 --> 00:06:31,690
going to do first is we're going to

95
00:06:31,690 --> 00:06:36,850
remove the title and we're going to add

96
00:06:36,850 --> 00:06:38,020
some parameters going to add the

97
00:06:38,020 --> 00:06:43,240
doctype the head and the body and then

98
00:06:43,240 --> 00:06:45,340
we're going to initialize them so we're

99
00:06:45,340 --> 00:06:47,560
going to put...

100
00:06:47,560 --> 00:06:50,080
....

101
00:06:50,080 --> 00:06:51,880
...

102
00:06:51,880 --> 00:06:54,660
....

103
00:06:54,660 --> 00:06:56,360
...

104
00:06:56,360 --> 00:07:02,450
...

105
00:07:02,450 --> 00:07:06,860
....so now that we've done that in our HTML

106
00:07:06,860 --> 00:07:09,510
. class we need to provide instances

107
00:07:09,510 --> 00:07:12,300
of all three classes when we create a

108
00:07:12,300 --> 00:07:15,660
new html doc object now my

109
00:07:15,660 --> 00:07:17,130
original code that we have defined down

110
00:07:17,130 --> 00:07:19,080
here for my page my _ page

111
00:07:19,080 --> 00:07:21,260
won't work now so I'm going to actually

112
00:07:21,260 --> 00:07:23,610
comment that out will comment out the

113
00:07:23,610 --> 00:07:28,380
entire bit of code there and by doing

114
00:07:28,380 --> 00:07:29,990
that you can see these errors further on

115
00:07:29,990 --> 00:07:31,070
down in the code but we can ignore

116
00:07:31,070 --> 00:07:32,960
them for now because i'm going to go

117
00:07:32,960 --> 00:07:35,570
once we've created a new document

118
00:07:35,570 --> 00:07:38,760
so below that now i'm going to type in...

119
00:07:38,760 --> 00:07:43,010
....

120
00:07:43,010 --> 00:07:47,300
..

121
00:07:47,300 --> 00:07:49,230
...

122
00:07:49,230 --> 00:07:53,450
...

123
00:07:53,450 --> 00:07:56,550
...so the are only going to be

124
00:07:56,550 --> 00:07:59,210
executed if the if that we defined

125
00:07:59,210 --> 00:08:03,080
on line 66 returns true but what we do need to

126
00:08:03,080 --> 00:08:05,390
do after the new body the adding of the

127
00:08:05,390 --> 00:08:08,850
three tags is we need to assign new doc

128
00:08:08,850 --> 00:08:10,130
type so let's go ahead and do that so....

129
00:08:10,130 --> 00:08:14,820
...

130
00:08:14,820 --> 00:08:28,460
...

131
00:08:28,460 --> 00:08:34,350
...so what we should probably should

132
00:08:34,350 --> 00:08:35,370
have done with all these code is have

133
00:08:35,370 --> 00:08:37,860
it indented anyway so I'm going to just go

134
00:08:37,860 --> 00:08:38,940
ahead and do that so we're going to

135
00:08:38,940 --> 00:08:42,620
indent to that level and

136
00:08:42,620 --> 00:08:43,799
will delete the comment because we no longer

137
00:08:43,799 --> 00:08:45,600
need that anymore

138
00:08:45,600 --> 00:08:48,060
delete the my page that's because we're

139
00:08:48,060 --> 00:08:49,290
going to change the definition of that

140
00:08:49,290 --> 00:08:53,810
my page will become...

141
00:08:53,810 --> 00:08:57,740
...

142
00:08:57,740 --> 00:09:05,680
...

143
00:09:05,680 --> 00:09:08,260
we're going to leave the width open

144
00:09:08,260 --> 00:09:10,600
there because that's now valid because

145
00:09:10,600 --> 00:09:13,420
we've recreated our my_page

146
00:09:13,420 --> 00:09:15,460
variable but we will do is just change

147
00:09:15,460 --> 00:09:17,110
the filename so its test three .

148
00:09:17,110 --> 00:09:17,980
HTML

149
00:09:17,980 --> 00:09:20,140
it's not trying to override the previous one

150
00:09:20,140 --> 00:09:23,020
we've created right so let's just open

151
00:09:23,020 --> 00:09:24,640
the project pane and run to

152
00:09:24,640 --> 00:09:29,500
confirm that it does work works ok and

153
00:09:29,500 --> 00:09:32,470
got test three dot HTML so let's open in

154
00:09:32,470 --> 00:09:36,550
browser open in chrome and we've got

155
00:09:36,550 --> 00:09:39,310
working as you can see again we

156
00:09:39,310 --> 00:09:40,870
don't see the title bar but we just go

157
00:09:40,870 --> 00:09:41,980
back in there

158
00:09:41,980 --> 00:09:43,990
view page source you can see that the

159
00:09:43,990 --> 00:09:46,240
title is actually showing in the HTML

160
00:09:46,240 --> 00:09:47,950
and depending on your browser you may

161
00:09:47,950 --> 00:09:50,140
see that title appearing for you but for

162
00:09:50,140 --> 00:09:51,130
me the way that I've configured

163
00:09:51,130 --> 00:09:56,080
chrome its not showing ok so the

164
00:09:56,080 --> 00:09:57,790
difference now is that the objects are

165
00:09:57,790 --> 00:10:01,000
created independently of my _ page

166
00:10:01,000 --> 00:10:03,550
but then passed to the constructor to

167
00:10:03,550 --> 00:10:05,710
create the new document now when it's

168
00:10:05,710 --> 00:10:06,700
done that way

169
00:10:06,700 --> 00:10:07,930
in other words when the class is

170
00:10:07,930 --> 00:10:10,300
rewritten to use aggregation rather than

171
00:10:10,300 --> 00:10:12,460
composition then the difference between

172
00:10:12,460 --> 00:10:14,980
aggregation and Composition becomes more

173
00:10:14,980 --> 00:10:17,740
obvious note that in both cases we

174
00:10:17,740 --> 00:10:20,530
still got has a relationships our new

175
00:10:20,530 --> 00:10:22,750
my_page object still has a

176
00:10:22,750 --> 00:10:25,330
doctype has a head element and has a

177
00:10:25,330 --> 00:10:27,760
body it's just what owns those objects

178
00:10:27,760 --> 00:10:30,190
that's changed now if we delete the

179
00:10:30,190 --> 00:10:32,290
my_page variable the other

180
00:10:32,290 --> 00:10:33,940
three objects obviously continue to

181
00:10:33,940 --> 00:10:36,040
exist and can be used elsewhere in the

182
00:10:36,040 --> 00:10:39,280
program and that's obviously because

183
00:10:39,280 --> 00:10:40,990
we've actually defined them as

184
00:10:40,990 --> 00:10:46,840
separately here on line 74 line 81 and line 82 so

185
00:10:46,840 --> 00:10:48,490
that's aggregation compared to

186
00:10:48,490 --> 00:10:51,070
composition so the question now is which

187
00:10:51,070 --> 00:10:53,560
one should you use and when and the

188
00:10:53,560 --> 00:10:55,510
answer to that is that it depends

189
00:10:55,510 --> 00:10:57,670
so in this example you might want the

190
00:10:57,670 --> 00:10:59,260
ability to swap out the documents

191
00:10:59,260 --> 00:11:02,230
contents that can be useful and web

192
00:11:02,230 --> 00:11:04,690
pages often swap out content in response

193
00:11:04,690 --> 00:11:08,080
to selections for menus for example now

194
00:11:08,080 --> 00:11:10,300
admittedly they tend to work on div tags

195
00:11:10,300 --> 00:11:11,950
rather than swapping the entire body

196
00:11:11,950 --> 00:11:14,420
section but the principles the same

197
00:11:14,420 --> 00:11:16,310
now it's very unlikely that you want to

198
00:11:16,310 --> 00:11:18,680
change the doctype so there's no real

199
00:11:18,680 --> 00:11:20,510
reason that you couldn't use composition

200
00:11:20,510 --> 00:11:22,460
for the doc type and maybe even the head

201
00:11:22,460 --> 00:11:24,770
element while using aggregation for the

202
00:11:24,770 --> 00:11:27,920
body design a structure of the classes

203
00:11:27,920 --> 00:11:30,320
that make up a program is arguably the

204
00:11:30,320 --> 00:11:31,940
hardest part of object-oriented

205
00:11:31,940 --> 00:11:34,130
programming and you have to think

206
00:11:34,130 --> 00:11:36,200
carefully about how the various classes

207
00:11:36,200 --> 00:11:38,600
will work together and also what sort of

208
00:11:38,600 --> 00:11:41,380
things you want to do with them now

209
00:11:41,380 --> 00:11:43,490
sometimes the relationship is obvious

210
00:11:43,490 --> 00:11:45,320
thinking back to our player class in the

211
00:11:45,320 --> 00:11:47,380
game program we probably want to give

212
00:11:47,380 --> 00:11:49,820
the players weapons of some sort

213
00:11:49,820 --> 00:11:51,200
now there's an obvious has a

214
00:11:51,200 --> 00:11:53,660
relationship there a player will have a

215
00:11:53,660 --> 00:11:55,880
weapon now the weapons will probably be

216
00:11:55,880 --> 00:11:58,100
strung across the dungeons waiting to be

217
00:11:58,100 --> 00:12:00,380
found by the players so they need to exist

218
00:12:00,380 --> 00:12:03,380
independently of the players the players

219
00:12:03,380 --> 00:12:05,380
will also exchanged their weapons when

220
00:12:05,380 --> 00:12:07,160
they stumble across better ones

221
00:12:07,160 --> 00:12:09,680
so in that scenario you'd probably be

222
00:12:09,680 --> 00:12:12,290
better off choosing aggregation rather

223
00:12:12,290 --> 00:12:14,840
than composition but other times such as

224
00:12:14,840 --> 00:12:17,030
our document here the relationship is less

225
00:12:17,030 --> 00:12:19,460
clear you could make a case for either

226
00:12:19,460 --> 00:12:21,530
aggregation or composition when

227
00:12:21,530 --> 00:12:23,810
considering the body of a document it

228
00:12:23,810 --> 00:12:25,160
really does depend on what you want to

229
00:12:25,160 --> 00:12:27,380
do with it as we saw changing the

230
00:12:27,380 --> 00:12:29,270
composition relationship to an

231
00:12:29,270 --> 00:12:31,040
aggregation one isn't really that

232
00:12:31,040 --> 00:12:32,120
difficult

233
00:12:32,120 --> 00:12:34,970
we created the test 2 HTML document by

234
00:12:34,970 --> 00:12:37,000
accessing the quote-unquote private body

235
00:12:37,000 --> 00:12:39,920
attribute and replacing it if you found

236
00:12:39,920 --> 00:12:41,780
your class was used like that a lot

237
00:12:41,780 --> 00:12:43,670
then you could add a property so that

238
00:12:43,670 --> 00:12:45,320
the body attribute was no longer hidden

239
00:12:45,320 --> 00:12:47,320
and we saw how to do that a little bit

240
00:12:47,320 --> 00:12:49,070
earlier in this section

241
00:12:49,070 --> 00:12:50,630
alright so let's move on to the next

242
00:12:50,630 --> 00:12:56,050
video

