1 00:00:01,300 --> 00:00:07,660 The next batter that we are going to discuss is the factory pattern, so the factory pattern is one 2 00:00:07,660 --> 00:00:16,210 of the most common design patterns out there and definitely it's widely used even by the developers 3 00:00:16,210 --> 00:00:26,920 that created Java, one of the libraries that are inside that sit inside core Java distribution. 4 00:00:27,220 --> 00:00:34,300 The calendar, the calendar framework is created using the factory budget. 5 00:00:34,750 --> 00:00:42,100 Basically, according to this pattern, you create an object factory that will create whatever object 6 00:00:42,100 --> 00:00:42,820 you may need. 7 00:00:45,070 --> 00:00:52,660 Let's discuss a bit about the advantages of using the factory pattern so it will hide the internal logic 8 00:00:52,660 --> 00:00:53,770 of creating objects. 9 00:00:54,400 --> 00:01:00,490 The client will know next to nothing about what it takes to create your object. 10 00:01:02,410 --> 00:01:06,490 It will enable the programmer to add different objects of the same type. 11 00:01:06,520 --> 00:01:15,190 So basically you will need to modify anything to in order to add a new object, you will just make use 12 00:01:15,190 --> 00:01:22,470 of of the factory and you will extend its capabilities to create other objects as well. 13 00:01:23,530 --> 00:01:27,280 Some of the disadvantages are the complexity of this pattern. 14 00:01:27,580 --> 00:01:29,080 So it's quite complex. 15 00:01:29,080 --> 00:01:35,980 And you will see from the diagram that it's actually a bit difficult to to implement it. 16 00:01:36,250 --> 00:01:41,210 And also, you cannot refactor your current code into a factory either. 17 00:01:41,500 --> 00:01:45,700 You will need to think about using factory pattern from the beginning. 18 00:01:47,110 --> 00:01:50,910 This is an example that we will use in our code. 19 00:01:50,980 --> 00:01:53,980 So basically we have an interface old computer. 20 00:01:54,490 --> 00:02:02,500 The computer will have a method called compute and there were there will be three objects that will 21 00:02:02,500 --> 00:02:04,240 implement this interface. 22 00:02:04,690 --> 00:02:07,360 So we will have laptop, phone and smart TV. 23 00:02:08,020 --> 00:02:12,070 All three of them will have different computer implementations. 24 00:02:12,880 --> 00:02:20,350 And alongside them you will have a computer, you have a computer factory actually that will have a 25 00:02:20,350 --> 00:02:26,660 method, get a computer that will return a type of computer. 26 00:02:26,680 --> 00:02:32,070 One of the three objects that implement this this interface. 27 00:02:32,470 --> 00:02:35,110 So let's see in the code how this looks like.