1
00:00:05,580 --> 00:00:07,730
now towards the end of the last video I

2
00:00:07,730 --> 00:00:09,990
mentioned that if we were going to fully

3
00:00:09,990 --> 00:00:12,060
implement the header then we could get

4
00:00:12,060 --> 00:00:14,430
our head class the same ability to add

5
00:00:14,430 --> 00:00:16,890
tags and build up its contents and in

6
00:00:16,890 --> 00:00:18,780
fact we might create a special kind of

7
00:00:18,780 --> 00:00:21,350
tag class that can contain other tags

8
00:00:21,350 --> 00:00:24,210
and then have both head and body inherit

9
00:00:24,210 --> 00:00:25,950
from that but we are keeping things

10
00:00:25,950 --> 00:00:27,630
simple here and at this point we've now

11
00:00:27,630 --> 00:00:30,030
got all the classes that make up our

12
00:00:30,030 --> 00:00:32,490
HTML document and we can create the

13
00:00:32,490 --> 00:00:34,410
document class let's go ahead and do

14
00:00:34,410 --> 00:00:38,060
that in the bottom

15
00:00:38,060 --> 00:00:48,260
ok so....

16
00:00:48,260 --> 00:00:50,760
....

17
00:00:50,760 --> 00:01:08,580
....

18
00:01:08,580 --> 00:01:14,550
...

19
00:01:14,550 --> 00:01:17,760
...

20
00:01:17,760 --> 00:01:22,440
...this init method

21
00:01:22,440 --> 00:01:25,170
defines three data attributes to hold

22
00:01:25,170 --> 00:01:28,320
the doctype head and body of a HTML

23
00:01:28,320 --> 00:01:31,200
document now in all three cases a new

24
00:01:31,200 --> 00:01:32,880
object of the appropriate classes

25
00:01:32,880 --> 00:01:35,190
created as you can see and each attribute

26
00:01:35,190 --> 00:01:36,660
is assigned a reference to the new

27
00:01:36,660 --> 00:01:42,000
object so our HTML dot class is made up

28
00:01:42,000 --> 00:01:44,790
of instances of the three other classes

29
00:01:44,790 --> 00:01:47,280
so you could say that it's composed of

30
00:01:47,280 --> 00:01:49,410
the three other classes and that's

31
00:01:49,410 --> 00:01:52,020
composition now this example is pretty

32
00:01:52,020 --> 00:01:54,900
uncomplicated because the HTML. class

33
00:01:54,900 --> 00:01:57,660
is entirely composed of other classes it

34
00:01:57,660 --> 00:01:59,460
doesn't have any data attributes of its

35
00:01:59,460 --> 00:02:00,000
own

36
00:02:00,000 --> 00:02:02,070
now that doesn't have to be the case so

37
00:02:02,070 --> 00:02:03,330
I don't want to give the impression that

38
00:02:03,330 --> 00:02:05,640
composition means creating a class and

39
00:02:05,640 --> 00:02:07,470
entirely out of other classes

40
00:02:07,470 --> 00:02:09,840
the important thing is that our HTML

41
00:02:09,840 --> 00:02:12,380
. class contains other classes and

42
00:02:12,380 --> 00:02:14,360
can make use of the attributes that

43
00:02:14,360 --> 00:02:16,880
those classes provide alright i'll

44
00:02:16,880 --> 00:02:18,060
carry on with the code

45
00:02:18,060 --> 00:02:20,490
so we can see this program working and

46
00:02:20,490 --> 00:02:23,310
how it produces a html doc so we

47
00:02:23,310 --> 00:02:26,010
want to add a method....

48
00:02:26,010 --> 00:02:27,540
...

49
00:02:27,540 --> 00:02:30,750
....

50
00:02:30,750 --> 00:02:36,090
...

51
00:02:36,090 --> 00:02:37,380
....

52
00:02:37,380 --> 00:02:42,060
....

53
00:02:42,060 --> 00:02:47,850
...we need to

54
00:02:47,850 --> 00:02:50,520
add content to our document and HTML

55
00:02:50,520 --> 00:02:53,730
content consists of various tags so what

56
00:02:53,730 --> 00:02:56,010
given the html. class and add tag or

57
00:02:56,010 --> 00:02:57,780
add_tagged method to allow

