Let's first take a look at our flyweight object, which is a product object in our in our case, so
we have only one property called Nien, which is final, and we don't have any setar for that for that
property.
We just assign a new name whenever we create a new a new product, which is in the constructor phase.
So basically, this object here is immutable.
So whenever we have multiple orders for the same product, we don't need to create multiple or product
objects because this would affect the memory management in our program.
We would we would need a lot of memory to store several thousand products that are ordered each day.
So we would need to to optimize this a little bit.
So for this, we applied the flyweight pattern in to deal with this deal, with this problem.
So we have the order class here, which contains final property of this type of called order number,
which is the order ID and also using composition will have a reference to the product that is being
ordered.
And for the sake of simplicity, for the marriage order method, I just inserted here a CSA statement
which will output the order number and the name of the product.
Also, we have the market which will be used to search for the products.
And this method over here is quite important for us because it will output how many objects were created
in the process, in the process for ordering the same products over and over and over again.
So also this inventory back end will be the back end application will be the interface between what
we've seen before and the client application.
So the grant application is hitting this class with requests.
So as you can see, we have to take order method which will be used to place place orders on different
products in our market.
And the process here will will output what we've seen before in the market in the order class.
It will all put this out statement here.
And this report, this final line here will will output how many objects were created in the whole process
of of ordering the same objects, ordering the same products.
So let's take a look in the application here.
We create a new inventory back in first and then we take some orders.
And as you can see, we order the same things twice with different order numbers.
And then we call the process, which will basically output in the console old orders that we have with
against all the products and with their corresponding order numbers.
And in the end, we will report how many products will be created for all these orders here.
So let's see this in action.
So as you can see, we have all the orders here printed thanks to this to this method here, but in
the end we can see that only three objects were created because we essentially ordered the same three
products in all these six orders.
So we are actually ordering them, ordering each product twice.
And therefore, we will not create six objects, but we will create only three objects thanks to this
flyweight pattern that helps us optimizing the memory management of our application.
It can be a bit cumbersome to apply this pattern because it's a bit complex to to explain and also what
most of the people are thinking about applying this a bit too early in the product lifecycle management.
So basically, you won't need to think about memory optimization at the beginning of a project.
You may need to think about it at a later stage in the in the product lifecycle.
So therefore it's it can cause some problems.
But it is a it is a very good strategy to optimize memory in your program, especially if you deal with
immutable objects that need to be much need to be reused all the time.
So instead of creating the same instances over and over again, use this flyweight pattern which will
help you to reuse the same the same objects during the during the flow of the application.
Also, caching is a very good idea.
So gashing is a very good strategy.
And it also can be thought of as a possible implementation of the flyaway pattern.
But you can always juggle with with all these methods and you can use this this approach that you've
seen in this video as well.
So that's it.
Join me in an editorial where we are going to discuss about the proxy battle.