1 00:00:00,930 --> 00:00:07,350 The flyweight pattern is used for memory optimization purposes, the design pattern is usually chosen 2 00:00:07,350 --> 00:00:10,680 whenever there is a need to reduce the number of created objects. 3 00:00:11,730 --> 00:00:17,160 The reason why this pattern has been included in a structural pattern is categories because it is used 4 00:00:17,160 --> 00:00:23,460 to optimize the object structures by reducing the memory footprint of your program through creating 5 00:00:23,460 --> 00:00:25,260 a smaller number of objects. 6 00:00:27,100 --> 00:00:32,830 The scope of it is to revitalize the already created objects and to avoid creating new instances whenever 7 00:00:32,830 --> 00:00:35,800 an object is needed in our code. 8 00:00:35,830 --> 00:00:40,330 We will have an online store composed of a backend application and a client application. 9 00:00:41,710 --> 00:00:47,500 The backend application consists of the inventory back, an object which is used to process the order 10 00:00:47,510 --> 00:00:48,960 sent by the client application. 11 00:00:50,080 --> 00:00:55,480 The orders are created on the market object and the orders contain different products. 12 00:00:56,990 --> 00:01:02,330 The product object is our flyweight object, therefore we will demonstrate that whenever two orders 13 00:01:02,330 --> 00:01:08,270 are created on the same product, only one instance of that product will be created by our application. 14 00:01:08,840 --> 00:01:11,690 Therefore, reducing the memory usage of the program. 15 00:01:13,350 --> 00:01:18,610 As a general rule, this pattern is suitable only for stateless or immutable objects. 16 00:01:19,620 --> 00:01:24,660 The definition for an immutable object is whenever an object cannot be changed after its creation. 17 00:01:25,770 --> 00:01:28,860 Such an example is the string object from the job IEEPA. 18 00:01:30,660 --> 00:01:36,330 Some of the biggest weaknesses of this pattern is that it can become a bit too complex, being more 19 00:01:36,330 --> 00:01:39,870 of an optimization solution for issues related to memory management. 20 00:01:41,040 --> 00:01:44,000 Let's have a look at the implementation of this pattern in the code.