1
00:00:01,210 --> 00:00:02,150
Remember I mentioned there are two

2
00:00:02,150 --> 00:00:04,300
variants of the same problem we can

3
00:00:04,300 --> 00:00:06,890
address with the facade pattern. This is

4
00:00:06,890 --> 00:00:08,930
an explanation of the second and more

5
00:00:08,930 --> 00:00:11,930
common situation. In this example, we have

6
00:00:11,930 --> 00:00:15,000
several service classes, each with a

7
00:00:15,000 --> 00:00:17,590
specific method that needs to be called by

8
00:00:17,590 --> 00:00:20,610
another program, class, or what have you.

9
00:00:20,610 --> 00:00:23,130
The program on the right must new up each

10
00:00:23,130 --> 00:00:25,830
of these services. It must then call each

11
00:00:25,830 --> 00:00:27,750
of the methods on these services, and

12
00:00:27,750 --> 00:00:30,340
believe it or not, in a poorly designed

13
00:00:30,340 --> 00:00:32,640
system, order of calling the methods, even

14
00:00:32,640 --> 00:00:35,585
across classes, is sometimes significant,

15
00:00:35,585 --> 00:00:37,510
which may indicate they share an

16
00:00:37,510 --> 00:00:42,070
underlying subsystem, like data storage.

17
00:00:42,070 --> 00:00:44,190
This is what that ends up looking like.

18
00:00:44,190 --> 00:00:46,900
Here's a visual. Code like this is common,

19
00:00:46,900 --> 00:00:48,520
and truthfully, we see it mostly in

20
00:00:48,520 --> 00:00:50,440
controller‑type classes where several

21
00:00:50,440 --> 00:00:53,870
services are injected via the constructor.

22
00:00:53,870 --> 00:00:55,770
In this case, we must instantiate the

23
00:00:55,770 --> 00:00:59,350
classes and then call the methods we need.

24
00:00:59,350 --> 00:01:01,290
Notice we're dealing with three services,

25
00:01:01,290 --> 00:01:03,650
two of which are giving us results to

26
00:01:03,650 --> 00:01:07,580
display. This repetitive code is, well,

27
00:01:07,580 --> 00:01:10,280
repetitive. Consider how it would look if

28
00:01:10,280 --> 00:01:12,410
you put it behind a facade. Maybe we can

29
00:01:12,410 --> 00:01:15,370
get away with one class and one function

30
00:01:15,370 --> 00:01:18,570
call. Just like working with the ball of

31
00:01:18,570 --> 00:01:21,210
mud class, we can introduce a facade class

32
00:01:21,210 --> 00:01:23,890
to obfuscate all the calls needed to be

33
00:01:23,890 --> 00:01:26,180
invoked on the various classes this facade

34
00:01:26,180 --> 00:01:29,300
hides from the caller. This abstraction

35
00:01:29,300 --> 00:01:31,700
allows the calling program to work solely

36
00:01:31,700 --> 00:01:34,730
with the facade and simplifies the code.

37
00:01:34,730 --> 00:01:37,100
And introducing an interface here really

38
00:01:37,100 --> 00:01:40,490
tidies things up for the caller and allows

39
00:01:40,490 --> 00:01:43,340
easily adding new facade classes should

40
00:01:43,340 --> 00:01:46,000
the underlying service classes change in the future.

