1 00:00:01,400 --> 00:00:03,200 -In this lecture, I would like to show 2 00:00:03,520 --> 00:00:07,840 that object-oriented design models the world in a different fashion. 3 00:00:08,160 --> 00:00:09,440 First, what you should know 4 00:00:09,760 --> 00:00:12,560 is that there is not one single model of the world. 5 00:00:12,880 --> 00:00:14,840 It depends on your focus of attention. 6 00:00:15,160 --> 00:00:18,040 For example, in a bank, they model the risk in a different way 7 00:00:18,360 --> 00:00:21,160 than in an insurance company, because in an insurance company, 8 00:00:21,480 --> 00:00:26,400 it is the key business aspect, whereas in a bank, it is a side effect. 9 00:00:26,720 --> 00:00:31,880 So, on the one hand, you do not have a unique model of the world, 10 00:00:32,200 --> 00:00:35,840 but in addition, you have different means and ways 11 00:00:36,160 --> 00:00:37,840 to model the world. 12 00:00:38,160 --> 00:00:41,520 In the past, we could use data-driven models, 13 00:00:41,840 --> 00:00:46,080 that are more used in database systems, 14 00:00:46,400 --> 00:00:50,160 object-oriented design, procedural modeling and so on. 15 00:00:50,480 --> 00:00:54,440 In this lecture, we will compare procedural modeling 16 00:00:54,760 --> 00:00:56,520 and object-oriented programming. 17 00:00:56,840 --> 00:00:58,720 What is object orientation? 18 00:00:59,040 --> 00:01:03,120 In fact, object orientation is a paradigm in that it is not a technology. 19 00:01:03,440 --> 00:01:06,360 Object-oriented design is a way to represent the world 20 00:01:06,680 --> 00:01:09,640 by simulating the world 21 00:01:09,960 --> 00:01:16,280 as a set of collaborating and decentralized entities. 22 00:01:16,600 --> 00:01:20,400 The idea is to try to minimize the complexity 23 00:01:20,720 --> 00:01:25,760 or to handle the complexity of what you are modeling, 24 00:01:26,080 --> 00:01:30,760 and also increase the reusability and lower the maintenance cost. 25 00:01:31,080 --> 00:01:35,840 Let us compare procedural design and object-oriented design. 26 00:01:36,160 --> 00:01:37,760 In procedural design, 27 00:01:38,080 --> 00:01:40,800 the system will focus on structures and procedures. 28 00:01:41,120 --> 00:01:43,800 Data is shared between procedures 29 00:01:44,120 --> 00:01:47,520 and is accessible from all procedures, even client procedures, 30 00:01:47,840 --> 00:01:51,960 and procedures know about the structure of data. 31 00:01:52,280 --> 00:01:54,880 There is not this notion of late binding. 32 00:01:55,200 --> 00:01:57,640 This means that even if we can simulate it, 33 00:01:57,960 --> 00:02:00,840 in fact, procedures have different name 34 00:02:01,160 --> 00:02:04,200 that are into a kind of single namespace like in C. 35 00:02:04,520 --> 00:02:07,600 And there is no decoupling between messages and methods. 36 00:02:07,920 --> 00:02:10,320 They are just procedures accessing data. 37 00:02:10,640 --> 00:02:16,240 Let us take an example to compare procedural modeling 38 00:02:16,560 --> 00:02:18,120 and object-oriented modeling. 39 00:02:18,440 --> 00:02:21,480 Imagine that we have a picture which is composed of shapes, 40 00:02:22,400 --> 00:02:24,960 like squares, rectangles and circles, 41 00:02:25,280 --> 00:02:28,880 and we want to compute the area of the picture. 42 00:02:29,200 --> 00:02:30,600 In the procedural way, 43 00:02:30,920 --> 00:02:33,160 and I wrote it in Java because you can write, 44 00:02:33,480 --> 00:02:36,800 in an object-oriented language, in a procedural way, 45 00:02:37,120 --> 00:02:39,400 what we will do is simple. 46 00:02:39,720 --> 00:02:43,680 We will iterate over the list of shapes, and for each shape, 47 00:02:44,000 --> 00:02:47,000 we will check: "If there is a square, then I know how to compute it. 48 00:02:47,320 --> 00:02:48,920 It is width multiplied by width. 49 00:02:49,240 --> 00:02:50,240 If there is a rectangle, 50 00:02:50,560 --> 00:02:54,280 I have to multiply the length by the height. 51 00:02:54,600 --> 00:02:59,160 And if there is a circle, I multiply pi by radius squared 52 00:02:59,480 --> 00:03:00,600 and I am done." 53 00:03:00,920 --> 00:03:03,400 We can also write that in Pharo. 54 00:03:03,720 --> 00:03:06,440 I can iterate over the shape and I do exactly the same. 55 00:03:06,760 --> 00:03:10,760 And I check: "If this class is from that kind, then I do that." 56 00:03:11,080 --> 00:03:13,720 If this class is from that kind, then I do that." 57 00:03:14,040 --> 00:03:16,080 Now, what are the drawbacks? There are several. 58 00:03:16,400 --> 00:03:18,680 First, all the logic is defined in one place, 59 00:03:19,000 --> 00:03:20,520 which is not bad per say. 60 00:03:20,840 --> 00:03:22,880 But the implications are bad. 61 00:03:23,200 --> 00:03:25,320 I cannot reuse the main functionality. 62 00:03:25,640 --> 00:03:27,560 What happens if I want to add a new shape? 63 00:03:27,880 --> 00:03:30,640 If I want to add a new shape, I have to modify this method 64 00:03:30,960 --> 00:03:34,080 or this procedure, and I have to recompile it. 65 00:03:34,400 --> 00:03:36,680 And I cannot even reuse the main procedure. 66 00:03:37,000 --> 00:03:40,080 Now, let us look at how this is done in the OOP way. 67 00:03:40,400 --> 00:03:45,040 In the OOP way, I will define the area in the class "picture" 68 00:03:45,360 --> 00:03:49,720 as a method, and I will iterate over the shapes. 69 00:03:50,040 --> 00:03:56,760 And I will ask each shape to compute its own area. 70 00:03:57,080 --> 00:03:59,960 And then, on square, rectangle and circle, 71 00:04:00,280 --> 00:04:05,000 I will define a method "area" that computes its area. 72 00:04:06,080 --> 00:04:08,840 What are the advantages? Adding a new shape is really easy. 73 00:04:09,160 --> 00:04:11,840 I just add a class with the message "area" and I am done. 74 00:04:12,160 --> 00:04:16,320 I can create objects and add them to the diagram and I am done. 75 00:04:16,640 --> 00:04:19,760 I can reuse the "Picture >> area" method. 76 00:04:20,080 --> 00:04:24,480 There is no problem, because it is independent of the shapes. 77 00:04:24,800 --> 00:04:30,880 And then, we can see that there is a decentralized view of computation. 78 00:04:31,200 --> 00:04:33,920 Each shape represents its own data/logic internally 79 00:04:34,240 --> 00:04:36,840 and exposes just the area to the rest. 80 00:04:38,320 --> 00:04:39,920 But there is a catch. 81 00:04:40,240 --> 00:04:42,760 Imagine, with object-oriented programming, 82 00:04:43,080 --> 00:04:47,480 if we had not given the same name to the method "area", 83 00:04:47,800 --> 00:04:52,280 then, we could not reuse the "Picture >> area" functionality 84 00:04:52,600 --> 00:04:54,880 because then, the client would have to check. 85 00:04:55,200 --> 00:04:59,240 So, all the shapes that offer the same service 86 00:04:59,560 --> 00:05:04,480 agree to expose the same API, the same interface to the client. 87 00:05:04,800 --> 00:05:06,680 This is what is called "polymorphism". 88 00:05:07,000 --> 00:05:10,240 This means that different objects can answer the same message 89 00:05:10,560 --> 00:05:12,040 with different executions. 90 00:05:12,640 --> 00:05:14,840 This is really important, because it is the way 91 00:05:15,160 --> 00:05:19,840 to build good object-oriented design by creating substitutable objects. 92 00:05:21,560 --> 00:05:24,880 At the end, what is an application in an object-oriented system? 93 00:05:25,200 --> 00:05:29,760 An application is a collection of interacting entities, objects. 94 00:05:30,080 --> 00:05:33,880 Objects are characterized by behavior and by state. 95 00:05:34,200 --> 00:05:39,640 The state is private and only methods can access the state. 96 00:05:39,960 --> 00:05:43,320 Objects communicate by messages that are resolved dynamically. 97 00:05:43,640 --> 00:05:45,880 There is a distinction between the messages 98 00:05:46,200 --> 00:05:49,320 and the methods that are executed. 99 00:05:49,640 --> 00:05:51,360 And message passing is late bound, 100 00:05:51,680 --> 00:05:54,280 so it depends on the receiver of the object 101 00:05:54,600 --> 00:05:58,040 and the computation that is attached to this object via its class. 102 00:05:59,480 --> 00:06:02,680 What we saw is that objects are unique entities. 103 00:06:03,000 --> 00:06:05,360 They have a private state, they react to messages, 104 00:06:05,680 --> 00:06:09,080 and their methods perform computation. 105 00:06:09,400 --> 00:06:13,880 The key point is that objects should offer polymorphic interfaces 106 00:06:14,200 --> 00:06:17,160 so that they can be mixed with other objects 107 00:06:17,480 --> 00:06:19,880 and that we can reuse the code that used them. 108 00:06:20,800 --> 00:06:23,120 Now, I would like to go over three cornerstones 109 00:06:23,440 --> 00:06:27,280 of object-oriented programming and show the interplay between them. 110 00:06:27,600 --> 00:06:29,640 The first two are encapsulation. 111 00:06:29,960 --> 00:06:35,440 Encapsulation is the ability to hide the way computation is done 112 00:06:35,760 --> 00:06:36,840 and data is stored. 113 00:06:37,160 --> 00:06:38,800 So, when I send a message, as a client, 114 00:06:39,120 --> 00:06:42,000 I do not care about the way my object manages data 115 00:06:42,320 --> 00:06:44,360 and how computation is performed. 116 00:06:44,680 --> 00:06:46,560 So, from that perspective, 117 00:06:46,880 --> 00:06:50,640 if an object is composed and delegates to other objects, 118 00:06:50,960 --> 00:06:52,440 I am not impacted, as a client. 119 00:06:52,760 --> 00:06:55,760 This means that the object is free to decompose 120 00:06:56,080 --> 00:07:01,680 its own computation into a subtask, in a decentralized way. 121 00:07:02,000 --> 00:07:06,080 This interplay with encapsulation and composition is really important. 122 00:07:07,160 --> 00:07:10,720 Then, there is another cornerstone, the distribution of responsibility. 123 00:07:11,040 --> 00:07:16,600 This means that when I compute a problem, I decompose the problem into subtasks. 124 00:07:16,920 --> 00:07:21,800 And this decomposition is emphasized by the use of late binding, 125 00:07:22,120 --> 00:07:29,240 where the idea is that the receiver will decide what to do. 126 00:07:29,560 --> 00:07:34,880 And these two things are also further emphasized 127 00:07:35,200 --> 00:07:36,720 by the idea of polymorphism. 128 00:07:37,040 --> 00:07:40,440 Because I have objects that exhibit the same interface, 129 00:07:40,760 --> 00:07:42,360 then I can mix them together. 130 00:07:42,680 --> 00:07:46,040 Because I have late binding, then I can have polymorphism, 131 00:07:46,360 --> 00:07:51,080 and then I can reuse objects and build more modular applications. 132 00:07:51,400 --> 00:07:54,920 The third cornerstone that I have not presented yet 133 00:07:55,240 --> 00:07:56,800 is an important one. 134 00:07:57,120 --> 00:08:00,320 It is the idea that I can build, in object-oriented programming, 135 00:08:00,640 --> 00:08:02,960 abstraction by extending other abstractions. 136 00:08:03,280 --> 00:08:07,360 So, the point is that I can define a class as a delta to another one. 137 00:08:07,680 --> 00:08:10,320 For example, if I have a "collection" class 138 00:08:10,640 --> 00:08:12,360 that manages collections of objects, 139 00:08:12,680 --> 00:08:16,040 then I can refine this collection of objects 140 00:08:16,360 --> 00:08:18,920 as "OrderedCollection", because there is an order. 141 00:08:19,240 --> 00:08:22,960 An "Array", for example, can be a kind of collection. 142 00:08:23,280 --> 00:08:27,480 And this mechanism to define an abstraction 143 00:08:27,800 --> 00:08:31,520 by specifying the delta from one abstraction to another one 144 00:08:31,840 --> 00:08:33,000 is called "inheritance". 145 00:08:33,320 --> 00:08:37,960 Inheritance is super powerful to reuse codes from superclasses. 146 00:08:38,280 --> 00:08:40,960 Here, for example, the code of collection will be reused 147 00:08:41,280 --> 00:08:44,040 in "OrderedCollection" and "Array". 148 00:08:44,360 --> 00:08:48,200 But I did not cover it in this lecture because I will in another one. 149 00:08:48,520 --> 00:08:50,240 So, what have we shown? 150 00:08:50,560 --> 00:08:53,080 That with object-oriented programming, 151 00:08:53,400 --> 00:08:58,560 programs are collaborating entities that exchange messages, 152 00:08:58,880 --> 00:09:01,840 that objects encapsulate their private data, 153 00:09:02,160 --> 00:09:07,520 that they can and should expose polymorphic APIs or interfaces 154 00:09:07,840 --> 00:09:11,040 so that they can be mixed with other objects 155 00:09:11,360 --> 00:09:13,560 and the client code can be reused. 156 00:09:13,880 --> 00:09:17,400 And we saw that late binding is a key aspect 157 00:09:17,720 --> 00:09:19,560 that enables all this dynamic. 158 00:09:19,880 --> 00:09:22,480 And at the end, we get more modular applications.