1
00:00:05,340 --> 00:00:07,610
so as promised in the previous video now

2
00:00:07,610 --> 00:00:10,440
time for a mini-challenge but just a

3
00:00:10,440 --> 00:00:12,450
point before we start don't spend more

4
00:00:12,450 --> 00:00:13,970
than a minute or two thinking about this

5
00:00:13,970 --> 00:00:14,810
one

6
00:00:14,810 --> 00:00:16,920
it is designed to be just something you
7

7
00:00:16,920 --> 00:00:18,350
should hopefully just get off the top of

8
00:00:18,350 --> 00:00:20,130
your head so don't spend again too much

9
00:00:20,130 --> 00:00:21,180
time on it

10
00:00:21,180 --> 00:00:22,910
so the challenge here is looking at the

11
00:00:22,910 --> 00:00:24,620
way the last two examples have been

12
00:00:24,620 --> 00:00:27,050
coded generally what is the difference

13
00:00:27,050 --> 00:00:28,770
between the approach taken here in all

14
00:00:28,770 --> 00:00:30,600
the previous example we use prior

15
00:00:30,600 --> 00:00:32,940
to that in this course have a think

16
00:00:32,940 --> 00:00:34,460
about that and come back when you

17
00:00:34,460 --> 00:00:35,600
ready end i will give you some more

18
00:00:35,600 --> 00:00:37,410
information about that

19
00:00:37,410 --> 00:00:42,480
okay welcome back the answer simply is

20
00:00:42,480 --> 00:00:45,540
there's no real difference at all so the

21
00:00:45,540 --> 00:00:47,550
two of these two examples use classes

22
00:00:47,550 --> 00:00:49,230
but apart from the fact that i wrote the

23
00:00:49,230 --> 00:00:51,060
classes in the program's is no real

24
00:00:51,060 --> 00:00:53,370
difference to the methodology that

25
00:00:53,370 --> 00:00:55,620
we employed in other words if the song

26
00:00:55,620 --> 00:00:57,600
album and artist classes had been part

27
00:00:57,600 --> 00:01:00,060
of the standard library then there's

28
00:01:00,060 --> 00:01:01,770
nothing to distinguish the approach here from

29
00:01:01,770 --> 00:01:03,960
anything else that we've done so far

30
00:01:03,960 --> 00:01:06,440
so this program uses objects but it's

31
00:01:06,440 --> 00:01:07,850
not object orientated programming

32
00:01:07,850 --> 00:01:10,920
object-oriented program is not just

33
00:01:10,920 --> 00:01:12,480
about classes it's a different way of

34
00:01:12,480 --> 00:01:15,270
thinking about problems and results in a

35
00:01:15,270 --> 00:01:17,790
different program design so it uses

36
00:01:17,790 --> 00:01:19,830
classes and objects for sure but it also

37
00:01:19,830 --> 00:01:21,570
includes important concepts such as

38
00:01:21,570 --> 00:01:24,060
encapsulation composition inheritance

39
00:01:24,060 --> 00:01:27,060
and delegation now I have discuss

40
00:01:27,060 --> 00:01:28,500
encapsulation at the start of this

41
00:01:28,500 --> 00:01:30,390
section we're going to be looking at

42
00:01:30,390 --> 00:01:31,980
inheritance and composition in later

43
00:01:31,980 --> 00:01:34,290
videos delegation on the other hand

44
00:01:34,290 --> 00:01:36,540
involves passing on responsibility for a

45
00:01:36,540 --> 00:01:38,550
task to another object that's better

46
00:01:38,550 --> 00:01:40,560
suited to deal with it and it's fairly

47
00:01:40,560 --> 00:01:42,360
you know real really closely tied to

48
00:01:42,360 --> 00:01:44,840
encapsulation now for example we've

49
00:01:44,840 --> 00:01:47,270
been encapsulated data in our classes but

50
00:01:47,270 --> 00:01:48,840
we haven't really encapsulated methods

51
00:01:48,840 --> 00:01:50,840
very well now there's an add song

52
00:01:50,840 --> 00:01:52,560
method in the album class and an add

53
00:01:52,560 --> 00:01:54,750
album method in the artist class but you

54
00:01:54,750 --> 00:01:55,860
know that's really it

55
00:01:55,860 --> 00:01:58,050
these two methods were included because