58
00:02:57,780 --> 00:03:00,360
tags to be added to the document this

59
00:03:00,360 --> 00:03:02,160
method just delegates the job of adding

60
00:03:02,160 --> 00:03:04,740
a tag to its instance of the body class

61
00:03:04,740 --> 00:03:06,870
the body class takes care of

62
00:03:06,870 --> 00:03:09,360
implementing the process of adding a new

63
00:03:09,360 --> 00:03:12,930
tag to its contents the HTML . class

64
00:03:12,930 --> 00:03:14,550
just passes on the name of the tag and

65
00:03:14,550 --> 00:03:16,980
the contents as you can see on line 53

66
00:03:16,980 --> 00:03:19,350
now we could have called this method

67
00:03:19,350 --> 00:03:21,180
anything add_content for

68
00:03:21,180 --> 00:03:23,820
example but by giving it the same name

69
00:03:23,820 --> 00:03:26,130
as the class that will actually be doing

70
00:03:26,130 --> 00:03:28,380
the work for us our body class in this

71
00:03:28,380 --> 00:03:31,110
case we can pass the HTML document

72
00:03:31,110 --> 00:03:33,480
object to any function that adds tags

73
00:03:33,480 --> 00:03:36,270
now the other thing we want to do with

74
00:03:36,270 --> 00:03:39,270
html objects is display so i'm

75
00:03:39,270 --> 00:03:45,960
going to add a display method....

76
00:03:45,960 --> 00:03:48,840
....

77
00:03:48,840 --> 00:03:52,320
....

78
00:03:52,320 --> 00:03:57,960
...

79
00:03:57,960 --> 00:04:07,110
...

80
00:04:07,110 --> 00:04:09,840
....

81
00:04:09,840 --> 00:04:17,820
....

82
00:04:17,820 --> 00:04:22,230
so once again this method delegates most

83
00:04:22,230 --> 00:04:24,770
of the work to the classes that contains

84
00:04:24,770 --> 00:04:28,500
so the DTD is displayed by calling the

85
00:04:28,500 --> 00:04:31,470
doc type instances display method now after

86
00:04:31,470 --> 00:04:33,720
the DTD everything else is enclosed in

87
00:04:33,720 --> 00:04:37,110
the HTML opening and closing tags on line

88
00:04:37,110 --> 00:04:40,650
57 and 60 now we could have created a

89
00:04:40,650 --> 00:04:42,840
class to handle that but that's really

90
00:04:42,840 --> 00:04:44,190
going to be too far

91
00:04:44,190 --> 00:04:46,140
there's only one html tag in a

92
00:04:46,140 --> 00:04:48,360
document and hard coding like this is a

93
00:04:48,360 --> 00:04:51,180
much simpler solution now the display

94
00:04:51,180 --> 00:04:53,520
the remaining content is also delegated

95
00:04:53,520 --> 00:04:56,430
to the appropriate classes so apart from

96
00:04:56,430 --> 00:04:58,260
including the opening and closing HTML

97
00:04:58,260 --> 00:05:01,020
tag our documents display method

98
00:05:01,020 --> 00:05:02,670
doesn't have a lot to do

99
00:05:02,670 --> 00:05:04,380
once again we could have called this

100
00:05:04,380 --> 00:05:06,420
method anything but by calling it

101
00:05:06,420 --> 00:05:08,910
display it matches the corresponding

102
00:05:08,910 --> 00:05:11,010
method in the classes its composed of

103
00:05:11,010 --> 00:05:13,800
by calling the method display it can then

104
00:05:13,800 --> 00:05:16,770
be used by any function that displays

105
00:05:16,770 --> 00:05:20,400
tag objects in fact the method it has are

106
00:05:20,400 --> 00:05:22,140
exactly the same as the method in the

107
00:05:22,140 --> 00:05:25,830
body class so our HTML dot class is now

108
00:05:25,830 --> 00:05:28,860
displaying polymorphic behavior it is a

109
00:05:28,860 --> 00:05:31,320
html doc but it can also be used

110
00:05:31,320 --> 00:05:34,140
in place of a body object it can even be

111
00:05:34,140 --> 00:05:35,670
used in place of any of our tag

112
00:05:35,670 --> 00:05:38,520
subclasses when pass to functions that

