1
00:00:02,140 --> 00:00:03,390
[Autogenerated] let's return to our code

2
00:00:03,390 --> 00:00:05,340
and look at how to apply the adapter

3
00:00:05,340 --> 00:00:07,930
pattern to improve it. Along the way,

4
00:00:07,930 --> 00:00:09,800
we'll perform a few simple re factoring

5
00:00:09,800 --> 00:00:12,400
zzzz. Well, most of the time when you're

6
00:00:12,400 --> 00:00:14,420
going to apply the adapter pattern, you're

7
00:00:14,420 --> 00:00:16,320
going to already be working with a

8
00:00:16,320 --> 00:00:18,570
particular interface to an existing class

9
00:00:18,570 --> 00:00:21,630
implementation. In our example. So far, we

10
00:00:21,630 --> 00:00:23,280
have two different ways of performing some

11
00:00:23,280 --> 00:00:25,520
work, but we haven't actually respected

12
00:00:25,520 --> 00:00:27,910
them out into classes yet. So the first

13
00:00:27,910 --> 00:00:30,580
step is to do that. We're gonna take our

14
00:00:30,580 --> 00:00:32,310
two different if statements here, one

15
00:00:32,310 --> 00:00:34,420
using a file and the other one talking to

16
00:00:34,420 --> 00:00:36,810
the A p I. And we're going to break those

17
00:00:36,810 --> 00:00:38,330
out into two different class

18
00:00:38,330 --> 00:00:40,660
implementations. Once we've done that, we

19
00:00:40,660 --> 00:00:43,280
have a character file source class

20
00:00:43,280 --> 00:00:45,290
implementation, which has a get characters

21
00:00:45,290 --> 00:00:48,090
from file method that takes in a file

22
00:00:48,090 --> 00:00:51,200
path. And then we have a Star Wars, a p I

23
00:00:51,200 --> 00:00:53,980
source which has a get characters method.

24
00:00:53,980 --> 00:00:55,770
Now, I have intentionally given these two

25
00:00:55,770 --> 00:00:58,160
methods different names and signatures so

26
00:00:58,160 --> 00:01:00,240
that we can apply the adapter pattern to

27
00:01:00,240 --> 00:01:02,200
them. And this is pretty common when

28
00:01:02,200 --> 00:01:03,430
you're looking at how you're gonna get

29
00:01:03,430 --> 00:01:05,020
data from somewhere, or how you're going

30
00:01:05,020 --> 00:01:07,520
to use a service from an implementation

31
00:01:07,520 --> 00:01:09,470
that they won't have exactly the same

32
00:01:09,470 --> 00:01:11,120
interface. And that's where the adapter

33
00:01:11,120 --> 00:01:13,850
pattern really shines. Once we have the

34
00:01:13,850 --> 00:01:16,040
functionality in two different classes,

35
00:01:16,040 --> 00:01:19,000
the next step is to identify an interface

36
00:01:19,000 --> 00:01:21,340
that we can use commonly across both of

37
00:01:21,340 --> 00:01:24,020
these classes. By looking at with the

38
00:01:24,020 --> 00:01:27,030
return in this case the people variable,

39
00:01:27,030 --> 00:01:29,810
which is a list of person, we can see how

40
00:01:29,810 --> 00:01:32,400
it's being used. And in this particular

41
00:01:32,400 --> 00:01:34,660
class, the client, the only thing we're

42
00:01:34,660 --> 00:01:37,580
using people for is a four each leap. So

43
00:01:37,580 --> 00:01:39,060
we really just need to be able to

44
00:01:39,060 --> 00:01:41,350
enumerate over this collection. With that

45
00:01:41,350 --> 00:01:43,020
in mind, when we think about the interface

46
00:01:43,020 --> 00:01:45,550
we want, we want to use an interface that

47
00:01:45,550 --> 00:01:48,090
requires the least amount of information

48
00:01:48,090 --> 00:01:50,200
in order to be able to execute the method.

