WEBVTT 00:00.270 --> 00:07.490 We are now at the fourth video of this section Abstract Factory method that is factory of factories. 00:07.770 --> 00:11.670 In the previous video we learned how to create and use factory methods. 00:11.670 --> 00:17.070 In this video we will reuse the factory we created in the build a design pattern. 00:17.070 --> 00:22.290 We will see the similarities to solve the same problem using a different approach so that you can see 00:22.290 --> 00:25.350 the strengths and weaknesses of each approach. 00:25.350 --> 00:30.370 Finally we are going to create a new factory to create shipment orders. 00:30.540 --> 00:36.240 So here are the acceptance criteria for using the vehicle objects factory method. 00:36.300 --> 00:42.380 We must retrieve a vehicle object using a factory returned by the Abstract Factory. 00:42.480 --> 00:49.620 The vehicle must be a concrete implementation of a motorbike or a car that implements both interfaces 00:49.770 --> 00:54.530 that is vehicle and car or vehicle and motorbike. 00:54.600 --> 00:57.180 Let us take the unit test. 00:57.180 --> 01:01.020 This is going to be a long example so pay attention please. 01:01.050 --> 01:07.590 We will have these entities which you see on your screen vehicle which is the interface that all objects 01:07.590 --> 01:09.900 in our factories must implement. 01:09.900 --> 01:18.180 Next is motorbike an interface for motorbikes of the types sport one seat and Cruze for two seats. 01:18.240 --> 01:27.690 The third one is car an interface for cars of types luxury with autos and family with five doors will 01:27.690 --> 01:34.560 also have vehicle factory an interface or rather the Abstract Factory to retrieve factories that implement 01:34.590 --> 01:36.820 the vehicle factory method. 01:36.840 --> 01:42.930 Then comes the motorbike factory which is a factory that implements the vehicle factory interface to 01:42.930 --> 01:50.670 return vehicle that implements the vehicle and motorbike interfaces and the last one is car factory. 01:50.670 --> 01:56.040 Another factory that implements the vehicle factory interface to return vehicles that implement the 01:56.040 --> 01:59.990 vehicle and car interfaces for clarity. 02:00.000 --> 02:03.140 We are going to separate each entity into a different file. 02:03.150 --> 02:08.490 We will start with the vehicle interface which will be in the vehicle dot go file I'll show you the 02:08.490 --> 02:09.830 one I have created. 02:10.020 --> 02:16.690 So within the go design patterns package I have created another package and named it abstract underscore 02:16.710 --> 02:17.600 factory. 02:17.700 --> 02:23.780 And here are all those files which will be using and you also need to create these files. 02:23.790 --> 02:25.670 The first one is the vehicle Dot. 02:25.680 --> 02:28.290 Go file let me open this. 02:28.290 --> 02:32.900 It should have the get wheels and get seats in the vehicle interface. 02:32.970 --> 02:36.390 Now the car and motorbike interfaces will be in the car. 02:36.390 --> 02:40.760 Dot go and motorbike dot go files respectively. 02:41.010 --> 02:42.030 Let me show you both. 02:42.630 --> 02:44.980 So this is the car dot go interface. 02:45.180 --> 02:46.860 And this is the motorbike. 02:46.860 --> 02:48.780 Dot go interface. 02:48.780 --> 02:53.280 We have one last interface the one that each factory must implement. 02:53.280 --> 02:55.480 This is in the vehicle underscore factory. 02:55.480 --> 02:57.900 Dot go file and here's the interface. 02:57.900 --> 02:58.710 We add to it. 02:59.400 --> 03:02.580 So now we are going to declare the car factory. 03:02.580 --> 03:08.410 It must implement the vehicle factory interface defined previously to return vehicles instances. 03:09.000 --> 03:11.120 We do this in the car underscore. 03:11.180 --> 03:13.350 Factory don't go file. 03:13.350 --> 03:20.370 We have defined two types of cars luxury and family the car factory will have to return cars that implement 03:20.400 --> 03:22.480 the car and vehicle interfaces. 03:22.500 --> 03:25.860 So we need two concrete implementations. 03:25.860 --> 03:30.530 We have added these implementations in the luxury underscore car don't go. 03:30.570 --> 03:33.350 And the second one is in the family underscore. 03:33.360 --> 03:36.990 Car doors go file that's all for cars. 03:36.990 --> 03:43.510 Now we need the motorbike factory which like the car factory must implement the vehicle factory interface. 03:43.590 --> 03:49.490 So is the motorbike underscored battery not go file for the motorbike factory. 03:49.490 --> 03:55.610 We have also defined two types of motorbikes using the concept keywords which are sport motorbike type 03:55.820 --> 03:58.180 and Cruze motorbike type. 03:58.250 --> 04:03.110 We will switch over to the argument in the build method to know which type shall be returned. 04:03.200 --> 04:05.380 Let's write the two concrete motorbikes. 04:05.450 --> 04:06.880 That is the sports motorbike. 04:06.890 --> 04:11.350 Don't go and the crew's motorbike don't go to finish. 04:11.350 --> 04:16.980 We need the abstract factory itself which we put in the previously created vehicle underscore factory 04:17.010 --> 04:19.080 not go file. 04:19.070 --> 04:24.070 We're going to write enough tests to make a reliable check as the scope of this course does not cover 04:24.130 --> 04:26.080 100 percent of the statements. 04:26.080 --> 04:29.020 It will be a good exercise for you to finish these tests. 04:29.020 --> 04:32.510 So we first create a file named vehicle underscore. 04:32.520 --> 04:37.450 Factory underscore test not go where we add a motorbike factory test. 04:37.750 --> 04:43.900 We use the package method yet vehicle factory to retrieve a motorbike factory passing the motorbike 04:43.900 --> 04:50.920 factory type in the parameters and check if we get any error then already with the motorbike factory 04:51.040 --> 04:58.790 we ask for a vehicle of the type sport motorbike type and check for errors again with the return vehicle 04:58.790 --> 05:04.220 we can ask for methods and the vehicle interface get wheels and get seats. 05:04.220 --> 05:10.370 We know that it is a motorbike but we cannot ask for the type of motorbike without using the type assertion 05:10.880 --> 05:15.830 we use the type assertion on the vehicle to retrieve the motorbike that the motorbike vehicle represents 05:15.920 --> 05:16.930 in the code line. 05:17.000 --> 05:23.720 Sport bike bound equals Motorbike Vehicle dot motorbike and we must check that the type we have received 05:23.840 --> 05:25.350 is correct. 05:25.350 --> 05:28.180 Finally now we have a motorbike instance. 05:28.190 --> 05:33.300 We can ask for the bike type by using the get motorbike type method. 05:33.320 --> 05:37.380 Now we write a test that checks the car factory in the same manner. 05:37.580 --> 05:43.850 Again we use the get vehicle factory method to retrieve a car factory by using the car factory type 05:43.940 --> 05:45.050 in the parameters. 05:45.260 --> 05:51.300 With this factory we want to car the luxury type so that it returns a vehicle instance. 05:51.350 --> 05:56.720 We again do the type assertion to point to a car instance so that we can ask for the number of doors 05:56.900 --> 05:59.380 using the get to method. 05:59.480 --> 06:04.380 Now move on to the terminal and let's run the unit tests done. 06:04.490 --> 06:09.990 It can't recognize any factory as their implementation is still not done. 06:10.010 --> 06:12.460 Now let's move on to implementation. 06:12.670 --> 06:17.020 The implementation of every factory is already done for the sake of brevity. 06:17.090 --> 06:21.890 They are very similar to the factory method with the only difference being that in the factory method 06:22.100 --> 06:27.650 we don't use an instance of the factory method because we use the package functions directly. 06:27.650 --> 06:33.500 Let me show you the implementation of the vehicle factory is what you need to add to the code file. 06:33.500 --> 06:38.430 Let us save the file and go back to terminal like in any factory. 06:38.430 --> 06:44.100 We switched between the factory possibilities to return the one that was demanded as we have already 06:44.130 --> 06:46.340 implemented all concrete vehicles. 06:46.380 --> 06:48.310 The tests must run too. 06:48.390 --> 06:50.150 So let us run the test now. 06:50.950 --> 06:53.040 As you can see all of them passed. 06:53.110 --> 06:59.110 Take a close look and note that we have used the hyphen cover flag when running the tests to return 06:59.110 --> 07:03.420 a coverage percentage of the package fifty eight point three percent. 07:03.430 --> 07:08.370 This tells us that fifty eight point three percent of the lines are covered by the tests we have written 07:08.860 --> 07:13.090 but forty one point seven percent are still not under the tests. 07:13.090 --> 07:18.120 This is because we haven't covered the crews motorbike and the family car with the tests. 07:18.130 --> 07:24.370 If you write those tests the results should rise to around seventy point eight percent type assertion 07:24.520 --> 07:26.560 is also known as casting. 07:26.560 --> 07:32.050 In other languages when you have an interface instance which is essentially a pointer to a struct you 07:32.050 --> 07:36.170 just have access to the interface methods with type assertion. 07:36.190 --> 07:41.890 You can tell the compiler the type of the pointed struct so you can access the entire struct fields 07:42.010 --> 07:44.600 and methods cool. 07:44.740 --> 07:50.800 In this video we have learned how to write a factory of factories that provides us with a very generic 07:50.920 --> 07:57.220 Object of vehicle type in the next video we'll be learning about prototype design pattern.