1 00:00:01,070 --> 00:00:07,130 Let's first take a look at our flyweight object, which is a product object in our in our case, so 2 00:00:07,400 --> 00:00:14,060 we have only one property called Nien, which is final, and we don't have any setar for that for that 3 00:00:14,450 --> 00:00:15,080 property. 4 00:00:15,080 --> 00:00:21,680 We just assign a new name whenever we create a new a new product, which is in the constructor phase. 5 00:00:22,010 --> 00:00:25,120 So basically, this object here is immutable. 6 00:00:25,310 --> 00:00:32,870 So whenever we have multiple orders for the same product, we don't need to create multiple or product 7 00:00:32,870 --> 00:00:38,870 objects because this would affect the memory management in our program. 8 00:00:38,870 --> 00:00:47,950 We would we would need a lot of memory to store several thousand products that are ordered each day. 9 00:00:48,260 --> 00:00:53,300 So we would need to to optimize this a little bit. 10 00:00:53,330 --> 00:01:01,420 So for this, we applied the flyweight pattern in to deal with this deal, with this problem. 11 00:01:01,790 --> 00:01:11,960 So we have the order class here, which contains final property of this type of called order number, 12 00:01:11,960 --> 00:01:17,150 which is the order ID and also using composition will have a reference to the product that is being 13 00:01:17,150 --> 00:01:17,660 ordered. 14 00:01:18,440 --> 00:01:28,700 And for the sake of simplicity, for the marriage order method, I just inserted here a CSA statement 15 00:01:28,940 --> 00:01:32,040 which will output the order number and the name of the product. 16 00:01:32,930 --> 00:01:36,110 Also, we have the market which will be used to search for the products. 17 00:01:36,260 --> 00:01:44,390 And this method over here is quite important for us because it will output how many objects were created 18 00:01:46,100 --> 00:01:52,930 in the process, in the process for ordering the same products over and over and over again. 19 00:01:53,270 --> 00:02:00,590 So also this inventory back end will be the back end application will be the interface between what 20 00:02:00,590 --> 00:02:02,590 we've seen before and the client application. 21 00:02:02,810 --> 00:02:06,830 So the grant application is hitting this class with requests. 22 00:02:07,140 --> 00:02:14,210 So as you can see, we have to take order method which will be used to place place orders on different 23 00:02:14,210 --> 00:02:15,470 products in our market. 24 00:02:15,830 --> 00:02:23,280 And the process here will will output what we've seen before in the market in the order class. 25 00:02:23,360 --> 00:02:25,970 It will all put this out statement here. 26 00:02:26,390 --> 00:02:36,980 And this report, this final line here will will output how many objects were created in the whole process 27 00:02:36,980 --> 00:02:41,240 of of ordering the same objects, ordering the same products. 28 00:02:41,510 --> 00:02:44,000 So let's take a look in the application here. 29 00:02:44,300 --> 00:02:48,440 We create a new inventory back in first and then we take some orders. 30 00:02:48,440 --> 00:02:55,370 And as you can see, we order the same things twice with different order numbers. 31 00:02:56,570 --> 00:03:06,440 And then we call the process, which will basically output in the console old orders that we have with 32 00:03:06,740 --> 00:03:12,110 against all the products and with their corresponding order numbers. 33 00:03:12,470 --> 00:03:18,710 And in the end, we will report how many products will be created for all these orders here. 34 00:03:18,740 --> 00:03:20,090 So let's see this in action. 35 00:03:24,970 --> 00:03:32,950 So as you can see, we have all the orders here printed thanks to this to this method here, but in 36 00:03:32,950 --> 00:03:39,040 the end we can see that only three objects were created because we essentially ordered the same three 37 00:03:39,430 --> 00:03:42,720 products in all these six orders. 38 00:03:43,450 --> 00:03:49,270 So we are actually ordering them, ordering each product twice. 39 00:03:49,270 --> 00:03:54,640 And therefore, we will not create six objects, but we will create only three objects thanks to this 40 00:03:55,300 --> 00:03:59,940 flyweight pattern that helps us optimizing the memory management of our application. 41 00:04:00,970 --> 00:04:11,670 It can be a bit cumbersome to apply this pattern because it's a bit complex to to explain and also what 42 00:04:11,970 --> 00:04:18,870 most of the people are thinking about applying this a bit too early in the product lifecycle management. 43 00:04:18,910 --> 00:04:25,840 So basically, you won't need to think about memory optimization at the beginning of a project. 44 00:04:26,140 --> 00:04:31,220 You may need to think about it at a later stage in the in the product lifecycle. 45 00:04:31,480 --> 00:04:35,350 So therefore it's it can cause some problems. 46 00:04:35,950 --> 00:04:42,580 But it is a it is a very good strategy to optimize memory in your program, especially if you deal with 47 00:04:42,760 --> 00:04:47,680 immutable objects that need to be much need to be reused all the time. 48 00:04:47,860 --> 00:04:54,820 So instead of creating the same instances over and over again, use this flyweight pattern which will 49 00:04:54,820 --> 00:05:03,460 help you to reuse the same the same objects during the during the flow of the application. 50 00:05:04,180 --> 00:05:06,160 Also, caching is a very good idea. 51 00:05:06,190 --> 00:05:07,990 So gashing is a very good strategy. 52 00:05:07,990 --> 00:05:13,570 And it also can be thought of as a possible implementation of the flyaway pattern. 53 00:05:13,900 --> 00:05:21,580 But you can always juggle with with all these methods and you can use this this approach that you've 54 00:05:21,580 --> 00:05:23,820 seen in this video as well. 55 00:05:24,910 --> 00:05:25,710 So that's it. 56 00:05:26,260 --> 00:05:30,220 Join me in an editorial where we are going to discuss about the proxy battle.