113
00:05:38,520 --> 00:05:40,830
just need to display method so we've

114
00:05:40,830 --> 00:05:42,840
implemented polymorphism without using

115
00:05:42,840 --> 00:05:45,180
inheritance we've used composition

116
00:05:45,180 --> 00:05:48,150
instead now the program itself does use

117
00:05:48,150 --> 00:05:50,430
inheritance all our other classes

118
00:05:50,430 --> 00:05:53,160
inherit from tagged but the HTML doc type

119
00:05:53,160 --> 00:05:55,920
isn't a subclass of tag it provides the

120
00:05:55,920 --> 00:05:58,290
same methods as their tags but does so

121
00:05:58,290 --> 00:06:00,330
by defining methods with the same name

122
00:06:00,330 --> 00:06:03,060
and then delegating the implementation

123
00:06:03,060 --> 00:06:04,920
of those methods to the classes that is

124
00:06:04,920 --> 00:06:07,290
composed of alright so let's see this

125
00:06:07,290 --> 00:06:09,290
running to make sure that this theory

126
00:06:09,290 --> 00:06:11,010
that we've been talking about does work

127
00:06:11,010 --> 00:06:14,370
in practice so we're going to add a

128
00:06:14,370 --> 00:06:16,470
method down here....

129
00:06:16,470 --> 00:06:19,040
....

130
00:06:19,040 --> 00:06:22,140
....

131
00:06:22,140 --> 00:06:25,230
...

132
00:06:25,230 --> 00:06:27,970
....

133
00:06:27,970 --> 00:06:33,490
....

134
00:06:33,490 --> 00:06:37,270
...

135
00:06:37,270 --> 00:06:42,520
....

136
00:06:42,520 --> 00:06:45,940
....

137
00:06:45,940 --> 00:06:48,040
...

138
00:06:48,040 --> 00:06:50,680
....

139
00:06:50,680 --> 00:06:54,190
....

140
00:06:54,190 --> 00:06:58,420
...

141
00:06:58,420 --> 00:07:27,100
...

142
00:07:27,100 --> 00:07:30,700
...creating a new

143
00:07:30,700 --> 00:07:33,070
HTML object and then were assigning the

144
00:07:33,070 --> 00:07:35,290
reference to it to a very variable

145
00:07:35,290 --> 00:07:38,050
called my_page so next we

146
00:07:38,050 --> 00:07:40,360
use this add_tag method to add a

147
00:07:40,360 --> 00:07:42,850
heading and a subheading now the add_tag

148
00:07:42,850 --> 00:07:45,010
method remember just

149
00:07:45,010 --> 00:07:46,840
delegate to the body classes add

150
00:07:46,840 --> 00:07:48,730
underscore tag method so these elements

151
00:07:48,730 --> 00:07:50,440
will really be added to the body section

152
00:07:50,440 --> 00:07:52,810
so effectively we're building up the

153
00:07:52,810 --> 00:07:55,120
document by calling the add_tag

154
00:07:55,120 --> 00:07:56,950
method to add the content that we want

155
00:07:56,950 --> 00:07:59,050
when we done the last line we're calling

156
00:07:59,050 --> 00:08:00,850
the display method to display the

157
00:08:00,850 --> 00:08:02,020
finished document

158
00:08:02,020 --> 00:08:03,910
alright so let's just run the program

159
00:08:03,910 --> 00:08:06,580
need to put a colon in that line

160
00:08:06,580 --> 00:08:10,780
correct on line are 63 and run this

161
00:08:10,780 --> 00:08:13,300
now

162
00:08:13,300 --> 00:08:15,340
and we got an error over here and we can see

163
00:08:15,340 --> 00:08:16,990
that the typo looks to me that I've use

164
00:08:16,990 --> 00:08:20,050
start _ capital T so just click on

165
00:08:20,050 --> 00:08:21,970
that to go to the link that's the

166
00:08:21,970 --> 00:08:23,620
problem you can see added a capital T

167
00:08:23,620 --> 00:08:26,620
i'll make the lowercase T because

168
00:08:26,620 --> 00:08:29,170
obviously the the attributes

169
00:08:29,170 --> 00:08:31,360
should be or have rather a lowercase T

170
00:08:31,360 --> 00:08:32,230
in its name

