Chain of responsibility command can also be used. We can define the coupons that can be applied as commands and chain the ones that we want to apply for each product. It would be similar to you decorator pattern implementation only.
With chain of responsibility each time additional information has to be passed to specify which coupon needs to be applied. Like in the chain of responsibility video the type needs to be passed. However, for decorator it is not needed.
You cant create each time a decorator once there is s new type of coupon. This will not work in a high scale syatem. keep coupon and product seperate at any cost. I appreate your effort but its not necesary to add design pattern in every LLD problem.
CoR might work better than decorator. Both process given data in a given order then Why CoR can be better? 1. Decorator gives client freedom to wrap different layers given their requirement. Here it's not needed CoR doesn't give this freedom to client and can help enforce single order of sequence across the client 2. Decorator works good when layers can be optional so that client can pick one and drop other. Here we want each layer MUST process and see if coupon is eligible then apply otherwise not apply and forward data. Which again enforced by CoR
6:10 It makes no sense for having multiple classes Item1 and Item2 which are having exactly same functionality. For 1000s of Items you will have 1000 Data Type classes? Two objects of Item1 class would be different if they have different properties like name, type etc which will have different hascode() result. I don't think ShoppingCart should store list of products after applying coupon, If you are needing to remove coupons from an item it won't be possible. Ordering of coupon will result in different price, this is not made clear in question. Instead of decorating product with coupon in the shoppingCart, there could have been functionality to pass this info down from the controller (main class here). Question - If you are actually needing to write implementation for a coupon type - D% off on Nth item of type T, How will you approach it with this design? Since this was not covered in the video.
Hi Umang, D% of the Nth item Type or any other specific requirements, we can easily add that business logic in the respective decorator class. If you see I have passed the Itemslist to TypeDecorator, it we have use that to add custom logic whether to apply discount or not or if required we can pass additional info from shopping cart too. And regarding creating class for each item. It you check my Inventory Management system, there I have handled that scenario (that we should not be creating 1000s of classes for each item) But in start itself i mentioned this question we can easily diverted to inventory management kind of logic, which requires more handling. So I tried to stay focus on the question specific which i think how to apply different coupons on items. But yes there can be other better approach buddy, i would love to see some other solutions and let's see if we can improve this solution. Could you please share UML of your approach,l through LinkedIn?
@@ConceptandCoding I think instead of having decorator on Product class, it would be better to have it on Cart class, So we have CouponDecorator which has Cart and is a type of some abstract class Cart. This will solve multiple issues in the existing design: 1.Each decorator type needn't take different fields, every decorator has access to cart and even for case of D% of nth item can be handled 2. In the existing design, we are applying all available coupons on an existing product. Usually user applies chooses coupons and they are applied even though multiple are eligible. Also when adding a coupon or removing coupon(tied to cart not to individual product), makes no sense to remove existing products and adding each product by re-applying the coupons. So by tying it to cart, user has more flexibility in adding and removing coupons dynamically. Only those coupons which user applied can be applied in order(sequence of coupon type) on cart.
@@kolasphoorti1030 Regarding your point, "if item removed there is no point of reapplying all the coupons on the existing items" But if you look at the ask, it's possible that when some item is removed from the cart, it might impact the discount eligibility for other items other(possible that some discount which got applied, now it's not eligible and vice versa) So reapplying has to be done in my view. But eager to see your solution, could you please share UML, we can discuss more on that.
By definition, Cart doesn't have items in any order/sequence. So, asking to apply x% discount on nth/next item in a cart is illogical. The question could be to calculate effective price of a cart, after applying a set of coupons. There can be coupons applicable per each item (some may be specific to an item type) and coupons applicable on the overall cart price(eg: if total price exceeds Rs.1000, you get an extra 5% discount). Here, order matters, once all the item-specific coupons are applied, only then cart coupon can be applied. This is an imaginary cart in the Q with abnormal behavior.
I was asked the same question in the LLD interview, I asked the same question to the interviewer but no response from him. I suggested a timestamp based approach like coupon can have timestamp, by seeing that we can know the next immediate item in the cart and we can apply to it. Got rejected
@@ramsankar585totally relatable. Interviewer has no clarity on the question, he must have read it somewhere along with a Solution. So, if you give him a different answer, he thinks it's wrong. If you ask questions, which he can't answer, his ego gets hurt :)
@@hemanthaugust7217 I agree with your point, it's difficult to confront interviewers sometimes. I couldn't understand this requirement as i just saw it but hypothetically speaking, this coupon type 'D% off on Nth item of type T' could be corresponding to a scenario like 'Get 10% off on your 3rd purchase of item T'. So it's more related to User and their overall purchase history, instead of current cart status only. But that increases the scope and complexity of this question
Hey Shreyansh. Firstly kudos on such nice LLD series. Great work. For the above vedio, you mentioned we can use decorator pattern for applying one or more coupons on cart. But generally on 1 cart we can apply only one coupon and 1 offer. should it be more of chain of responsibility pattern?
Shouldnt we be using chain of responsibility here? My coupons chained together and applied to cart. With decorator, how will we handle case when a discount is to be applied to the nth item of same type?
Hi Shrayansh, I think this we can also solve by using the Strategy pattern right, in that case what can be the missing advantage which the decorator can provide us?
I have one question i.e when you are calling PercentageCouponDecorator inside TypeCouponDecorator i.e this TypeCouponDecorator(PercentageCouponDecorator(product, 10),........) but first parameter of TypeCouponDecorator constructor must be a product but PercentageCouponDecorator(product, 10) is not returning anything so how it is working as a first parameter.
How are we verifying if the element is next item or nth item of type t ? (Conditions for coupon 2 and 3). Shouldn't we instead supply cartId to coupons and apply decorator pattern then ?
Here I have passed the Itemslist, to specific decorator class, there we can add logic to handle specific scenario, like I did for type decorator. If required we can add more parameters from cart class.
Every month i switch between HLD and LLD, this month i have covered 3 topics of LLD, 1 more i will do. Next month I will do HLD, expect 3-4 HLD videos. I have shared the roadmap in 1st video, there are some videos left buddy
When you said Decorator pattern, I instantly recalled your Pizza example. Your way of teaching is so good. Thanks for the video ❤
Chain of responsibility command can also be used. We can define the coupons that can be applied as commands and chain the ones that we want to apply for each product. It would be similar to you decorator pattern implementation only.
With chain of responsibility each time additional information has to be passed to specify which coupon needs to be applied. Like in the chain of responsibility video the type needs to be passed. However, for decorator it is not needed.
You cant create each time a decorator once there is s new type of coupon. This will not work in a high scale syatem. keep coupon and product seperate at any cost. I appreate your effort but its not necesary to add design pattern in every LLD problem.
exactly my point.
Hey, do you have a better solution to the problem? Could you please share ?
Question in morning, solution in the evening. This seems like a good exercise 👍
it's recently asked question in a good company, so I was little eager to solve this
I always have difficulty implementing is-a relationship in real World projects, thanks for showing this.
Thanks
CoR might work better than decorator. Both process given data in a given order then Why CoR can be better?
1. Decorator gives client freedom to wrap different layers given their requirement. Here it's not needed
CoR doesn't give this freedom to client and can help enforce single order of sequence across the client
2. Decorator works good when layers can be optional so that client can pick one and drop other. Here we want each layer MUST process and see if coupon is eligible then apply otherwise not apply and forward data. Which again enforced by CoR
6:10 It makes no sense for having multiple classes Item1 and Item2 which are having exactly same functionality. For 1000s of Items you will have 1000 Data Type classes? Two objects of Item1 class would be different if they have different properties like name, type etc which will have different hascode() result.
I don't think ShoppingCart should store list of products after applying coupon, If you are needing to remove coupons from an item it won't be possible.
Ordering of coupon will result in different price, this is not made clear in question.
Instead of decorating product with coupon in the shoppingCart, there could have been functionality to pass this info down from the controller (main class here).
Question - If you are actually needing to write implementation for a coupon type - D% off on Nth item of type T, How will you approach it with this design? Since this was not covered in the video.
Hi Umang,
D% of the Nth item Type or any other specific requirements, we can easily add that business logic in the respective decorator class.
If you see I have passed the Itemslist to TypeDecorator, it we have use that to add custom logic whether to apply discount or not or if required we can pass additional info from shopping cart too.
And regarding creating class for each item.
It you check my Inventory Management system, there I have handled that scenario (that we should not be creating 1000s of classes for each item)
But in start itself i mentioned this question we can easily diverted to inventory management kind of logic, which requires more handling.
So I tried to stay focus on the question specific which i think how to apply different coupons on items.
But yes there can be other better approach buddy, i would love to see some other solutions and let's see if we can improve this solution.
Could you please share UML of your approach,l through LinkedIn?
@@ConceptandCoding I think instead of having decorator on Product class, it would be better to have it on Cart class, So we have CouponDecorator which has Cart and is a type of some abstract class Cart. This will solve multiple issues in the existing design:
1.Each decorator type needn't take different fields, every decorator has access to cart and even for case of D% of nth item can be handled
2. In the existing design, we are applying all available coupons on an existing product. Usually user applies chooses coupons and they are applied even though multiple are eligible. Also when adding a coupon or removing coupon(tied to cart not to individual product), makes no sense to remove existing products and adding each product by re-applying the coupons. So by tying it to cart, user has more flexibility in adding and removing coupons dynamically. Only those coupons which user applied can be applied in order(sequence of coupon type) on cart.
@@ConceptandCoding I think better approach would be visitor pattern
@@kolasphoorti1030
Regarding your point, "if item removed there is no point of reapplying all the coupons on the existing items"
But if you look at the ask, it's possible that when some item is removed from the cart, it might impact the discount eligibility for other items other(possible that some discount which got applied, now it's not eligible and vice versa)
So reapplying has to be done in my view.
But eager to see your solution, could you please share UML, we can discuss more on that.
@@kolasphoorti1030 Can you share the UML?
I am so happy that I am able to think on my own. Thanks sir for the videos🙂
Please add more videos related to LLD and tips on how to approach an actual interviews
Sure will do
By definition, Cart doesn't have items in any order/sequence. So, asking to apply x% discount on nth/next item in a cart is illogical.
The question could be to calculate effective price of a cart, after applying a set of coupons.
There can be coupons applicable per each item (some may be specific to an item type) and coupons applicable on the overall cart price(eg: if total price exceeds Rs.1000, you get an extra 5% discount).
Here, order matters, once all the item-specific coupons are applied, only then cart coupon can be applied.
This is an imaginary cart in the Q with abnormal behavior.
I was asked the same question in the LLD interview, I asked the same question to the interviewer but no response from him. I suggested a timestamp based approach like coupon can have timestamp, by seeing that we can know the next immediate item in the cart and we can apply to it. Got rejected
@@ramsankar585totally relatable. Interviewer has no clarity on the question, he must have read it somewhere along with a Solution. So, if you give him a different answer, he thinks it's wrong. If you ask questions, which he can't answer, his ego gets hurt :)
@@hemanthaugust7217 I agree with your point, it's difficult to confront interviewers sometimes. I couldn't understand this requirement as i just saw it but hypothetically speaking, this coupon type 'D% off on Nth item of type T' could be corresponding to a scenario like 'Get 10% off on your 3rd purchase of item T'. So it's more related to User and their overall purchase history, instead of current cart status only. But that increases the scope and complexity of this question
Why did we choose to create Product as an abstract class and not as an Interface?
Hey Shreyansh. Firstly kudos on such nice LLD series. Great work.
For the above vedio, you mentioned we can use decorator pattern for applying one or more coupons on cart. But generally on 1 cart we can apply only one coupon and 1 offer. should it be more of chain of responsibility pattern?
it can, just based on the requirement or ask from the interviewer we can modify it a bit. but you are right
Shouldnt we be using chain of responsibility here? My coupons chained together and applied to cart. With decorator, how will we handle case when a discount is to be applied to the nth item of same type?
It can, try to implement it, and pls share your UML through LinkedIn buddy
Where can I find the code for this ? Please share the gitlab link
How would something like, buy 2 get 1 free coupon work? You probably need the product and frequency as well.
Hi Shrayansh, I think this we can also solve by using the Strategy pattern right, in that case what can be the missing advantage which the decorator can provide us?
If we use strategy pattern then for different combination of coupons that can be applied different strategies need to be created.
Shrayansh, can you pls push the code to your gitlab? Thanks.
Why Chain of Responsibility can not be applied to this???
had same question , i think we can try to implement this on our own with chain of responsibility
Asked in Swiggy i think .
I have one question i.e when you are calling PercentageCouponDecorator inside TypeCouponDecorator i.e this TypeCouponDecorator(PercentageCouponDecorator(product, 10),........) but first parameter of TypeCouponDecorator constructor must be a product but PercentageCouponDecorator(product, 10) is not returning anything so how it is working as a first parameter.
how this will solve D%off on Nth item of Type T?
Could you please upload these solutions to any repository, it's easier to refer. Thanks for making the videos.
Sure will do it here:
gitlab.com/shrayansh8/interviewcodingpractise/-/tree/main/src/LowLevelDesign
@@ConceptandCoding Where is the code for this video ? The repo you shared does not have this video's source code
How are we verifying if the element is next item or nth item of type t ? (Conditions for coupon 2 and 3). Shouldn't we instead supply cartId to coupons and apply decorator pattern then ?
You can add that logic while adding product in the cart.
Where is this code uploaded ?
With decorator, how will we handle the case of applying discount to the Nth item?
Here I have passed the Itemslist, to specific decorator class, there we can add logic to handle specific scenario, like I did for type decorator.
If required we can add more parameters from cart class.
Hello @ConceptandCoding , Can you please upload code and share link ?
Sure will add the link in description
Hi sir how many videos are remaining for lld playlist and when will u continue hld playlist
Thank u for providing such a great content ☺️
Every month i switch between HLD and LLD, this month i have covered 3 topics of LLD, 1 more i will do. Next month I will do HLD, expect 3-4 HLD videos.
I have shared the roadmap in 1st video, there are some videos left buddy
@@ConceptandCoding ok sir got it ,still can u tell roughly how much videos for lld
where can I get this code ?
Can you please share the code link for this LLD?
i will upload the code and share
Where is this code uploaded ?
Will upload today
is thre ny code link?
I will push by eod at: gitlab.com/shrayansh8/interviewcodingpractise/-/tree/main/src/LowLevelDesign
code??
@@ConceptandCoding