49
00:01:50,200 --> 00:01:52,180
For instance, if we take the interface

50
00:01:52,180 --> 00:01:55,200
from the characters from file call that

51
00:01:55,200 --> 00:01:57,600
requires a file path, we won't really be

52
00:01:57,600 --> 00:02:00,070
able to use that interface when we call

53
00:02:00,070 --> 00:02:02,100
the FBI. I mean, we could, but then we'd

54
00:02:02,100 --> 00:02:04,040
have to pass in some file path that

55
00:02:04,040 --> 00:02:06,260
wouldn't be used in order to fetch this

56
00:02:06,260 --> 00:02:08,690
list of characters from an online A P I.

57
00:02:08,690 --> 00:02:10,930
That doesn't make any sense. Instead,

58
00:02:10,930 --> 00:02:14,140
we're gonna create a simpler interface,

59
00:02:14,140 --> 00:02:15,460
and we're gonna start with something like

60
00:02:15,460 --> 00:02:19,020
this. This I character source adapter

61
00:02:19,020 --> 00:02:22,250
simply has one method get characters and

62
00:02:22,250 --> 00:02:24,820
it returns. An innumerable of person does

63
00:02:24,820 --> 00:02:28,750
not require any other information. Now, as

64
00:02:28,750 --> 00:02:30,680
we look at this, the one change that we

65
00:02:30,680 --> 00:02:33,600
might want to make is to make this a sink

66
00:02:33,600 --> 00:02:35,850
because all of the other calls that were

67
00:02:35,850 --> 00:02:38,300
making are actually a wearable. And so

68
00:02:38,300 --> 00:02:39,870
it's not that we're actually returning

69
00:02:39,870 --> 00:02:41,890
back. A list of person were actually

70
00:02:41,890 --> 00:02:45,040
returning back, a task of list of person.

71
00:02:45,040 --> 00:02:47,210
And so, for our needs, we can convert this

72
00:02:47,210 --> 00:02:50,160
into a task of innumerable of person and

73
00:02:50,160 --> 00:02:53,850
get the same type of behavior. Now that

74
00:02:53,850 --> 00:02:56,500
we've introduced the interface, the target

75
00:02:56,500 --> 00:02:58,680
that we're going to use for our service,

76
00:02:58,680 --> 00:03:01,340
we can re factor it to call this interface

77
00:03:01,340 --> 00:03:02,970
instead of calling the implementation

78
00:03:02,970 --> 00:03:05,210
directly, and then we'll be almost

79
00:03:05,210 --> 00:03:07,860
complete with how we're going to apply the

80
00:03:07,860 --> 00:03:10,920
adapter pattern. The next you're factoring

81
00:03:10,920 --> 00:03:13,540
is to create an instance of the eye

82
00:03:13,540 --> 00:03:16,320
character source adapter and then assign

83
00:03:16,320 --> 00:03:18,910
that to either a character file source

84
00:03:18,910 --> 00:03:21,790
adapter or a Star Wars, a P I character

85
00:03:21,790 --> 00:03:24,860
source it after noticed that we've taken

86
00:03:24,860 --> 00:03:27,850
the vile path argument that used to be on

87
00:03:27,850 --> 00:03:29,920
the get characters method, and we've moved

88
00:03:29,920 --> 00:03:32,260
it as a constructor argument into the

89
00:03:32,260 --> 00:03:34,900
adapter itself. Noticed that if statement

90
00:03:34,900 --> 00:03:37,390
has changed where before it was actually

91
00:03:37,390 --> 00:03:39,650
responsible for loading the people

92
00:03:39,650 --> 00:03:42,480
variable. Now all it does is create an

93
00:03:42,480 --> 00:03:44,840
instance of the character source type. The

94
00:03:44,840 --> 00:03:47,490
actual call to load the people is only

95
00:03:47,490 --> 00:03:50,060
performed once, and it's polymorphic now

96
00:03:50,060 --> 00:03:51,660
because it doesn't care which type of

