1 00:00:01,160 --> 00:00:04,040 -In this lecture, I will teach you what an object is. 2 00:00:04,360 --> 00:00:07,600 In fact, an object is a small entity that has a state 3 00:00:07,920 --> 00:00:11,160 and reacts to messages by executing methods. 4 00:00:11,480 --> 00:00:13,200 So, what is an object? 5 00:00:13,520 --> 00:00:18,040 An object is a small computer entity characterized by three properties. 6 00:00:18,360 --> 00:00:21,240 It has a state, a behavior and an identity, 7 00:00:21,560 --> 00:00:23,640 and it will collaborate with other objects, 8 00:00:23,960 --> 00:00:28,760 because it does not live in isolation, to perform more complex tasks. 9 00:00:29,640 --> 00:00:31,920 I will start with a few examples. 10 00:00:32,240 --> 00:00:35,720 Imagine a really simple object, such as a die. 11 00:00:36,600 --> 00:00:39,240 The state of a die is its number of faces. 12 00:00:39,560 --> 00:00:43,200 Its behavior is: "I want to roll the die to get a result." 13 00:00:43,520 --> 00:00:47,400 And this die will probably live in "aDieHandle", 14 00:00:47,720 --> 00:00:50,240 so that we can roll several dice together. 15 00:00:51,520 --> 00:00:53,960 Now, if we take the example of a rectangle... 16 00:00:54,280 --> 00:00:56,920 A rectangle is characterized by an X and a Y, 17 00:00:57,240 --> 00:01:01,520 and an extent, or a width and a height. 18 00:01:01,840 --> 00:01:03,920 That is the state of the rectangle. 19 00:01:04,240 --> 00:01:06,400 Then, the rectangle knows how to clip itself, 20 00:01:06,720 --> 00:01:10,560 what its area is, how to expand, shrink, 21 00:01:10,880 --> 00:01:12,280 and all these kinds of behavior. 22 00:01:12,600 --> 00:01:16,000 In fact, a rectangle also interacts with other objects. 23 00:01:16,320 --> 00:01:18,560 For example, the canvas on your computer 24 00:01:18,880 --> 00:01:24,560 will have rectangles to minimize the redisplay of your window. 25 00:01:24,880 --> 00:01:29,280 So, when you move your window, the display will generate rectangles 26 00:01:29,600 --> 00:01:33,960 and compute new rectangles to redisplay what should be redisplayed. 27 00:01:34,280 --> 00:01:36,640 Now, here is the example of a contact. 28 00:01:36,960 --> 00:01:39,920 In your iPhone, you have a friend as a contact. 29 00:01:40,240 --> 00:01:44,400 So, this is an object that will represent his or her e-mail address, name, 30 00:01:44,720 --> 00:01:48,200 address, phone number and picture. 31 00:01:48,520 --> 00:01:52,720 The behavior will be that you can display this object 32 00:01:53,040 --> 00:01:54,320 or query it. 33 00:01:54,640 --> 00:01:57,480 And also, this contact will work in a contact manager. 34 00:01:57,800 --> 00:02:01,600 So, the contact manager will be able to manage some of these objects. 35 00:02:01,920 --> 00:02:04,280 Similarly, if you collect games, for example, 36 00:02:04,600 --> 00:02:07,080 and you want to know what games you have, 37 00:02:07,400 --> 00:02:11,120 you will have a game item which has a name. 38 00:02:11,440 --> 00:02:13,280 Is it on PS2 or PS3? 39 00:02:13,600 --> 00:02:15,440 How much did it cost? 40 00:02:15,760 --> 00:02:16,960 Did you finish it or not? 41 00:02:17,280 --> 00:02:22,480 And then, you will be able to serialize this object on a disk, 42 00:02:22,800 --> 00:02:26,960 and it will collaborate with an item-collector system 43 00:02:27,280 --> 00:02:29,280 that will help you collect your games. 44 00:02:29,600 --> 00:02:33,680 So, what you see is that an object has a state, 45 00:02:34,000 --> 00:02:37,840 a behavior and an identity, and will collaborate with other objects 46 00:02:38,160 --> 00:02:40,040 to perform more complex tasks. 47 00:02:43,760 --> 00:02:47,560 I mentioned the identity of the object. 48 00:02:47,880 --> 00:02:50,040 Often, you confuse identity and equality. 49 00:02:50,360 --> 00:02:53,920 Here is a trick to avoid confusion. 50 00:02:54,240 --> 00:02:57,280 You enter a pizzeria and want to order a pizza. 51 00:02:57,600 --> 00:02:59,560 You want to order the same pizza as a guy. 52 00:02:59,880 --> 00:03:04,160 In the case of identity, you would eat his pizza. 53 00:03:04,480 --> 00:03:07,120 In the case of equality, you only want the same. 54 00:03:07,440 --> 00:03:12,520 So, identity is the real object that you point with your finger, 55 00:03:12,840 --> 00:03:15,760 whereas equality means wanting the same. 56 00:03:16,080 --> 00:03:17,920 Are those objects the same? 57 00:03:20,080 --> 00:03:22,960 In fact, an object has a responsibility, 58 00:03:23,280 --> 00:03:27,040 and it will expose this responsibility to you 59 00:03:27,360 --> 00:03:30,600 via a behavior, by executing a behavior. 60 00:03:30,920 --> 00:03:33,360 For example, a turtle has a responsibility 61 00:03:33,680 --> 00:03:35,880 to draw a line when it moves. 62 00:03:36,720 --> 00:03:39,640 This is the responsibility of this object. 63 00:03:39,960 --> 00:03:44,360 A file, for example, has a responsibility to have a content and to open itself. 64 00:03:44,680 --> 00:03:48,760 It is really important to understand that objects have responsibilities. 65 00:03:49,080 --> 00:03:52,960 And these responsibilities are embedded and realized 66 00:03:53,280 --> 00:03:57,000 by the behavior that is given when it is executed. 67 00:03:59,080 --> 00:04:01,040 An object will also encapsulate data. 68 00:04:01,360 --> 00:04:03,520 So, what you can see on the slide 69 00:04:03,840 --> 00:04:07,800 is that we can think that data is not exposed to the client. 70 00:04:08,120 --> 00:04:12,000 So, data is something that only the method of the object can access. 71 00:04:12,320 --> 00:04:15,120 So, we should consider data as something private. 72 00:04:15,440 --> 00:04:18,160 The methods are exposed, and you, as a client, 73 00:04:18,480 --> 00:04:22,480 or other objects, as clients, can only send messages to the object 74 00:04:22,800 --> 00:04:25,800 to execute the methods that will modify or not, 75 00:04:26,120 --> 00:04:29,280 or use the private data of the object. 76 00:04:29,600 --> 00:04:32,760 That is why, on this slide, I have this larger border 77 00:04:33,080 --> 00:04:34,760 between messages and methods. 78 00:04:37,080 --> 00:04:39,240 An object will react to messages. 79 00:04:39,560 --> 00:04:43,280 For example, I send the message "go" to the turtle 80 00:04:43,600 --> 00:04:46,000 and as a reaction, the turtle will execute a method 81 00:04:46,320 --> 00:04:51,840 that will move the turtle on the screen and draw a line on the screen. 82 00:04:52,840 --> 00:04:55,280 Now, what you should see is that different objects 83 00:04:55,600 --> 00:04:57,040 can understand the same message. 84 00:04:57,360 --> 00:05:01,080 In fact, I can ask a window to open and get a new window on my desktop, 85 00:05:01,400 --> 00:05:04,960 or I can ask a window to open a file, and I will look at its content, 86 00:05:05,280 --> 00:05:09,880 or if I open a Zip archive, I want to see the Zip item inside. 87 00:05:10,200 --> 00:05:13,080 And what you see is that it is the same message, 88 00:05:13,400 --> 00:05:16,560 but I have three different behaviors that have been executed. 89 00:05:18,360 --> 00:05:22,240 Now, a method specifies a behavior. 90 00:05:22,560 --> 00:05:27,000 So, you have the class "rectangle" that defines the height of a rectangle. 91 00:05:27,320 --> 00:05:29,080 To do so, it will compute, for example, 92 00:05:29,400 --> 00:05:33,640 the difference between the bottom and the top point of the rectangle. 93 00:05:35,160 --> 00:05:39,160 Now, a rectangle can also define a method "area" 94 00:05:39,480 --> 00:05:44,320 to compute its area by multiplying the width by the height. 95 00:05:44,640 --> 00:05:47,880 The methods specify the behavior that will be executed 96 00:05:48,200 --> 00:05:49,640 in reaction to the message. 97 00:05:49,960 --> 00:05:54,640 Now, a method can also send other messages to itself. 98 00:05:54,960 --> 00:06:01,160 For example, the message "topCenter" will send the message "center" 99 00:06:01,480 --> 00:06:02,600 and the message "top". 100 00:06:02,920 --> 00:06:06,920 The message "center" will also send the message "topLeft" and "bottomRight" 101 00:06:07,240 --> 00:06:08,560 to the rectangle. 102 00:06:08,880 --> 00:06:11,440 This means that we will have a cascade of messages 103 00:06:11,760 --> 00:06:13,960 in reaction to the execution of a method. 104 00:06:14,880 --> 00:06:15,960 And there is no problem. 105 00:06:16,280 --> 00:06:19,000 What I show in this example is that all the methods 106 00:06:19,320 --> 00:06:21,160 were methods of the class "rectangle". 107 00:06:21,480 --> 00:06:23,120 We will see what a class is later. 108 00:06:23,440 --> 00:06:25,640 But in fact, it does not have to be this way. 109 00:06:25,960 --> 00:06:29,640 This means that an object can also delegate to other objects. 110 00:06:29,960 --> 00:06:32,560 So, an object can say: "To perform this task, 111 00:06:32,880 --> 00:06:36,880 I will send messages to other objects and ask them to perform subtasks. 112 00:06:37,200 --> 00:06:40,360 And then, I will take the result and recombine it 113 00:06:40,680 --> 00:06:42,160 to build something larger." 114 00:06:45,800 --> 00:06:49,440 Here is a typical example of delegation. 115 00:06:49,760 --> 00:06:53,800 An item collector can ask its items to save themselves on the disk. 116 00:06:54,120 --> 00:06:57,040 Or it can say: "I will ask another object 117 00:06:57,360 --> 00:07:00,560 that is responsible for serializing objects 118 00:07:00,880 --> 00:07:03,040 to serialize myself." 119 00:07:03,360 --> 00:07:07,920 It depends. This is a design choice to know how we distribute 120 00:07:08,240 --> 00:07:09,800 the responsibility in the system. 121 00:07:10,120 --> 00:07:12,560 What is important to realize is that you, as an object, 122 00:07:12,880 --> 00:07:15,400 can delegate a task to somebody else. 123 00:07:17,360 --> 00:07:20,560 What you should know is that an object is characterized by a state, 124 00:07:20,880 --> 00:07:22,520 a behavior and an identity, 125 00:07:22,840 --> 00:07:26,320 that an object collaborates with other objects to realize tasks, 126 00:07:26,640 --> 00:07:31,600 that an object answers to messages and executes methods by reaction, 127 00:07:31,920 --> 00:07:34,920 and that an object may delegate a computation to another object, 128 00:07:35,240 --> 00:07:36,720 or to several other objects, 129 00:07:37,040 --> 00:07:39,640 and combine them to perform a task.