56
00:01:58,050 --> 00:02:00,360
the relevant classes own the lists that they will be

57
00:02:00,360 --> 00:02:02,370
added to so it seemed quite natural

58
00:02:02,370 --> 00:02:03,770
that the artist class should be

59
00:02:03,770 --> 00:02:06,000
responsible for adding album object to

60
00:02:06,000 --> 00:02:08,520
the list but this only considers the

61
00:02:08,520 --> 00:02:10,680
data has been no attempt to encapsulate

62
00:02:10,680 --> 00:02:12,770
the functionality of the program

63
00:02:12,770 --> 00:02:15,290
encapsulation involves encapsulating

64
00:02:15,290 --> 00:02:17,160
data and methods

65
00:02:17,160 --> 00:02:19,290
it's not just data so we really need

66
00:02:19,290 --> 00:02:21,000
to make some changes to turn this into

67
00:02:21,000 --> 00:02:23,520
an object-oriented program rather than

68
00:02:23,520 --> 00:02:25,080
just a program that happens to use

69
00:02:25,080 --> 00:02:26,280
objects then there's obviously an

70
00:02:26,280 --> 00:02:27,990
important distinction between those two

71
00:02:27,990 --> 00:02:31,350
encapsulating methods means considering

72
00:02:31,350 --> 00:02:34,020
questions such as which object is best

73
00:02:34,020 --> 00:02:36,360
suited to dealing with this task or

74
00:02:36,360 --> 00:02:38,940
where the responsibility for a performing

75
00:02:38,940 --> 00:02:41,580
this function belong so relating this to

76
00:02:41,580 --> 00:02:44,250
the real world if my car breaks down i'm

77
00:02:44,250 --> 00:02:45,180
going to look for someone who can

78
00:02:45,180 --> 00:02:46,650
encapsulates the knowledge of how to

79
00:02:46,650 --> 00:02:47,490
fix it

80
00:02:47,490 --> 00:02:49,680
mechanic in this case so my local

81
00:02:49,680 --> 00:02:51,750
mechanic has been programmed by

82
00:02:51,750 --> 00:02:53,430
attending college perhaps with a knowledge

83
00:02:53,430 --> 00:02:55,710
necessary to fix a car so i'm going to

84
00:02:55,710 --> 00:02:57,810
delegate the job to him or her

85
00:02:57,810 --> 00:02:59,670
similarly if I want an extension on my

86
00:02:59,670 --> 00:03:01,250
house i'm not going to try building

87
00:03:01,250 --> 00:03:03,390
myself going to find someone who's got

88
00:03:03,390 --> 00:03:05,820
the skills to do this for me so and that's

89
00:03:05,820 --> 00:03:07,470
of course unless you're builder a

90
00:03:07,470 --> 00:03:08,970
builder encapsulates the skills required

91
00:03:08,970 --> 00:03:10,800
to build something that won't fall down or

92
00:03:10,800 --> 00:03:12,900
leak in the rain and consequently I

93
00:03:12,900 --> 00:03:16,650
delegate that task to them so relating

94
00:03:16,650 --> 00:03:19,410
this back to our program looking at our

95
00:03:19,410 --> 00:03:21,210
load data method bring that up on the

96
00:03:21,210 --> 00:03:23,780
screen that function is doing all the

97
00:03:23,780 --> 00:03:25,800
work of parsing the data and working out

98
00:03:25,800 --> 00:03:27,590
what to do when it finds a new album and

99
00:03:27,590 --> 00:03:30,030
a new song really though the object that

100
00:03:30,030 --> 00:03:32,090
knows the most about albums is the

101
00:03:32,090 --> 00:03:34,280
artists and albums know all about songs

102
00:03:34,280 --> 00:03:36,500
so we really should encapsulate the

103
00:03:36,500 --> 00:03:38,400
methods for dealing with albums in the

104
00:03:38,400 --> 00:03:41,060
artist class and for dealing with songs in

105
00:03:41,060 --> 00:03:43,740
the album class so once that's done load

106
00:03:43,740 --> 00:03:45,930
data can delegate dealing with is to the

107
00:03:45,930 --> 00:03:48,390
appropriate class will start by

108
00:03:48,390 --> 00:03:50,160
rewriting the load data function to

109
00:03:50,160 --> 00:03:52,170
delegate most of the work to the artists

