How will you cache objects if we want characters with different font type. Assume there are thousands of font types . What data structure will you use for cache and what will be the key ?
Hello, Can you please change the caption of the video to reflect the correct sequence numbering for the LLD playlist.... It will be helpful for someone like me who is starting with the playlist from start. And keep up adding these patterns to the playlist.
Hi Sir, great video, a small doubt; while calling display function second time on same Humanoid Object, old robot object which was previously displayed on x=1,y=2, will move on new coordinates. Another robot will be created only in that case, if we create a new object. Kindly clarfiy.
Hi Shreyansh, 2 questions here: 1. Do we need cache in factory class to store all objects? What if we make the flightweight classes singleton? then we can get rid of cache and logic in factory method still will create only one object of one type? 2. In word processor example, shouldn't there be multiple concrete classes of various characters and fonts extending iletter interface? Also, the cache if we need to use it in factory class, shouldn't the key in the hashmap be combination of character and font to store different objects?
Flightweight class can not be Singleton. Because you can create more than 1 object like we did in Robot and processor example. Objects with unique data. Why we need to create unique class for each character? A field in a class is sufficient in my opinion. Yes i agree with you on part that, for word processor example, in cache we can generate a key by multiple combinations. But since our explanation, i used only 1 font Arial font, so based on character only i implemented the cache.
@@ConceptandCoding amazing video.. hats off 2 u sir, but still sir, why NOT singleton?... reason being we are just updating the X and Y coordinates of a SINGLE object only (by single I mean single in terms of type, i.e HUMANOID or Dog) Its the leverage of display function which is allowing us to update a single object, but still the previous position of the character / sprite printed remains intact, as we might had already done something like print(character / humanoid-sprite) we can't create it as a singleton , is this because of the fact that there are 2 implementations (humanoid and dog)of a same Interface IRobot ??.. if that so, can we create 2 separate interfaces ?.. then in that case it wont be needing cache, as we would have 2 different singleton classes ??
There is ane more reason that it can not be Singleton because let's take an example of Humanoid only, Let's say i have to create a green color robot, red color robot, now do i have to create different classes for each color of robot, no i can use the same class but update my Cache key. Hope that clarifies why Singleton will not work here
@@ConceptandCoding I am still not convinced about why we can't use singleton. Let's take the use case u mentioned(where we need different color robots): Case 1: If we take the color as attribute in humanoid robot class , if we use singleton we need to create 2 objects, so as in case of using cache in factory. Case 2: since the color is extrinsic, we can remove it as attribute and put it as parameter in display method, so in that case if we use singleton, we create only one object for humanoid and same as using caching. So I don't see any extra advantage of caching here.
@@kolasphoorti1030 don't think singleton would be efficient since if you go with that approach we would need to create 26 classes which will be a lot of duplicated code.Singleton only allows one instance of a class so we would have to create 26 classes for each character from scratch whereas in the example in the video they used only one class ILetter to hold 26 characters then 26 instances for each character is mapped to the Hash.
Hi a couple of doubts: 1) How thread-safe is the map logic? as it is quite similar to singleton null-check logic which needs lots of handling for thread safety? 2) why it is not advisable to make intrinsic data static so that it is shared among objects of the class? Thanks.
The map logic in the Flyweight pattern is not thread-safe by default and requires concurrency mechanisms like synchronized blocks or ConcurrentHashMap to ensure thread safety. Intrinsic data should not be static to avoid concurrent modification issues and complex lifecycle management. The Flyweight pattern aims to share immutable common state while allowing unique extrinsic state per instance.
Could we not pass the extrinsic data along in the constructor itself? Since we are utilizing a distinct object for each HumanRobot (flyweight object), what if we incorporate the extrinsic data as an attribute in the flyweight object? We could maintain these extrinsic fields as mutable, and when displaying, instead of passing them as parameters, we could directly utilize the object variables.
when we try to create a new object for same character, the first object is returned again but with different coordinate 29:43 But suppose we want to change the font size of first object, then we cant do that since now that same object is holding the different coordinate. suppose i type This is The Tiger all T have different positions and suppose different font. but our sole object for T will hold the position and font only for the last T and when we try to make some changes into the 3rd T, from UI, how will the word processor that we have designed handle this issue since it has now lost the info about previous all T
Hi, instrinic data is one which are common for all and do not change once set. If this example, i make it instrinic, means font size is same for all characters. But in your case, if it's changes, then we can make it extrinsic and passed in display method.
So First of all very very tanhkyou forthis video I get one doubt here as you are storing this object in map so when you get value from the map and store in a varibale as new variable will allocate new memory isn't it take again the space of object that required (31KB in this video ) and for every new Object it will again reallocate and memoery will increase So the thing will remain the same How it decreasaing the memory ? Please give some insights I am very confused Thanks
Great explanation Shreyansh, just one small doubt. For character t, all objects are pointing to same memory in heap, so what if i want to change 't' having some row, col to some other character. I know it is final, but in real word processor, i can do that. In this, if it is not immutable, it will change all `t` to that character in all row and col
Very good video. Crystal clear. Just 1 question - Is factory class thread safe ? Can we use concept of singleton design pattern inside factory class to make sure one and only one object is created ? Thank you very much ❤
Factory class is thread safe. In this scenario of factory method it worked on member variable which is HashMap, we can use concurrent HashMap if required.
Sir did I understand this pattern right plz let me know the advantage of creating method for extrinsic value is that we can reuse I ject in basis of common intrinsic value and the extrinsic value passed in method can be used for other logic. If we don't separate intrinsic and extrinsic value then we have to create object everytime for varying extrinsic value as thses values are intialised in constructor
In the Flyweight design pattern, intrinsic data, such as font or size, is shared among multiple objects. If the intrinsic data needs to change, you either create a new flyweight object with the updated data or update the existing flyweight object, ensuring that all dependent objects are updated accordingly.
Hello Sir, I just have a small doubt in game example you gave. In the example we are getting the same object from the cache and setting the coordinates so the position of the object will change. But What if we want to have multiple robot objects on the screen? Can we create multiple robot objects with same Sprite class and type of the robot as Sprite is the class which is taking lot of memory so we cache only sprite class only. Is that a good approach? also will it still be the example of Flyweight pattern too?
Yes Sprite is a 2D bitmap, which is used to create an object on screen. But yes this is just an example I think of, there can be different examples too.
Hey , if intrics data is not common but extrinsic is common than in this case can we implement flyweight pattern ? If no than why ? If yes than will the implementation be same ?
Design a banking system which allows a user to 1. Create an account 2. Withdraw or deposit funds 3. Check account balance 4. Deactivate account. Expose REST APIs in relation to these functions. No need of using a database, perform all calculations in-memory. Time limit was 1.5 hours. In the interview / review related to this round I was asked about how will you handle concurrency issues while doing multiple transactions involving withdrawal / deposit or transferring of funds from one account to another . please add this question to your bucket. @shreyansh
This is the best channel for Design Patterns ! No one explains like this!!
thanks for the feedback
Congrats! Shreyansh just laid out the foundation for the creation of the Borg!
Thanks for this explanation! Crystal clear!
Thank you
Very well explained!
tha
Shrayansh bro you are awesome 😎
Thank you
apart from my open questions same as to that of asked by Kola sphoorti, its an amazing video, and an honour for me to get education from Sheyansh.
I have answered to Kola question. Let me check if there is any follow up question too.
Thank you
God of DP. hatsoff to u bhaiyaa
Thank you
A little additional point in making the object immutable, make the variable final also 12:15.
can't we make type and body as static members and then use them instead of using cache?
How will you cache objects if we want characters with different font type. Assume there are thousands of font types . What data structure will you use for cache and what will be the key ?
character+type+size as key?
Hello,
Can you please change the caption of the video to reflect the correct sequence numbering for the LLD playlist.... It will be helpful for someone like me who is starting with the playlist from start.
And keep up adding these patterns to the playlist.
Yes, this is the latest videos, so i keep it at top for one day and will push it down in the playlist tomm
Hi Sir, great video, a small doubt; while calling display function second time on same Humanoid Object, old robot object which was previously displayed on x=1,y=2, will move on new coordinates. Another robot will be created only in that case, if we create a new object. Kindly clarfiy.
Nice Explaination, One question - where will we store the state of object (robot) i.e x & y pos - if we have multiple robots in the game?
in the cache right
Hi Shreyansh,
Awesome explanation. Can you make a video on designing a payment gateway?
Noted
Hi Shreyansh, 2 questions here:
1. Do we need cache in factory class to store all objects? What if we make the flightweight classes singleton? then we can get rid of cache and logic in factory method still will create only one object of one type?
2. In word processor example, shouldn't there be multiple concrete classes of various characters and fonts extending iletter interface? Also, the cache if we need to use it in factory class, shouldn't the key in the hashmap be combination of character and font to store different objects?
Flightweight class can not be Singleton. Because you can create more than 1 object like we did in Robot and processor example.
Objects with unique data.
Why we need to create unique class for each character? A field in a class is sufficient in my opinion.
Yes i agree with you on part that, for word processor example, in cache we can generate a key by multiple combinations.
But since our explanation, i used only 1 font Arial font, so based on character only i implemented the cache.
@@ConceptandCoding amazing video.. hats off 2 u sir, but still sir, why NOT singleton?... reason being we are just updating the X and Y coordinates of a SINGLE object only (by single I mean single in terms of type, i.e HUMANOID or Dog)
Its the leverage of display function which is allowing us to update a single object, but still the previous position of the character / sprite printed remains intact, as we might had already done something like print(character / humanoid-sprite)
we can't create it as a singleton , is this because of the fact that there are 2 implementations (humanoid and dog)of a same Interface IRobot ??.. if that so, can we create 2 separate interfaces ?.. then in that case it wont be needing cache, as we would have 2 different singleton classes ??
There is ane more reason that it can not be Singleton because let's take an example of Humanoid only,
Let's say i have to create a green color robot, red color robot, now do i have to create different classes for each color of robot, no i can use the same class but update my Cache key.
Hope that clarifies why Singleton will not work here
@@ConceptandCoding I am still not convinced about why we can't use singleton. Let's take the use case u mentioned(where we need different color robots):
Case 1: If we take the color as attribute in humanoid robot class , if we use singleton we need to create 2 objects, so as in case of using cache in factory.
Case 2: since the color is extrinsic, we can remove it as attribute and put it as parameter in display method, so in that case if we use singleton, we create only one object for humanoid and same as using caching.
So I don't see any extra advantage of caching here.
@@kolasphoorti1030 don't think singleton would be efficient since if you go with that approach we would need to create 26 classes which will be a lot of duplicated code.Singleton only allows one instance of a class so we would have to create 26 classes for each character from scratch
whereas in the example in the video they used only one class ILetter to hold 26 characters then 26 instances for each character is mapped to the Hash.
Hi a couple of doubts:
1) How thread-safe is the map logic? as it is quite similar to singleton null-check logic which needs lots of handling for thread safety?
2) why it is not advisable to make intrinsic data static so that it is shared among objects of the class?
Thanks.
The map logic in the Flyweight pattern is not thread-safe by default and requires concurrency mechanisms like synchronized blocks or ConcurrentHashMap to ensure thread safety.
Intrinsic data should not be static to avoid concurrent modification issues and complex lifecycle management. The Flyweight pattern aims to share immutable common state while allowing unique extrinsic state per instance.
How do you show all characters on screen with this structure?
rather than caching can we not create both as singleton
Could we not pass the extrinsic data along in the constructor itself? Since we are utilizing a distinct object for each HumanRobot (flyweight object), what if we incorporate the extrinsic data as an attribute in the flyweight object? We could maintain these extrinsic fields as mutable, and when displaying, instead of passing them as parameters, we could directly utilize the object variables.
when we try to create a new object for same character, the first object is returned again but with different coordinate 29:43
But suppose we want to change the font size of first object, then we cant do that since now that same object is holding the different coordinate.
suppose i type
This is The Tiger
all T have different positions and suppose different font. but our sole object for T will hold the position and font only for the last T and when we try to make some changes into the 3rd T, from UI, how will the word processor that we have designed handle this issue since it has now lost the info about previous all T
Hi, instrinic data is one which are common for all and do not change once set.
If this example, i make it instrinic, means font size is same for all characters.
But in your case, if it's changes, then we can make it extrinsic and passed in display method.
So First of all very very tanhkyou forthis video
I get one doubt here as you are storing this object in map
so when you get value from the map and store in a varibale as new variable will allocate new memory isn't it take again the space of object that required (31KB in this video )
and for every new Object it will again reallocate and memoery will increase
So the thing will remain the same How it decreasaing the memory ?
Please give some insights I am very confused
Thanks
Great explanation Shreyansh, just one small doubt. For character t, all objects are pointing to same memory in heap, so what if i want to change 't' having some row, col to some other character. I know it is final, but in real word processor, i can do that.
In this, if it is not immutable, it will change all `t` to that character in all row and col
Maybe we can change the letter type to being extrinsic too along with row and column being extrinsic, lemme know if you come across a better solution
Very good video. Crystal clear. Just 1 question - Is factory class thread safe ? Can we use concept of singleton design pattern inside factory class to make sure one and only one object is created ?
Thank you very much ❤
Factory class is thread safe.
In this scenario of factory method it worked on member variable which is HashMap, we can use concurrent HashMap if required.
@@ConceptandCoding Okay, thank you sir for the explanation.
Hi
why we dint make intrinsic to static so that it can be shared with all the robots?
Sir did I understand this pattern right plz let me know the advantage of creating method for extrinsic value is that we can reuse I ject in basis of common intrinsic value and the extrinsic value passed in method can be used for other logic.
If we don't separate intrinsic and extrinsic value then we have to create object everytime for varying extrinsic value as thses values are intialised in constructor
Sir waiting for ur reply 😊
can anyone tell how an object can be simultaneously present at multiple coordinates?
Ouick Question : why there is a need to have two subclasses ( humanoid and robotic dog) when we have type and body attributes in the class ?
how to handle font/size change in this pattern? Basically when intrinsic data is getting changed?
In the Flyweight design pattern, intrinsic data, such as font or size, is shared among multiple objects. If the intrinsic data needs to change, you either create a new flyweight object with the updated data or update the existing flyweight object, ensuring that all dependent objects are updated accordingly.
Bahya jo lld hld ki common videos he
Like how distributed computing works
Vo lld wali playlist me bhi add kardo
i have kept HLD and LLD topics in respective playlist buddy
Hello Sir, I just have a small doubt in game example you gave. In the example we are getting the same object from the cache and setting the coordinates so the position of the object will change. But What if we want to have multiple robot objects on the screen? Can we create multiple robot objects with same Sprite class and type of the robot as Sprite is the class which is taking lot of memory so we cache only sprite class only. Is that a good approach? also will it still be the example of Flyweight pattern too?
Yes Sprite is a 2D bitmap, which is used to create an object on screen.
But yes this is just an example I think of, there can be different examples too.
Quick question is it necessary to code LLD questions in JAVA in an interview ? can we use typescript instead ?
Any OOPs language can be used
Hey , if intrics data is not common but extrinsic is common than in this case can we implement flyweight pattern ? If no than why ? If yes than will the implementation be same ?
I did not get the question, intrics data mean common data only.
How extrinsic will be common (if it's common then it's not extrinsic)
@@ConceptandCoding oh got it ok, that means if we don't have intrics data than we cannot apply flyweight pattern, right?
@@gurupreetsingh8347 yes
@@ConceptandCoding thanks buddy
Is this course comprehensive for an interview ?
If you understand the concept it would really help you in interview
@@ConceptandCoding Thank you sir. One more question. This is good for Microsoft fresher system design interview ???
memory is limited x10 us bro moment
Design a banking system which allows a user to 1. Create an account 2. Withdraw or deposit funds 3. Check account balance 4. Deactivate account. Expose REST APIs in relation to these functions. No need of using a database, perform all calculations in-memory. Time limit was 1.5 hours. In the interview / review related to this round I was asked about how will you handle concurrency issues while doing multiple transactions involving withdrawal / deposit or transferring of funds from one account to another . please add this question to your bucket. @shreyansh
Thanks buddy.
I am adding it