1
00:00:01,100 --> 00:00:02,610
We've introduced the decorator pattern in

2
00:00:02,610 --> 00:00:04,700
this course, coded up a couple of samples,

3
00:00:04,700 --> 00:00:06,410
and seen an instance of how the decorator

4
00:00:06,410 --> 00:00:09,410
pattern is used within .NET itself. At

5
00:00:09,410 --> 00:00:11,010
this point, we'll wrap the course up by

6
00:00:11,010 --> 00:00:12,370
talking about where you might use the

7
00:00:12,370 --> 00:00:14,070
decorator pattern in your code and some

8
00:00:14,070 --> 00:00:16,770
tips for doing so. One of the places I

9
00:00:16,770 --> 00:00:18,910
most frequently make use of decorators is

10
00:00:18,910 --> 00:00:20,360
when I need to apply cross cutting

11
00:00:20,360 --> 00:00:23,360
concerns. These are things like logging,

12
00:00:23,360 --> 00:00:24,670
performance tracking, caching, and

13
00:00:24,670 --> 00:00:27,830
authorization. We saw two examples of this

14
00:00:27,830 --> 00:00:29,810
in the course, and you can recognize from

15
00:00:29,810 --> 00:00:31,770
these examples how the decorator pattern

16
00:00:31,770 --> 00:00:34,220
keeps these concerns neatly separated in

17
00:00:34,220 --> 00:00:36,520
another class. So whenever you have these

18
00:00:36,520 --> 00:00:38,570
cross cutting concerns come up, think

19
00:00:38,570 --> 00:00:40,180
about if you can implement them using a

20
00:00:40,180 --> 00:00:42,870
decorator pattern. Another type of

21
00:00:42,870 --> 00:00:44,400
behavior we can implement using the

22
00:00:44,400 --> 00:00:46,800
decorator pattern is to modify the data

23
00:00:46,800 --> 00:00:49,550
that is sent to and from a component. For

24
00:00:49,550 --> 00:00:51,350
example, say there are certain fields of

25
00:00:51,350 --> 00:00:53,140
an object we need to encrypt and decrypt

26
00:00:53,140 --> 00:00:55,780
before being passed to a component. Or say

27
00:00:55,780 --> 00:00:57,260
there are fields whose values we need to

28
00:00:57,260 --> 00:00:59,400
convert, say from U.S. standard units to

29
00:00:59,400 --> 00:01:01,880
the metric system. Both of these problems

30
00:01:01,880 --> 00:01:03,780
could easily be solved with a decorator

31
00:01:03,780 --> 00:01:06,840
class. I also want to share a couple

32
00:01:06,840 --> 00:01:08,320
pieces of advice on implementing the

33
00:01:08,320 --> 00:01:11,020
decorator pattern. One of the requirements

34
00:01:11,020 --> 00:01:12,590
of the decorator pattern is that you have

35
00:01:12,590 --> 00:01:14,590
a common interface, or base class, that

36
00:01:14,590 --> 00:01:16,470
both your concrete implementations and

37
00:01:16,470 --> 00:01:19,330
decorated classes can derive from. But

38
00:01:19,330 --> 00:01:20,530
what if the class you want to add

39
00:01:20,530 --> 00:01:22,330
functionality to doesn't implement a

40
00:01:22,330 --> 00:01:24,360
common interface or derive from a common

41
00:01:24,360 --> 00:01:27,480
base class? What can you do then? The most

42
00:01:27,480 --> 00:01:28,930
straightforward answer is that you can

43
00:01:28,930 --> 00:01:31,010
extract an interface from the class and

44
00:01:31,010 --> 00:01:32,780
have your class and your decorators

45
00:01:32,780 --> 00:01:35,380
implement that interface. While you could

46
00:01:35,380 --> 00:01:37,650
extract this interface manually, you can

47
00:01:37,650 --> 00:01:39,820
also use the built‑in refactoring tools in

