1 00:00:01,780 --> 00:00:06,850 Yes, exactly, but there is another type of operational pattern, which is very similar with what we've 2 00:00:06,850 --> 00:00:08,680 seen previously with the factory captain. 3 00:00:09,780 --> 00:00:17,970 They have the same advantages and disadvantages, meaning that they hide very well the details of the 4 00:00:17,970 --> 00:00:25,020 implementation for creating an object, but also they are very difficult to refactor your current code 5 00:00:25,020 --> 00:00:25,590 base. 6 00:00:25,590 --> 00:00:32,790 And so basically, you need to think about having a factory pattern from the beginning, from the beginning, 7 00:00:32,790 --> 00:00:34,080 from the design phase. 8 00:00:35,430 --> 00:00:42,420 And a big difference is the fact that instead of having a single object factory, which will create 9 00:00:42,420 --> 00:00:49,320 all the objects for you, you have a super factory which will create a factory that will create finally 10 00:00:50,540 --> 00:00:52,230 the object that you're interested in. 11 00:00:53,490 --> 00:01:00,720 This is the diagram of the abstract factory pattern, so you can see we have here the abstract factory, 12 00:01:00,750 --> 00:01:05,700 which is an abstract class or an interface which has a computer method. 13 00:01:05,970 --> 00:01:14,210 This should be in a segmented and your classes that will extend this will need to implement this this 14 00:01:14,220 --> 00:01:14,580 method. 15 00:01:15,900 --> 00:01:22,800 The next phase is to have a computer factory, a generic factory, which will create a computer like 16 00:01:22,800 --> 00:01:23,660 we've seen before. 17 00:01:24,360 --> 00:01:30,990 But also we can have different types of factories like portable computer factory, for example, which 18 00:01:30,990 --> 00:01:38,610 will create a specific type of computer that will have that will share this trait, this this feature 19 00:01:38,610 --> 00:01:45,730 being being a portable one and something independent from from this structure here. 20 00:01:45,750 --> 00:01:52,290 It's the factory producer which will create the factory that we are interested in, depending on the 21 00:01:52,290 --> 00:01:53,610 object that you need to create. 22 00:01:53,850 --> 00:01:59,810 You might need to create a portable computer factory or another type of factory. 23 00:02:00,150 --> 00:02:08,310 But sticking with this example, we will see how we create an object using this design pattern. 24 00:02:08,320 --> 00:02:09,810 So let's take a look in the code. 25 00:02:11,110 --> 00:02:14,940 Let's start with the abstract glass here, the abstract factory glass. 26 00:02:15,400 --> 00:02:22,690 As you can see, this is an abstract method which will be implemented in the concrete implementation 27 00:02:22,690 --> 00:02:26,860 of this of this glass, which will return the computer. 28 00:02:26,860 --> 00:02:35,110 And the method will accept parameter of type string, which will contain basically the computer type 29 00:02:35,410 --> 00:02:38,650 that we need to create in our factory. 30 00:02:39,880 --> 00:02:44,590 We then have two factories that we created. 31 00:02:44,710 --> 00:02:48,270 So we have a computer factory like we've seen before. 32 00:02:48,280 --> 00:02:51,970 It has the same the same implementation here. 33 00:02:52,300 --> 00:03:03,130 But also we have a portable factory which will extend the factory and will have only the means to create 34 00:03:04,180 --> 00:03:07,330 some portable computers like a laptop or a phone, for example. 35 00:03:08,140 --> 00:03:14,260 So the third one, the third type of computer or other other types that are not portable will be left 36 00:03:14,260 --> 00:03:22,280 out of this implementation and something that is new for specific for this design. 37 00:03:22,280 --> 00:03:31,210 But is the factory producer and we can see here that it produces a factory that we need to that we need 38 00:03:31,210 --> 00:03:31,820 in our code. 39 00:03:31,840 --> 00:03:39,550 So basically, if we need a portable one, we will just supply this to this method. 40 00:03:39,700 --> 00:03:42,900 We will just supply the billion portable set to true. 41 00:03:43,120 --> 00:03:50,800 And as you can see, it will return a portable computer in this case or if this will be sent to force, 42 00:03:50,800 --> 00:03:53,200 it will return a basic computer factory. 43 00:03:54,630 --> 00:03:56,490 The rest of the code is still the same. 44 00:03:56,580 --> 00:04:05,880 So we have an interface of type computer that has this compute method here and for each type of computer, 45 00:04:05,880 --> 00:04:11,760 the computer method will return a different line in the console, as you can see here. 46 00:04:12,760 --> 00:04:17,590 All right, so let's take a look at this in action, so we have this at class, there are the main method 47 00:04:17,590 --> 00:04:22,060 here which will create an abstract factory of this type. 48 00:04:22,060 --> 00:04:31,180 And as you can see, we we are just calling the get factory method from this factory producer class, 49 00:04:31,180 --> 00:04:32,350 which is a static method. 50 00:04:32,360 --> 00:04:40,720 So we just we don't need to have a factory producer object created beforehand and we supply this portable 51 00:04:42,190 --> 00:04:45,400 this portable boolean value to false. 52 00:04:45,970 --> 00:04:53,230 And here when we create a new computer, we just do computer, factory, computer, and we will select 53 00:04:53,230 --> 00:04:54,360 an old portable one. 54 00:04:55,150 --> 00:05:03,620 And here we should have the line that is specific to this implementation printed out in the console. 55 00:05:03,640 --> 00:05:06,070 So let's just test that. 56 00:05:08,490 --> 00:05:13,210 So, as we expected, we have this line correctly, I'll put it here. 57 00:05:13,860 --> 00:05:17,520 So this is the abstract factory in a nutshell. 58 00:05:18,990 --> 00:05:25,920 Of course, this is overly simplified example, but you may have seen that it's a bit complex. 59 00:05:25,920 --> 00:05:34,920 So it's actually very, very difficult to refactor your code, to adapt, to adapt it to an abstract, 60 00:05:35,100 --> 00:05:41,490 abstract factory or even a factory pattern, because you need to create all these classes beforehand 61 00:05:41,490 --> 00:05:49,340 and you need to take into account a lot of a lot of aspects of object creation. 62 00:05:49,530 --> 00:05:57,900 So it's better to think of designing your code from the beginning with an abstract factory or a factory 63 00:05:59,880 --> 00:06:03,120 and address all the changes from the beginning. 64 00:06:04,260 --> 00:06:08,890 That's it for for this for this type of of pattern. 65 00:06:08,920 --> 00:06:12,810 Join me in the next story on where we are going to discuss about the Singleton pattern.