171
00:08:32,230 --> 00:08:35,679
let's just try running it again and this

172
00:08:35,679 --> 00:08:37,210
time we seem to fix it which is much

173
00:08:37,210 --> 00:08:38,120
better

174
00:08:38,120 --> 00:08:40,270
alright so you can see now if

175
00:08:40,270 --> 00:08:42,789
you familiar with HTML that this is now

176
00:08:42,789 --> 00:08:45,710
perfectly formed HTML code

177
00:08:45,710 --> 00:08:47,330
now it's not as readable as it would be

178
00:08:47,330 --> 00:08:49,640
if a human it type it manually but line

179
00:08:49,640 --> 00:08:51,650
breaks are ignored in HTML documents

180
00:08:51,650 --> 00:08:53,690
there only added to make it easy for

181
00:08:53,690 --> 00:08:56,330
humans to read if you're not familiar

182
00:08:56,330 --> 00:08:58,550
with HTML the real test will be if it

183
00:08:58,550 --> 00:09:01,210
displays correctly in a browser so let's

184
00:09:01,210 --> 00:09:02,690
actually make a change so that we could

185
00:09:02,690 --> 00:09:05,140
write the document out to a file instead

186
00:09:05,140 --> 00:09:07,090
of just printing it now the only changes

187
00:09:07,090 --> 00:09:09,400
we need to make are to the three display

188
00:09:09,400 --> 00:09:11,450
methods i'm going to start with the base

189
00:09:11,450 --> 00:09:13,790
class the tag classes go back up and

190
00:09:13,790 --> 00:09:16,010
look at that

191
00:09:16,010 --> 00:09:18,140
so the display method here on the line

192
00:09:18,140 --> 00:09:20,890
11 we want to change that what we're

193
00:09:20,890 --> 00:09:23,510
going to do is add another parameter here

194
00:09:23,510 --> 00:09:25,520
so after the display itself...

195
00:09:25,520 --> 00:09:29,830
....

196
00:09:29,830 --> 00:09:35,300
....

197
00:09:35,300 --> 00:09:38,710
ok so we've added a named parameter called

198
00:09:38,710 --> 00:09:41,060
file that matches the file parameter of

199
00:09:41,060 --> 00:09:43,130
the print function so we need to

200
00:09:43,130 --> 00:09:47,000
do the same in the body class down here

201
00:09:47,000 --> 00:09:48,560
under display in the body class on line

202
00:09:48,560 --> 00:09:54,640
38....

203
00:09:54,640 --> 00:09:58,130
...

204
00:09:58,130 --> 00:10:01,010
....

205
00:10:01,010 --> 00:10:04,370
alright so we added a filename parameter

206
00:10:04,370 --> 00:10:06,400
as you can see there to this method as

207
00:10:06,400 --> 00:10:08,450
well and then provide the default value

208
00:10:08,450 --> 00:10:11,930
of none so if a file isn't specified the

209
00:10:11,930 --> 00:10:13,340
class will be printed to the screen

210
00:10:13,340 --> 00:10:16,130
effectively alright so now the

211
00:10:16,130 --> 00:10:18,560
display method of the html. class

212
00:10:18,560 --> 00:10:22,550
ok so down here in the display method

213
00:10:22,550 --> 00:10:27,590
we put....

214
00:10:27,590 --> 00:10:30,010
....

215
00:10:30,010 --> 00:10:32,140
...

216
00:10:32,140 --> 00:10:36,460
....

217
00:10:36,460 --> 00:10:39,230
...

218
00:10:39,230 --> 00:10:58,580
...

219
00:10:58,580 --> 00:11:01,520
....so now that we've done that we can

220
00:11:01,520 --> 00:11:03,510
now send the output to a file if we want

221
00:11:03,510 --> 00:11:05,730
to using the technique we saw back in

222
00:11:05,730 --> 00:11:08,090
Section 10 when we're writing to files

223
00:11:08,090 --> 00:11:11,580
so let's end this video here now in the

224
00:11:11,580 --> 00:11:13,400
next video we'll come back and test

225
00:11:13,400 --> 00:11:14,760
this will talk a little bit more about

226
00:11:14,760 --> 00:11:17,010
composition and then we'll move on to a

227
00:11:17,010 --> 00:11:23,870
challenge see you in the next video