110
00:03:52,170 --> 00:03:54,450
class now when doing this I'm going to

111
00:03:54,450 --> 00:03:56,610
be calling methods that haven't yet

112
00:03:56,610 --> 00:03:58,830
been written now this may seem an odd

113
00:03:58,830 --> 00:04:00,300
thing to do but an object-oriented

114
00:04:00,300 --> 00:04:02,310
approach means you don't have to worry

115
00:04:02,310 --> 00:04:04,710
about how something is done you focus on

116
00:04:04,710 --> 00:04:06,590
the fact that you needed to be done and

117
00:04:06,590 --> 00:04:08,520
something else will be responsible for

118
00:04:08,520 --> 00:04:10,340
getting it done so that would make more

119
00:04:10,340 --> 00:04:12,150
sense when you see the load data code so

120
00:04:12,150 --> 00:04:13,410
let's go ahead and do that

121
00:04:13,410 --> 00:04:15,750
so the first thing we want to do now if

122
00:04:15,750 --> 00:04:17,060
load data is we're going to delete this

123
00:04:17,060 --> 00:04:19,019
first bit of initialization code we no

124
00:04:19,019 --> 00:04:21,750
longer need that so delete those three

125
00:04:21,750 --> 00:04:24,840
lines they're or to be precise just

126
00:04:24,840 --> 00:04:26,850
just those first two we want to leave

127
00:04:26,850 --> 00:04:28,740
the artist_list we still need

128
00:04:28,740 --> 00:04:29,390
that

129
00:04:29,390 --> 00:04:32,540
...what I'm going to do

130
00:04:32,540 --> 00:04:34,400
is that even though it's going to

131
00:04:34,400 --> 00:04:36,170
involve some code that has already been

132
00:04:36,170 --> 00:04:38,680
that exists on screen I'm going to delete it

133
00:04:38,680 --> 00:04:40,850
and retype some of this in just so you can

134
00:04:40,850 --> 00:04:43,100
see sort of much easier what's been

135
00:04:43,100 --> 00:04:45,620
changed so i'm going to leave the width code

136
00:04:45,620 --> 00:04:47,150
so we were obviously

137
00:04:47,150 --> 00:04:48,920
initializing the list on line 92

138
00:04:48,920 --> 00:04:51,230
this basic code here that retrieves the

139
00:04:51,230 --> 00:04:52,930
dialogue from albums . txt that's not

140
00:04:52,930 --> 00:04:54,590
going to change and we still going to

141
00:04:54,590 --> 00:04:56,390
leave that printing out but after

142
00:04:56,390 --> 00:04:57,980
that i'm going to remove all that code

143
00:04:57,980 --> 00:05:00,920
and delete it because we can

144
00:05:00,920 --> 00:05:02,840
substantially reduce the amount of code

145
00:05:02,840 --> 00:05:07,850
here so I'm now on line 101 and we're

146
00:05:07,850 --> 00:05:10,460
going to process each entry that we're

147
00:05:10,460 --> 00:05:12,770
receiving or getting from the albums.txt

148
00:05:12,770 --> 00:05:17,900
so...

149
00:05:17,900 --> 00:05:20,390
...

150
00:05:20,390 --> 00:05:24,680
...so we're looking for the

151
00:05:24,680 --> 00:05:27,050
artist based on the name attribute which we

152
00:05:27,050 --> 00:05:28,610
talk about extensively in the last video

153
00:05:28,610 --> 00:05:32,710
and if the new artist is set to none

154
00:05:32,710 --> 00:05:34,330
meaning that we weren't able to find it

155
00:05:34,330 --> 00:05:36,710
in the list already then we're going to

156
00:05:36,710 --> 00:05:40,460
create new artist....

157
00:05:40,460 --> 00:05:42,290
...

158
00:05:42,290 --> 00:06:04,430
...

159
00:06:04,430 --> 00:06:08,570
...and that's that's it

160
00:06:08,570 --> 00:06:11,390
so the first part of the function is

161
00:06:11,390 --> 00:06:13,250
unchanged the artist class isn't

162
00:06:13,250 --> 00:06:14,570
interested in the fact that data is

163
00:06:14,570 --> 00:06:16,100
coming from a text file

164
00:06:16,100 --> 00:06:17,540
nor is interested in the fact that it's

