about summary refs log tree commit diff
path: root/usth/ICT2.7/P3L3 Design Patterns Subtitles/5 - Factory Method Pattern - lang_en_vs5.srt
blob: c7ebddd3aabef6dad6daaf6336573bd87df69817 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
1
00:00:00,012 --> 00:00:02,350
Let's now look at the first design pattern that we

2
00:00:02,350 --> 00:00:05,700
will discuss, the factory method pattern. And I'm going to start

3
00:00:05,700 --> 00:00:09,220
by discussing the intent of the pattern and its applicability.

4
00:00:09,220 --> 00:00:12,210
As far as the intent is concerned, the factory method pattern

5
00:00:12,210 --> 00:00:16,690
allows for creating objects without specifying their class, by invoking

6
00:00:16,690 --> 00:00:19,370
what we call a factory method. And what that is, is

7
00:00:19,370 --> 00:00:22,680
a method whose main goal is to create class instances.

8
00:00:22,680 --> 00:00:25,510
So when is this pattern useful? So when is it applicable?

9
00:00:25,510 --> 00:00:28,080
For example, it is applicable in cases in which a class

10
00:00:28,080 --> 00:00:31,890
cannot anticipate the type of object it must create. That is,

11
00:00:31,890 --> 00:00:34,800
the type of an object is not known at compile time,

12
00:00:34,800 --> 00:00:37,800
is not known until the code runs. A typical example of

13
00:00:37,800 --> 00:00:40,500
this, are frameworks. So if you ever used a framework, you

14
00:00:40,500 --> 00:00:44,280
will know that, normally, frameworks only know about interfaces and abstract

15
00:00:44,280 --> 00:00:47,450
classes. So the exact type of the objects of these classes

16
00:00:47,450 --> 00:00:50,840
is only known at runtime. The second case in which the factory

17
00:00:50,840 --> 00:00:53,835
method pattern is applicable, is when a class wants its

18
00:00:53,835 --> 00:00:57,160
subclasses to specify the type of objects it creates. And we'll

19
00:00:57,160 --> 00:00:59,920
see an example of this in a minute. Finally, factory

20
00:00:59,920 --> 00:01:03,580
method patterns are applicable when a class needs control over the

21
00:01:03,580 --> 00:01:06,760
creation of its objects. And in this case, one possible

22
00:01:06,760 --> 00:01:09,380
example is when there is a limit on the number of

23
00:01:09,380 --> 00:01:12,930
objects that can be created. Special example, it's a singleton. If

24
00:01:12,930 --> 00:01:15,840
you're familiar with a singleton, a singleton is a class for

25
00:01:15,840 --> 00:01:18,930
which only one instance can be created. The factory method pattern

26
00:01:18,930 --> 00:01:21,760
is perfect in these cases, because it allows to control how many

27
00:01:21,760 --> 00:01:24,640
objects get created. So in this case, it would allow the creation

28
00:01:24,640 --> 00:01:27,290
only of a single object. And from the second time that it

29
00:01:27,290 --> 00:01:30,040
is invoked, it will just return the object that was previously

30
00:01:30,040 --> 00:01:33,700
created. Now let's go ahead and see how this pattern actually works,

31
00:01:33,700 --> 00:01:37,100
and let's do that by discussing the structure and the participants for

32
00:01:37,100 --> 00:01:41,330
the pattern. The structure that is represented here, using the UML notation,

33
00:01:41,330 --> 00:01:45,530
includes three classes, the Creator, the ConcreteCreator,

34
00:01:45,530 --> 00:01:48,000
and the Product. The Creator provides the

35
00:01:48,000 --> 00:01:50,710
interface for the factory method. So this

36
00:01:50,710 --> 00:01:53,200
here, is the interface for the factory method

37
00:01:53,200 --> 00:01:55,950
that, when invoked, returns an object of

38
00:01:55,950 --> 00:01:59,440
type Product. The ConcreteCreator provides the actual

39
00:01:59,440 --> 00:02:02,350
method for creating the Product. So this

40
00:02:02,350 --> 00:02:06,170
method is a concrete implementation of this interface.

41
00:02:06,170 --> 00:02:10,630
Finally, the Product is the object created by the factory method. So

42
00:02:10,630 --> 00:02:12,350
summarizing, we have the interface for

43
00:02:12,350 --> 00:02:14,300
the factory method, the actual implementation of

44
00:02:14,300 --> 00:02:17,540
the summary method, and the object that is created by the factory method,

45
00:02:17,540 --> 00:02:21,020
when it is invoked. So let's look at an example of this pattern.