WEBVTT 00:00.360 --> 00:06.280 We are now at the second video of the section adapter design pattern in the previous video. 00:06.330 --> 00:09.910 We looked at composite design patterns in this video. 00:09.930 --> 00:16.140 We will use an incompatible interface with an adapter object and unit test our printer adapter. 00:16.140 --> 00:20.230 We will also see the implementation of the adapter design pattern. 00:20.340 --> 00:24.880 One of the most commonly used structural patterns is the adapter pattern. 00:25.020 --> 00:30.780 Like in real life where you have to plug adapters and bolt adapters in go an adapter will allow us to 00:30.780 --> 00:35.120 use something that wasn't built for a specific task at the beginning. 00:35.190 --> 00:40.920 The adapter pattern is very useful when for example an interface gets outdated and it's not possible 00:40.920 --> 00:43.290 to replace it easily or fast. 00:43.290 --> 00:48.870 Instead you create a new interface to deal with the current needs of your application which under the 00:48.870 --> 00:55.920 hood uses implementations of the old interface adapter also helps us to maintain the open or closed 00:55.920 --> 00:59.700 principle in our apps making the more predictable too. 00:59.730 --> 01:06.030 They also allow us to write code which uses some base that we can't modify adapt or design pattern will 01:06.030 --> 01:10.670 help you fit the needs of two parts of the code that are incompatible at first. 01:10.680 --> 01:16.290 This is the key to being kept in mind when deciding if the adapter pattern is a good design for your 01:16.290 --> 01:17.060 problem. 01:17.100 --> 01:22.980 Two interfaces that are incompatible but which must work together are good candidates for an adapter 01:22.980 --> 01:28.770 pattern but they could also use the facade pattern for example for our example. 01:28.770 --> 01:32.070 We will have an old printer interface and a new one. 01:32.190 --> 01:37.620 Users of the new interface don't expect the signature that the old one has and we need an adapter so 01:37.620 --> 01:42.270 that the users can still use an old implementation if necessary. 01:42.270 --> 01:45.420 Here are the requirements and acceptance criteria. 01:45.510 --> 01:51.600 Having an old interface called Legacy printer and a new one called Modern printer create a structure 01:51.780 --> 01:57.090 that implements the modern printer interface and can use the legacy printer interface as described in 01:57.090 --> 01:58.530 these steps. 01:58.560 --> 02:03.650 First we create an adapter object that implements the modern printer interface. 02:03.750 --> 02:09.960 Next the new adapter object must contain an instance of the legacy printer interface. 02:09.960 --> 02:16.980 And lastly when using modern printer it must call the legacy printer interface under the hood pre fixing 02:16.980 --> 02:19.470 it with the text adapter. 02:19.470 --> 02:26.040 Now we move on to unit testing our printer adapter lets open the adapter dot go file. 02:26.040 --> 02:30.780 Here we have written the legacy code but we weren't tested as we should imagine that it isn't. 02:30.780 --> 02:37.970 Our code the legacy interface called Legacy printer as a print method that accepts a string and returns 02:37.970 --> 02:38.900 a message. 02:39.020 --> 02:45.380 Our my legacy printer struct implements the legacy printer interface and modifies the past string by 02:45.380 --> 02:48.350 prefixes the text legacy printer. 02:48.350 --> 02:54.410 After modifying the text the my legacy printer struct prints the text on the console and then returns 02:54.410 --> 02:55.190 it. 02:55.340 --> 02:58.570 Next we declare the new interface that will have to adapt. 02:58.580 --> 03:01.790 So this is the new printer interface. 03:01.790 --> 03:07.490 In this case the new print stored method doesn't accept any string as an argument because it will have 03:07.490 --> 03:10.250 to be stored in the implementers in advance. 03:10.340 --> 03:14.460 We call our adapter patterns printer adapter interface. 03:14.510 --> 03:19.440 As mentioned earlier the printer adapter must have a field to store the string to print. 03:19.640 --> 03:25.130 It must also have a field to store an instance of the legacy printer adapter so let's save this file 03:25.310 --> 03:26.900 and move to the adapter test. 03:26.900 --> 03:30.710 Dot go file where we have written the unit tests. 03:30.710 --> 03:36.980 Now we use the message Hello world for our adapter when using this message with an instance of the my 03:36.980 --> 03:43.880 legacy printer struct it prints the text legacy printer hello world we created an instance of the printer 03:43.880 --> 03:50.420 adapter interface called adapter we passed an instance of the my legacy printer struct as the legacy 03:50.420 --> 03:53.180 printer field called old printer. 03:53.330 --> 03:59.690 Also we set the message we want to print in the message field then we use the print stored method of 03:59.690 --> 04:01.280 the modern printer interface. 04:01.310 --> 04:05.840 This method doesn't accept any argument and must return the modified string. 04:05.870 --> 04:11.900 We know that the my legacy printer struct returns the past string prefixed with the text legacy printer 04:12.320 --> 04:18.800 and the adapter will prefix it with the text adapter so in the end we must have the text legacy printer 04:18.860 --> 04:25.810 colon adapter hello world as we are storing an instance of an interface we must also check that we handle 04:25.810 --> 04:28.610 the situation where the pointer is nil. 04:28.610 --> 04:32.090 This is done with the test which you see highlighted here. 04:32.090 --> 04:38.540 If we don't pass an instance of the legacy printer interface the adapter must ignore its adapt nature 04:38.570 --> 04:41.660 and simply print and return the original message. 04:41.900 --> 04:49.270 Time to run our tests so move on to the terminal and run the command go test hyphen V. 04:49.460 --> 04:53.390 Here's the output and you can see that our test has failed. 04:53.390 --> 04:59.170 Now let's step further to see the implementation to make our single test pass. 04:59.180 --> 05:05.150 We must reuse the old my legacy printer that is stored in printer adapter struct. 05:05.150 --> 05:08.180 Now we need to make some changes to our code. 05:08.300 --> 05:10.920 So let me add the lines of code here. 05:11.180 --> 05:17.300 Okay in the print stored method we check whether we actually have an instance of the legacy printer 05:17.630 --> 05:18.560 in this case. 05:18.560 --> 05:24.020 We compose a new string with the stored message and the adapter prefix to store it in the returning 05:24.020 --> 05:26.900 variable called new message. 05:26.960 --> 05:33.380 Then we use the pointer to my legacy printer struct to print the composed message using the legacy printer 05:33.380 --> 05:34.700 interface. 05:34.700 --> 05:40.820 In case there is no legacy printer instance stored in the old printer field we simply assign the stored 05:40.820 --> 05:45.350 message to the returning variable new message and return the method. 05:45.350 --> 05:51.530 This should be enough to pass our tests so let's save the file and navigate to the terminal to run the 05:51.530 --> 05:54.270 test again execute the command. 05:54.350 --> 05:58.450 Go test hyphen v perfect. 05:58.520 --> 06:04.250 Now he can still use the old legacy printer interface by using this adapter while we use the modern 06:04.250 --> 06:07.040 printer interface for future implementations. 06:07.040 --> 06:13.820 Just keep in mind that the adapter pattern must ideally just provide the way to use the old legacy printer 06:13.910 --> 06:15.250 and nothing else. 06:15.260 --> 06:20.900 This way its scope will be more encapsulated and more maintainable in the future. 06:20.960 --> 06:26.750 In this video with the adapter design pattern you've learned a quick way to achieve the open or close 06:26.750 --> 06:32.780 principle in your applications instead of modifying your old source code you have created a way to use 06:32.780 --> 06:37.150 the old functionality with a new signature in the next video. 06:37.250 --> 06:39.740 We will learn about the bridge design pattern.