Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:04,250 --> 00:00:09,590
So let's talk about one of the most commonly used design patterns with the most commonly talked about,
2
00:00:09,590 --> 00:00:15,500
which is the observer persona, and we're going to start with a motivating example from the game world.
3
00:00:15,530 --> 00:00:19,130
So imagine that in my game I have a rampage mode.
4
00:00:19,130 --> 00:00:23,430
I get some sort of power up and I become invincible and scary to enemies.
5
00:00:23,450 --> 00:00:26,690
So usually enemies will try to attack me.
6
00:00:26,720 --> 00:00:33,140
However, if I get a rampage power up, I become invincible, and as a result, enemies should try to
7
00:00:33,140 --> 00:00:33,910
run away now.
8
00:00:33,920 --> 00:00:35,930
But a few other things happen in the game as well.
9
00:00:35,960 --> 00:00:38,750
Apart from that, that's not the only behavior that people see.
10
00:00:38,750 --> 00:00:44,870
They will also see that their health component should stop registering damage and that the UI should
11
00:00:44,870 --> 00:00:48,800
be tinted red or some other kind of UI update an indicator.
12
00:00:48,810 --> 00:00:52,160
So the situation we've got is a lot of dependencies.
13
00:00:52,160 --> 00:00:56,540
We've got a rampage mode component of some sort, which gets enabled.
14
00:00:56,540 --> 00:00:59,720
When we only pick up that power up, it kind of detects the power up.
15
00:00:59,720 --> 00:01:02,840
Somehow, let's just assume that that bit is sorted out.
16
00:01:02,870 --> 00:01:06,380
We've got the Rampage mode, has to talk to a whole bunch of enemies.
17
00:01:06,380 --> 00:01:11,100
It has to call a method on the enemies to say, Hey, you should be scared of me right now.
18
00:01:11,120 --> 00:01:17,900
So in a naive example, this might do a find objective type, find all the enemy eyes and call a method
19
00:01:17,900 --> 00:01:20,060
on them saying run away something like that.
20
00:01:20,120 --> 00:01:24,270
Now this is obviously creating a dependency between Rampage and the enemy air.
21
00:01:24,330 --> 00:01:28,070
It's made rampage dependent and coupled to the enemy A.I..
22
00:01:28,100 --> 00:01:32,420
And also, if we carry on with this naive train of thought, if we have a rampage UI, it has to go
23
00:01:32,420 --> 00:01:39,830
and say, Hey, get the UI and update it to the UI that we're in rampage mode and draw the red tint
24
00:01:39,830 --> 00:01:43,550
and go to the hell component and say, Hey, enable invulnerability.
25
00:01:43,700 --> 00:01:45,620
So it's got all of these dependencies.
26
00:01:45,620 --> 00:01:46,310
All of a sudden.
27
00:01:46,310 --> 00:01:51,960
It knows a lot about all of these different classes, which is not an ideal situation to be in.
28
00:01:51,980 --> 00:01:57,590
What we want to do is essentially flip this around because this would take it from having one class
29
00:01:57,590 --> 00:02:02,780
with a dependency on lots of classes to a lot of classes having a dependency on one class, which is
30
00:02:02,780 --> 00:02:05,530
a much better place to be in in this case.
31
00:02:05,550 --> 00:02:08,810
And one naive way of doing this is in update.
32
00:02:08,810 --> 00:02:15,650
So the Rampage UI could every update check with their players rampage components and say, Are you rampaging?
33
00:02:15,660 --> 00:02:18,380
And if so, I should draw the Rampage UI.
34
00:02:18,480 --> 00:02:23,180
The same would go with the health component could check its rampage, and it could enable its invulnerability
35
00:02:23,180 --> 00:02:27,410
and the all of the enemies could run that update methods to check that as well.
36
00:02:27,440 --> 00:02:34,540
Now this has an efficiency problem because updates will be run every frame and rampage might be something
37
00:02:34,550 --> 00:02:37,460
an event that happens once or twice in the whole course of the game.
38
00:02:37,460 --> 00:02:45,020
So this is an okay strategy, but not ideal, and it's also not great from the point of view of understanding
39
00:02:45,020 --> 00:02:45,500
the code.
40
00:02:45,560 --> 00:02:47,810
So what could we do instead?
41
00:02:47,930 --> 00:02:50,000
So that's where the observer pattern comes in.
42
00:02:50,030 --> 00:02:56,960
Now I'm going to use this graphic, which is a UML diagram taken from Wikipedia, because I think it's
43
00:02:56,960 --> 00:03:01,970
time that we start to understand these kinds of diagrams where you'll see them all over the place where
44
00:03:01,970 --> 00:03:05,080
ever design or programming patterns are discussed.
45
00:03:05,090 --> 00:03:07,340
So we're going to try and understand what this means.
46
00:03:07,370 --> 00:03:13,400
So the key things in the observer pattern are the observer class, which is this thing over here and
47
00:03:13,400 --> 00:03:15,830
the subject, which is something over here.
48
00:03:15,860 --> 00:03:18,110
The subject is the thing that we are observing.
49
00:03:18,110 --> 00:03:20,600
The observer is the thing doing the observing.
50
00:03:20,600 --> 00:03:24,640
In our example, the subject here would be the rampage component.
51
00:03:24,650 --> 00:03:27,980
The observers would be a number of different classes.
52
00:03:27,980 --> 00:03:32,250
They would be the enemy class, the Rampage UI class and the health class.
53
00:03:32,270 --> 00:03:33,660
So what's going on here?
54
00:03:33,680 --> 00:03:40,460
Basically, the observer has an update method, which basically is saying something has changed.
55
00:03:40,520 --> 00:03:44,240
For example, Rampage has started, Rampage has stopped.
56
00:03:44,240 --> 00:03:46,390
That would be our update method on the observer.
57
00:03:46,400 --> 00:03:48,080
And then you can see what we've got here.
58
00:03:48,080 --> 00:03:52,220
This arrow, this empty white arrow is indicating inheritance.
59
00:03:52,310 --> 00:03:54,740
So we've got a concrete observer, a concrete observer.
60
00:03:54,740 --> 00:03:57,590
B, in our example, this would be an enemy.
61
00:03:57,600 --> 00:04:00,380
This would be a health component and the rampage.
62
00:04:00,440 --> 00:04:03,580
Why each of these implements the update method.
63
00:04:03,590 --> 00:04:07,640
So each of them would have Rampage enabled Rampage disabled on them.
64
00:04:07,640 --> 00:04:14,180
And you can then have a situation where they register themselves for notifications so you can see the
65
00:04:14,180 --> 00:04:19,149
subject here has a register, observer, unregister observer and notify observers.
66
00:04:19,160 --> 00:04:25,310
So what would happen is the health component, which would call register observer on the rampage components.
67
00:04:25,310 --> 00:04:31,610
The enemies would do the same and then when the rampage actually starts the subject, in which case
68
00:04:31,640 --> 00:04:36,290
the rampage component would notify all of the observers it essentially runs through.
69
00:04:36,290 --> 00:04:41,240
As you can see this little snippet of code here notify observers would go and do a fully over all of
70
00:04:41,240 --> 00:04:44,200
the registered observers and call their update method.
71
00:04:44,210 --> 00:04:50,600
So this is obviously not exclusive to rampaging and is a very common pattern to want to use when in
72
00:04:50,600 --> 00:04:56,600
a situation where you have a one to many kind of communication, where you have one class, the subject
73
00:04:56,600 --> 00:05:01,670
that wants to send a message to lots of different observers and doesn't particularly care who those
74
00:05:01,670 --> 00:05:03,680
observers are, we don't want to know.
75
00:05:03,810 --> 00:05:08,400
Who needs to update is the response to this is just a fire and forget kind of situation now there's
76
00:05:08,400 --> 00:05:09,930
a lot of ways for us to do this.
77
00:05:09,930 --> 00:05:14,850
This the kind of classic way is to implement all these methods, but there's a lot of better ways for
78
00:05:14,850 --> 00:05:16,680
us to do this in unity and C-sharp.
79
00:05:16,680 --> 00:05:19,290
So we're going to look at some of those methods in the next lecture.
80
00:05:19,300 --> 00:05:25,890
But for now, let's have a quick little challenge about this pattern and think about the principles
81
00:05:25,890 --> 00:05:28,230
that this pattern is trying to improve.
82
00:05:28,260 --> 00:05:31,830
So take a look back at the commandments from the last section.
83
00:05:31,830 --> 00:05:37,350
And which does this particular pattern implement post video and have a little ponder?
84
00:05:40,710 --> 00:05:41,580
Okay, welcome back.
85
00:05:41,610 --> 00:05:46,440
So what I think this implements is not super clear, necessarily straight off the bat, but this is
86
00:05:46,440 --> 00:05:47,730
about loose coupling.
87
00:05:47,730 --> 00:05:54,420
Primarily, we had a situation in the first naive implementation of Rampage, where Rampage was unnecessarily
88
00:05:54,420 --> 00:06:00,000
coupled to a whole lot of components, which is undesirable because any of those components changing
89
00:06:00,000 --> 00:06:02,580
would cause a change in rampage.
90
00:06:02,610 --> 00:06:08,010
So what you're doing is you're reducing the coupling essentially by changing the direction of those
91
00:06:08,010 --> 00:06:12,820
dependencies, and that's why we like to use things like the observer pattern.
92
00:06:12,840 --> 00:06:14,730
Okay, so that's the observer pattern.
93
00:06:14,730 --> 00:06:15,360
In a nutshell.
94
00:06:15,360 --> 00:06:21,510
In the next decade, we're going to look at how we can actually implement it using various unity methods.
10072
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.