97
00:03:51,660 --> 00:03:54,400
adapter we're using. We can actually

98
00:03:54,400 --> 00:03:56,820
replace this whole if statement with a

99
00:03:56,820 --> 00:03:58,900
factory, which we can do in a future where

100
00:03:58,900 --> 00:04:01,290
factory. Let's look at the implementation.

101
00:04:01,290 --> 00:04:02,950
We'll start with this Star Wars, a p I

102
00:04:02,950 --> 00:04:04,900
character source adaptor, because it's

103
00:04:04,900 --> 00:04:07,400
going to be the simpler one. Here at the

104
00:04:07,400 --> 00:04:09,980
top, you can see the actual client

105
00:04:09,980 --> 00:04:12,940
implementation of the Star Wars AP Class

106
00:04:12,940 --> 00:04:16,140
and then below, we've added, are adapter.

107
00:04:16,140 --> 00:04:18,320
All the adapter does is create an instance

108
00:04:18,320 --> 00:04:21,870
of the underlying Star Wars. A P I type.

109
00:04:21,870 --> 00:04:24,300
This is going to use composition and later

110
00:04:24,300 --> 00:04:26,270
on we might decide that we want to inject

111
00:04:26,270 --> 00:04:28,660
that instance through the constructor so

112
00:04:28,660 --> 00:04:30,420
that our dependency injection container

113
00:04:30,420 --> 00:04:32,220
could be responsible for its lifetime. But

114
00:04:32,220 --> 00:04:33,960
as a first pass weaken just insane, she

115
00:04:33,960 --> 00:04:36,320
ate it ourselves. Then our get characters

116
00:04:36,320 --> 00:04:39,740
call is trivial. All it does is delegate

117
00:04:39,740 --> 00:04:41,670
to the Star Wars, a p I instance, and call

118
00:04:41,670 --> 00:04:43,920
it's Get characters method. We made sure

119
00:04:43,920 --> 00:04:46,010
it was this simple. When we chose to use

120
00:04:46,010 --> 00:04:48,570
the get character's signature for our

121
00:04:48,570 --> 00:04:51,040
interface that we introduced, we made sure

122
00:04:51,040 --> 00:04:53,040
that it matched the Star Wars, a P I

123
00:04:53,040 --> 00:04:55,470
implementation. The character file source

124
00:04:55,470 --> 00:04:57,840
requires a little more work at the top.

125
00:04:57,840 --> 00:04:59,630
You can see the initial character file

126
00:04:59,630 --> 00:05:02,870
source. It requires the file name on its

127
00:05:02,870 --> 00:05:05,610
get characters from file method. This

128
00:05:05,610 --> 00:05:08,370
particular interface is incompatible with

129
00:05:08,370 --> 00:05:11,000
the Star Wars, a P I interface. It has a

130
00:05:11,000 --> 00:05:12,590
different signature, and it has a

131
00:05:12,590 --> 00:05:16,090
different method name. So in our adapter,

132
00:05:16,090 --> 00:05:18,040
we're going to do similar to what we did

133
00:05:18,040 --> 00:05:20,170
with the Star Wars adapter. But we have to

134
00:05:20,170 --> 00:05:22,080
do a little bit more work. We need that

135
00:05:22,080 --> 00:05:24,860
file name to pass into that method, and so

136
00:05:24,860 --> 00:05:26,590
we're going to pass that into the

137
00:05:26,590 --> 00:05:29,720
constructor of our adapter. Instance we

138
00:05:29,720 --> 00:05:31,660
take in the file name and we assign it to

139
00:05:31,660 --> 00:05:35,000
a private local field, Then inside of our

140
00:05:35,000 --> 00:05:36,950
interface implementation of the get

141
00:05:36,950 --> 00:05:38,730
characters method. Cole, we're gonna pass

142
00:05:38,730 --> 00:05:40,970
that file name to the instance of the

143
00:05:40,970 --> 00:05:43,300
character file source that we have as

144
00:05:43,300 --> 00:05:46,020
another private field again in a future.