165
00:06:17,540 --> 00:06:20,060
red as a tab-delimited list and so load

166
00:06:20,060 --> 00:06:22,390
data as a result continues to be responsible

167
00:06:22,390 --> 00:06:24,620
for reading the data and splitting it

168
00:06:24,620 --> 00:06:25,960
into the correct fields and of course

169
00:06:25,960 --> 00:06:27,620
that's the code from line

170
00:06:27,620 --> 00:06:31,790
94 to 98 the list of artists belongs

171
00:06:31,790 --> 00:06:33,560
to the main programs so load data also

172
00:06:33,560 --> 00:06:36,380
retains responsibility for creating a

173
00:06:36,380 --> 00:06:38,200
new artist object after checking that

174
00:06:38,200 --> 00:06:39,800
one doesn't already exist with the

175
00:06:39,800 --> 00:06:41,230
code here on line 102

176
00:06:41,230 --> 00:06:43,680
that's all it does so everything else

177
00:06:43,680 --> 00:06:45,210
you can see now is delegated to the

178
00:06:45,210 --> 00:06:47,320
artist class by calling ist add song

179
00:06:47,320 --> 00:06:51,130
method on line 106 and then providing

180
00:06:51,130 --> 00:06:53,110
it with the fields remaining fields the

181
00:06:53,110 --> 00:06:55,150
album field the year field in the song

182
00:06:55,150 --> 00:06:56,770
field so that it can go ahead and create

183
00:06:56,770 --> 00:06:59,230
that at this point we're not interested

184
00:06:59,230 --> 00:07:00,930
in how the add song method does

185
00:07:00,930 --> 00:07:03,130
we'll work i know that the song will

186
00:07:03,130 --> 00:07:04,740
need to be added somehow but the focus

187
00:07:04,740 --> 00:07:07,390
here is on dealing with artists only a

188
00:07:07,390 --> 00:07:08,920
little worried about how add song will

189
00:07:08,920 --> 00:07:10,390
function when I get around to coding it

190
00:07:10,390 --> 00:07:12,760
until then I'm just gonna call it and

191
00:07:12,760 --> 00:07:14,680
not worry about it so it might seem

192
00:07:14,680 --> 00:07:16,180
strange calling a method that doesn't

193
00:07:16,180 --> 00:07:17,680
exist and you know of course the

194
00:07:17,680 --> 00:07:19,930
program won't work until it does exist but

195
00:07:19,930 --> 00:07:21,180
if you think about it it's really no

196
00:07:21,180 --> 00:07:23,430
different from delegating the repair of

197
00:07:23,430 --> 00:07:25,660
a car to a mechanic you know in other

198
00:07:25,660 --> 00:07:27,330
words I don't need any idea what the

199
00:07:27,330 --> 00:07:28,660
mechanic will do it don't need to be a

200
00:07:28,660 --> 00:07:30,930
mechanic and I don't need to understand how to fix

201
00:07:30,930 --> 00:07:32,980
a car I just know that the car needs

202
00:07:32,980 --> 00:07:33,960
fixing

203
00:07:33,960 --> 00:07:36,130
so I can call my mechanic and say fix

204
00:07:36,130 --> 00:07:39,160
the car and I do say hello first and please

205
00:07:39,160 --> 00:07:41,770
afterwards of course but basically it

206
00:07:41,770 --> 00:07:43,300
boils down to it I'm calling my

207
00:07:43,300 --> 00:07:46,180
mechanics fix_car method and

208
00:07:46,180 --> 00:07:48,160
hopefully i'm pretty confident that when

209
00:07:48,160 --> 00:07:50,920
the car returns it will be fixed so I'm

210
00:07:50,920 --> 00:07:52,510
sure you agree that looking at this code

211
00:07:52,510 --> 00:07:54,790
now it's fast simple then my previous

212
00:07:54,790 --> 00:07:57,130
version as a result much easy to

213
00:07:57,130 --> 00:07:59,410
understand and in fact creating a

214
00:07:59,410 --> 00:08:01,570
flowchart this would probably be totally

215
00:08:01,570 --> 00:08:03,390
over-the-top because the code that we've

216
00:08:03,390 --> 00:08:05,490
got now in our load data

217
00:08:05,490 --> 00:08:07,300
function pretty much document

