Open Closed Principle from Neha of IGDTUW for your next LLD Interview.
Amazon and Flipkart is in a e-commerce war.
Time to market is crucial.
In this war, the discount battle is gaining lot of heat.
Neha, Senior SWE and her team at Flipkart is spearheading the discounts division from Kormangala office.
Sachin, CEO is sitting in a war room with Neha and Team.
Sachin is constantly negotiating with investor for more funds.
After 2H long call, Sachin finally gets the nod.
“BOOM, We have the funds” said Sachin.
Sachin shouts in joy “Neha we have the funds, lets offers 10% discount in electronics, 15% in footwear and 30% in clothes”
Neha replies “Sachin, our customer will have these discounts by tomorrow”
Neha quickly writes a Discount class with a public method Calculate
Calculate method has 3 conditions, If product category
is electronics discount 10%
is footwear 15%
is Clothes 30%
In the next 5H Neha and her team
Tests
Builds
Deploy
Neha calls Sachin, “Sachin, the discounts are up and running”
The marketing team promotes this like crazy.
Flipkart starts receiving 30K orders per minute.
Revenue is off the roof.
But Profits?
SriRam CFO calls Sachin as says “We are losing $1M an hour, we need to stop this mayhem”
Sachin got a plan.
Sachin calls Neha “Neha we need to update the discount, I need 10% discounts only on Samsung and Motorola mobile phones, 15% discount of women footwear and 12%
Neha thinks, “What can I do now to save this situation?”
Neha is worried because if she adds all this features in the same Discount class
1/ The class will be cluttered with conditionals
2/ Future changes will make the class more cluttered.
3/ What if adding some more changes breaks the functionality?
Neha is a smart girl.
Neha recollects about Open Closed Principle she read in college.
A class should be open for extension, but closed for modification.
"Voila'" said Neha.
Neha fixes her hair bun, puts on the Sony headphones, puts Slack on DND.
Refactoring starts.
1/ DiscountStrategy interface is created.
2/ DiscountStrategy interface has a calculateDiscount method
3/ Electronics, Apparels, Footwears are divided in seperate classes which implements DiscountStrategy
4/ Discount class now has a public method applyDiscount which accepts product strategy..
5/ applyDiscount class applies appropriate discount based on the product passed.
Neha made the changes in 2H and asked her junior colleagues to implement the new changes from Sachin in the appropriate classes.
Each merchandise category has its own class which made
1/ Maintenance a breeze
2/ Testing effective
3/ Extensibility a no problem.
Neha has just put Flipkart back in the game reducing the time to market, adaptability and scalability at par with Amazon.
Neha gets promoted to Staff Software Engineer.
--------
Some more considerations:
- OCP at granular helps managing massive softwares breeze.
- In a real system you'd need a Factory method to fetch the discount rules that will apply to the product on runtime.
- Discount engines are very complex and takes years to mature.
- SRP is for optimising classes, OCP is to organize classes.
- Neha is a fictional character
----
If you liked the content do
Like to donate dopamine shot.
Comment to share your views.
Repost to show you discovered something cool.
---------
I am Sagar, I write stories to make building Software and Life Easy.
Go Follow me now.