145
00:05:46,020 --> 00:05:47,850
Re factoring We could modify this so that

146
00:05:47,850 --> 00:05:49,400
instead of in Stan Sheeting the character

147
00:05:49,400 --> 00:05:51,510
file source directly, we could request it

148
00:05:51,510 --> 00:05:53,870
from the constructor. After a quick re

149
00:05:53,870 --> 00:05:56,070
factory, we can eliminate Thean Stan she

150
00:05:56,070 --> 00:05:58,070
ation of the underlying character file

151
00:05:58,070 --> 00:05:59,860
source and just requested through the

152
00:05:59,860 --> 00:06:01,860
constructor. This will allow us to use

153
00:06:01,860 --> 00:06:04,260
dependency injection. In this case, it

154
00:06:04,260 --> 00:06:05,730
doesn't really change the behavior of the

155
00:06:05,730 --> 00:06:07,690
system, but it does give us an opportunity

156
00:06:07,690 --> 00:06:10,740
to manage the lifetime of the underlying

157
00:06:10,740 --> 00:06:13,340
target interface through the IOC

158
00:06:13,340 --> 00:06:14,920
container, the dependency injection

159
00:06:14,920 --> 00:06:16,430
container that we're using in our

160
00:06:16,430 --> 00:06:19,720
application. We do the same thing with the

161
00:06:19,720 --> 00:06:22,510
Star Wars, a p I adapter, and that makes

162
00:06:22,510 --> 00:06:24,440
it so that inside the character displaced

163
00:06:24,440 --> 00:06:27,230
service, we can just inject an eye

164
00:06:27,230 --> 00:06:29,640
character source adapter. We don't care

165
00:06:29,640 --> 00:06:32,190
which particular implementation it is. We

166
00:06:32,190 --> 00:06:34,320
no longer need an enumeration to tell us

167
00:06:34,320 --> 00:06:36,460
which type of source we're using. We're

168
00:06:36,460 --> 00:06:38,080
going to get that information just based

169
00:06:38,080 --> 00:06:40,090
on the type that gets injected. And we'll

170
00:06:40,090 --> 00:06:42,280
manage that type centrally when we create

171
00:06:42,280 --> 00:06:43,790
the application, or at least when we

172
00:06:43,790 --> 00:06:46,550
create this particular service. Now this

173
00:06:46,550 --> 00:06:48,540
Star Wars character display service

174
00:06:48,540 --> 00:06:50,440
definitely follows the open closed

175
00:06:50,440 --> 00:06:52,800
principle because we can adjust it to work

176
00:06:52,800 --> 00:06:54,760
with any different source of character

177
00:06:54,760 --> 00:06:57,310
data without having to change any of the

178
00:06:57,310 --> 00:07:00,810
code inside of this class Now where we

179
00:07:00,810 --> 00:07:03,040
have to in Stan, she ate things is in this

180
00:07:03,040 --> 00:07:05,580
case going to be inside our tests. So

181
00:07:05,580 --> 00:07:07,960
whereas before our test didn't need to

182
00:07:07,960 --> 00:07:10,070
worry about in stand seeing the individual

183
00:07:10,070 --> 00:07:12,330
character file source for character file

184
00:07:12,330 --> 00:07:15,090
source adapter. Now, since we've moved all

185
00:07:15,090 --> 00:07:16,700
of these things into constructor

186
00:07:16,700 --> 00:07:18,510
injection, we have to provide those

187
00:07:18,510 --> 00:07:21,140
instances when we create the service

188
00:07:21,140 --> 00:07:23,220
inside the application itself, we would

189
00:07:23,220 --> 00:07:25,220
probably just wire this up and used the

190
00:07:25,220 --> 00:07:27,610
dependency injection container. If we were

191
00:07:27,610 --> 00:07:30,150
using a SP dot net core. This would happen

192
00:07:30,150 --> 00:07:35,000
inside of the startup dot CS class inside of the Configure Service's method

