WEBVTT 0 00:01.170 --> 00:02.070 Welcome back. 1 00:02.910 --> 00:13.120 If something quacks moves and swims like a duck it should be a duck so we don't care about duck type 2 00:13.120 --> 00:13.740 here. 3 00:14.020 --> 00:22.540 We just need to know about its behavior so we can work with it without knowing anything else about the 4 00:22.540 --> 00:23.890 duck type. 5 00:23.890 --> 00:32.930 Let's discuss what I mean let's say there is a power socket type like this one and there are a few electrical 6 00:32.930 --> 00:41.500 device types that need power the socket doesn't know anything about the devices so it can power up any 7 00:41.500 --> 00:50.560 device they all need to connect the power socket to draw power so they need a common interface to the 8 00:50.560 --> 00:53.760 power socket in order to draw power from it. 9 00:53.950 --> 00:57.160 Here is that common interface I present to you. 10 00:57.160 --> 00:59.320 The power drawer interface. 11 00:59.620 --> 01:07.630 It provides a Draw method that can give power to any connected device the socket has a plug method that 12 01:07.720 --> 01:11.240 accepts any device that has a Draw method. 13 01:11.350 --> 01:17.000 So each device implements a Draw method to draw power from the socket. 14 01:17.080 --> 01:21.840 So we say they satisfy the power drawer interface. 15 01:21.910 --> 01:27.700 Now we can plug the devices to the power socket using the plug method. 16 01:27.700 --> 01:36.430 Now they all are connected to the power socket the socket calls the draw method on each device and feeds 17 01:36.430 --> 01:43.630 them power the socket can power up any device only with one condition. 18 01:43.630 --> 01:52.200 As long as they have a draw method in other words when they satisfy the power drawer interface. 19 01:52.240 --> 02:00.850 So in this example the power socket type doesn't know anything about the device types all the devices 20 02:00.940 --> 02:04.270 need to draw power from the power socket. 21 02:04.510 --> 02:09.760 So they all implement the power drawer interface to do that. 22 02:09.940 --> 02:18.400 They implement a draw method by doing so they satisfy the interface so we can plug them to the socket 23 02:19.410 --> 02:25.120 the socket powers up the devices by calling their draw method. 24 02:25.210 --> 02:34.270 In summary interfaces decouple different types from each other so you can create more maintainable programs. 25 02:34.390 --> 02:43.510 For example you can add more device types without changing anything of the socket type an interface 26 02:43.690 --> 02:45.490 is a protocol. 27 02:45.730 --> 02:47.550 It is an abstract type. 28 02:47.680 --> 02:50.230 It doesn't have any implementation. 29 02:50.230 --> 02:55.670 It only describes the expected behavior from a type. 30 02:55.790 --> 03:01.670 For example any type that has a Draw method can satisfy the power drawer interface. 31 03:02.210 --> 03:07.160 So the socket type can use any type that has a Draw method. 32 03:07.160 --> 03:14.010 It's like saying I need a draw method but I don't care where it comes from. 33 03:14.030 --> 03:19.160 So an interface the couples the types from each other. 34 03:19.160 --> 03:27.520 It's because the types don't need to know anything about each other but the interface the opposite of 35 03:27.520 --> 03:30.490 an abstract type is a concrete type. 36 03:31.060 --> 03:38.230 So which types are not abstract? All the types are concrete types except the interface types. 37 03:38.500 --> 03:44.380 So all the types you can see here are concrete types. 38 03:44.400 --> 03:52.230 Let's take a look at an example first I'm going to delete the rest of the code from here. 39 03:52.280 --> 03:55.980 I'm going to create a list variable in my store. 40 03:56.030 --> 04:01.420 I sell games so I'm going to add all the games to my store. 41 04:01.520 --> 04:04.610 I want my customers to see the games. 42 04:04.730 --> 04:08.130 So I'm going to print them you know. 43 04:08.210 --> 04:16.790 I also want to sell books but it doesn't work because Moby Dick is a book not a game. 44 04:16.800 --> 04:19.080 So what should I do? 45 04:19.080 --> 04:22.910 Should I create another list only for books? 46 04:23.040 --> 04:27.160 Should I duplicate the list type all over again? 47 04:27.240 --> 04:29.330 There should be a better way. 48 04:29.340 --> 04:33.060 What does the print method need to do to print a game. 49 04:34.000 --> 04:36.930 The it is a game variable. 50 04:37.080 --> 04:46.070 A game has two methods print and discount but the code here only uses the print method. 51 04:46.200 --> 04:48.660 Let's see who else has a print method. 52 04:49.650 --> 04:56.220 The book type has a print method we know that the game type also has a print method. 53 04:56.890 --> 05:04.480 So the common behavior among the game and book types is the print method we can use an interface type 54 05:04.600 --> 05:06.900 to describe this behavior. 55 05:06.940 --> 05:15.600 So let's create it the name of the new type can be printer because they all print something. 56 05:15.720 --> 05:17.680 This is the common behavior. 57 05:17.700 --> 05:21.890 Next I'm going to say that the printer is an interface type. 58 05:22.080 --> 05:27.060 You know I want to create the type that only has a print method nothing more. 59 05:27.210 --> 05:29.380 So I'm going to say print. 60 05:29.370 --> 05:32.060 It's just a method name without code. 61 05:32.280 --> 05:36.850 As you can see the printer interface is an abstract type. 62 05:36.900 --> 05:40.220 I mean it doesn't contain any implementation. 63 05:40.290 --> 05:43.230 It only describes the printing behavior. 64 05:43.320 --> 05:44.360 Nothing more. 65 05:44.640 --> 05:48.880 It's an abstract type a protocol of how to print something. 66 05:48.960 --> 05:55.590 Here it says you can only satisfy me if you have a print method on you. 67 05:55.590 --> 05:57.800 And I don't care about the rest. 68 05:57.870 --> 06:00.180 You can implement it however you want. 69 06:00.960 --> 06:04.880 Let's take a look at the interface syntax in detail here. 70 06:05.100 --> 06:09.770 We declare a new printer interface type that prints something. 71 06:09.990 --> 06:16.500 That's why we name it as printer with an -er suffix as a convention. 72 06:16.500 --> 06:25.540 The interface keyword says that the printer is an interface type inside you type the method but without 73 06:25.600 --> 06:33.700 its implementation an interface only defines the expected behavior and it doesn't care where it comes 74 06:33.700 --> 06:34.280 from. 75 06:34.510 --> 06:41.930 The other types need to implement the print method if they want to. 76 06:42.130 --> 06:47.590 I'm going to change the type of the list instead of storing games in it, 77 06:47.590 --> 06:50.460 I'm going to store printer interface values. 78 06:50.620 --> 06:59.980 Yes I can do it because interface values are values so the loop variable here becomes a printer type. 79 07:00.710 --> 07:03.460 Let's take a look at its methods. 80 07:03.500 --> 07:05.570 There is only the print method. 81 07:05.570 --> 07:07.430 This is not a coincidence. 82 07:07.610 --> 07:13.640 It's because the printer interface has only one method and it is the print method. 83 07:13.640 --> 07:14.780 Just like here. 84 07:15.140 --> 07:20.550 Let's go back to the main function as you can see there are no more errors. 85 07:20.570 --> 07:23.570 OK let's try this program. 86 07:23.570 --> 07:24.450 Excellent. 87 07:24.500 --> 07:28.910 It prints the games as well as the books all together. 88 07:28.910 --> 07:34.050 Isn't it awesome? By changing the list to a slice of printers. 89 07:34.070 --> 07:37.080 Now I can store different types in it. 90 07:37.100 --> 07:45.630 Let me show you the types of elements inside the list as you can see the list contains game pointers 91 07:45.780 --> 07:48.020 and book values altogether. 92 07:48.360 --> 07:52.420 Even though there are different types behind the scenes. 93 07:52.590 --> 07:53.890 OK let me print. 94 07:53.980 --> 07:55.730 Let me comment out this printf. 95 07:55.740 --> 07:56.920 Excuse me. 96 07:57.060 --> 08:01.940 Okay let's say I also want to sell puzzles in my store. 97 08:02.010 --> 08:08.300 To do that I'm going to duplicate the book.go file first. 98 08:08.320 --> 08:15.910 I'm also going to change the book type to puzzle as you can see the puzzle has a print method. 99 08:15.930 --> 08:19.530 This means that I can use it as a printer. 100 08:19.530 --> 08:23.610 So let's add a puzzle to my store in the main function. 101 08:23.790 --> 08:32.770 I'm going to create a new puzzle variable and I'm going to add it to my store let's try it. 102 08:32.910 --> 08:33.600 Nice. 103 08:33.600 --> 08:37.100 Now I can sell puzzles as well as an example. 104 08:37.140 --> 08:41.930 I'm going to remove the print method from the puzzle type OK. 105 08:42.040 --> 08:44.490 Let's get back to the main function. 106 08:44.740 --> 08:52.210 As you can see now it doesn't work because the puzzle type doesn't have a print method anymore so it 107 08:52.210 --> 08:56.330 doesn't satisfy the printer interface anymore. 108 08:56.350 --> 09:05.190 That's why I cannot add it to the store slice the puzzle type is no longer a printer. 109 09:05.200 --> 09:06.650 Let me take it back. 110 09:06.760 --> 09:09.980 I want it to be a printer OK. 111 09:10.250 --> 09:17.300 If you are coming from another object oriented programming language you can see that there is no implements 112 09:17.330 --> 09:19.190 keyword in Go. 113 09:19.250 --> 09:23.690 You don't have to say whether a type implements an interface or not. 114 09:23.690 --> 09:25.520 It is implicit. 115 09:25.520 --> 09:28.080 It is one of the powerful features of Go. 116 09:28.190 --> 09:31.490 You can always add interfaces later on. 117 09:31.670 --> 09:34.310 You don't have to specify them earlier. 118 09:34.430 --> 09:41.210 Any type that has the methods of an interface can automatically satisfy the interface. 119 09:41.210 --> 09:48.560 For example let's take a look at these types the printer interface only has a print method. 120 09:48.560 --> 09:49.520 What do you think? 121 09:49.520 --> 09:53.390 Which one of these types can satisfy the printer interface? 122 09:56.470 --> 09:57.160 OK. 123 09:57.160 --> 10:05.700 Each one of them has a print method so every one of them can satisfy the printer interface. 124 10:05.780 --> 10:09.800 Now there was a discount method in the interface as well. 125 10:09.800 --> 10:11.160 What about now? 126 10:11.210 --> 10:21.120 Which one of these types can satisfy the interface? 127 10:21.190 --> 10:26.350 This time only the game type can satisfy the interface. I mean the printer interface however the other types cannot satisfy it. 128 10:26.350 --> 10:26.960 Why? 129 10:27.070 --> 10:30.750 It's because they don't have a discount method. 130 10:30.760 --> 10:41.000 A type can only satisfy an interface when it implements all of the methods of that interface. 131 10:41.060 --> 10:45.830 The bigger the interface the weaker the abstraction. 132 10:45.860 --> 10:52.710 This means that the more methods included in an interface harder to satisfy it. 133 10:52.790 --> 10:58.250 That's why you might want to include fewer or even only a single method in an interface. 134 11:01.340 --> 11:03.230 Let me show you one last thing. 135 11:03.980 --> 11:09.580 You can also compare interface values with other values. 136 11:09.730 --> 11:19.210 For example I can check whether the first item is equal to the minecraft or I can check for the last 137 11:19.330 --> 11:29.070 item let's try it as you can see they both return true because they were equal all right. 138 11:29.080 --> 11:31.820 It was a simple thing but I wanted to show you. 139 11:31.900 --> 11:35.160 We will discuss more more more about interfaces. 140 11:35.170 --> 11:36.610 No worries. 141 11:36.610 --> 11:37.660 See you in the next lecture.