218
00:08:07,300 --> 00:08:10,120
itself so now that the main program has

219
00:08:10,120 --> 00:08:11,700
been written it's time to focus on the

220
00:08:11,700 --> 00:08:13,930
details of that adds song method that were

221
00:08:13,930 --> 00:08:16,270
calling on line 106 so what

222
00:08:16,270 --> 00:08:17,950
we'll do is we'll create that method

223
00:08:17,950 --> 00:08:20,550
first and then explain what it's doing

224
00:08:20,550 --> 00:08:22,540
let's go back up to the artist class

225
00:08:22,540 --> 00:08:23,830
that we have defined the code for that

226
00:08:23,830 --> 00:08:25,990
we're going to add this method now

227
00:08:25,990 --> 00:08:28,180
this add song after the add album

228
00:08:28,180 --> 00:08:31,020
method so add here...

229
00:08:31,020 --> 00:08:33,580
...

230
00:08:33,580 --> 00:09:52,780
...

231
00:09:52,780 --> 00:09:55,300
ok so that's our doc string for this

232
00:09:55,300 --> 00:09:57,910
method so let's start by writing it i'm

233
00:09:57,910 --> 00:09:58,990
just gonna move this up a little bit so

234
00:09:58,990 --> 00:10:01,210
you can see more of the screen so we're

235
00:10:01,210 --> 00:10:03,070
gonna start I will actually type

236
00:10:03,070 --> 00:10:04,090
it out then we'll talk about a little

237
00:10:04,090 --> 00:10:07,690
bit more so...

238
00:10:07,690 --> 00:10:10,480
...

239
00:10:10,480 --> 00:11:19,820
...

240
00:11:19,820 --> 00:11:22,850
ok so hopefully the doc string that i

241
00:11:22,850 --> 00:11:25,910
added on line 82 really says all that

242
00:11:25,910 --> 00:11:28,220
needs to be said so add_song uses

243
00:11:28,220 --> 00:11:29,960
the name of the album to check if an album

244
00:11:29,960 --> 00:11:31,370
is already present in the list of

245
00:11:31,370 --> 00:11:34,040
albums and then create a new album if it

246
00:11:34,040 --> 00:11:36,170
isn't or if it doesn't exist in other

247
00:11:36,170 --> 00:11:37,460
words it's not present the list of

248
00:11:37,460 --> 00:11:39,590
albums anything else that needs to be

249
00:11:39,590 --> 00:11:41,500
done is been delegated to the album's

250
00:11:41,500 --> 00:11:43,610
to the album class add song method

251
00:11:43,610 --> 00:11:46,520
the artist class knows about albums so

252
00:11:46,520 --> 00:11:48,980
it deals with the album part it doesn't

253
00:11:48,980 --> 00:11:50,420
know about songs that's left to the

254
00:11:50,420 --> 00:11:52,310
album class to deal with

255
00:11:52,310 --> 00:11:53,930
once again I don't care what the album

256
00:11:53,930 --> 00:11:56,030
classes add song method does at this

257
00:11:56,030 --> 00:11:57,920
stage I just know i'm going to call it

258
00:11:57,920 --> 00:11:59,780
and let the album class handle what

259
00:11:59,780 --> 00:12:02,750
happens and you saw me add print

260
00:12:02,750 --> 00:12:03,980
statements here so that we can see

261
00:12:03,980 --> 00:12:05,240
what's happening when we come to run the

262
00:12:05,240 --> 00:12:07,280
program but the rest of the code again

263
00:12:07,280 --> 00:12:09,020
once again pretty much document itself

264
00:12:09,020 --> 00:12:11,270
so i'm going to finish the video here

265
00:12:11,270 --> 00:12:13,570
now I don't know why I added a semi colon at

266
00:12:13,570 --> 00:12:15,860
the end by the way wrong programming language

267
00:12:15,860 --> 00:12:18,560
so I'm going to finish the video here in the

268
00:12:18,560 --> 00:12:20,120
next video we'll come back and will

269
00:12:20,120 --> 00:12:22,790
implement the album class add song method

270
00:12:22,790 --> 00:12:25,000
so that we can delegate the task of

271
00:12:25,000 --> 00:12:27,980
dealing with the song to that method so

272
00:12:27,980 --> 00:12:33,850
see you in the next video