48
00:01:39,820 --> 00:01:41,970
Visual Studio, either with the keyboard

49
00:01:41,970 --> 00:01:45,210
shortcut of Ctrl+R and then Ctrl+I, or

50
00:01:45,210 --> 00:01:47,200
through the Edit> Refactor> Extract

51
00:01:47,200 --> 00:01:50,520
Interface menu item. What if, however, you

52
00:01:50,520 --> 00:01:52,370
have a class that you don't own, or for

53
00:01:52,370 --> 00:01:54,260
some reason you cannot change to implement

54
00:01:54,260 --> 00:01:57,160
an interface? In that case, what you want

55
00:01:57,160 --> 00:01:59,250
to do is to use another design pattern,

56
00:01:59,250 --> 00:02:01,590
the Adapter pattern, to put a class in

57
00:02:01,590 --> 00:02:03,320
front of your component and then have the

58
00:02:03,320 --> 00:02:05,520
Adapter pattern implement the interface

59
00:02:05,520 --> 00:02:07,940
that you have extracted. In this case, the

60
00:02:07,940 --> 00:02:09,840
Adapter class will be a simple wrapper

61
00:02:09,840 --> 00:02:11,870
around your Component class, but it will

62
00:02:11,870 --> 00:02:13,550
give you a class that you control, and

63
00:02:13,550 --> 00:02:15,440
therefore serve as an extension point that

64
00:02:15,440 --> 00:02:16,930
you can build your decorator classes

65
00:02:16,930 --> 00:02:20,280
around. Finally, the decorator pattern,

66
00:02:20,280 --> 00:02:22,550
like all software design patterns, is more

67
00:02:22,550 --> 00:02:24,260
than just about UML diagrams or

68
00:02:24,260 --> 00:02:26,510
specifications. They're really a way to

69
00:02:26,510 --> 00:02:29,440
think about solving problems in software.

70
00:02:29,440 --> 00:02:31,090
You've seen how to implement the decorator

71
00:02:31,090 --> 00:02:33,390
pattern in this course, but keep the big

72
00:02:33,390 --> 00:02:36,140
ideas of the decorator pattern in mind.

73
00:02:36,140 --> 00:02:38,440
First, using an interface or common base

74
00:02:38,440 --> 00:02:39,920
class helps us to be able to create

75
00:02:39,920 --> 00:02:42,300
loosely coupled software by allowing us to

76
00:02:42,300 --> 00:02:45,200
program to an abstraction. In the case of

77
00:02:45,200 --> 00:02:47,300
the decorator pattern, though, this

78
00:02:47,300 --> 00:02:49,650
ability to program to an abstraction also

79
00:02:49,650 --> 00:02:51,190
gives us an extension point for our

80
00:02:51,190 --> 00:02:53,280
classes because now we can create

81
00:02:53,280 --> 00:02:55,200
decorator classes that wrap around our

82
00:02:55,200 --> 00:02:57,660
component objects and even around each

83
00:02:57,660 --> 00:02:59,580
other. And through these decorator

84
00:02:59,580 --> 00:03:02,190
classes, we can inject functionality into

85
00:03:02,190 --> 00:03:05,290
the method calls of our component. Being

86
00:03:05,290 --> 00:03:07,200
able to layer objects together in this

87
00:03:07,200 --> 00:03:08,945
onion‑type structure and then intercept

88
00:03:08,945 --> 00:03:11,760
and modify method calls is a very powerful

89
00:03:11,760 --> 00:03:13,670
idea because it allows us to separate

90
00:03:13,670 --> 00:03:15,740
concerns and dynamically add new

91
00:03:15,740 --> 00:03:19,110
functionality when new needs come up. So

92
00:03:19,110 --> 00:03:20,760
as you work with the decorator pattern,

93
00:03:20,760 --> 00:03:23,370
keep these ideas in mind. Thank you for

94
00:03:23,370 --> 00:03:28,000
watching, and I wish you good luck and you continue your studies of design patterns.

