Factory Method Pattern for LLD Interviews | BLR Traffic <-> Dating Apps Example
? Now that I have your attention, if you're preparing for LLD interviews, read further to solidify your understanding of FACTORY METHOD design pattern with two examples 1 - related to Blr traffic, 2 - related to Dating Apps.
? Broadly speaking, design patterns fall into 3 categories:
Factory Method pattern falls under Creating Objects category.
? Example - 1: Bangalore Traffic Example -
?? Requirement:
?? Observation:
You just got a chance to bring in abstraction at the level of CREATING these Uber / Ola / Rapido objects for client usage. So basically, this design pattern wants to abstract away from the client-code, as-to HOW a particular object is created and HOW it's used.
That my friend, is Factory Method Pattern.
Don't worry if you did not understand it well yet. Let's take one more shot at it.
? Example - 2 : Tinder/Bumble for getting a prospective date!
?? Requirement:
?? Observation:
?? The Factory Method separates product construction code from the code that actually uses the product. Therefore it’s easier to extend the product construction code independently from the rest of the code.
领英推荐
?? Hopefully the above example gave some insight into WHEN/WHY to use FACTORY method pattern.
?? If you've made it this far, pat your back! Let's delve deeper into the class design for this pattern.
1 - DatingApp interface - this just contains a skeleton of what features of the dating application you want to expose to the client.
2 - Concrete Implementations of above interface - These would have CUSTOM logic of curating matches for folks on Tinder / Bumble by implementing the getMatches() method.
3 - DatingAppCreator - This class declares the factory method (createApp()) that returns new DatingApp objects. Important to note, that return type for the createApp() method needs to be DatingApp.
4 - TinderAppCreator/BumbleAppCreator - This class, overrides the base factory method (createApp()) to return a different type of DatingApp (TinderApp or BumbleApp) or any other app that you end up searching for matches on, tomorrow!
?? Now that we have written the code responsible for creating different kinds of dating applications, let's see how you'd invoke them in the client code.
?? In the main client code, you could just do :
?? Benefits:
?? When to use:
???Remember the above points in your next interview. The interviewer would be like below:
?? This is my first attempt at sharing an article on a design pattern. Please excuse me for mistakes and more importantly if any feel free to correct me in comments / DMs.
?? Your like / comment / share would by-default motivate me for further articles on other patterns and multi-threading!
???Godspeed!
Principal Engineer at Arcesium
2 年I mainly see two approaches of factory on various blogs., 1. Have static factory method with switch case to return DatingApp with identifier(most blogs mention this) https://www.digitalocean.com/community/tutorials/factory-design-pattern-in-java 2. This post , few blogs mention this. Clearly 2nd is trying to avoid if else and future modifications to factory class when new Dating App is added, but I still like the first one because it makes client code free of implementation details and responsibilities of creating concrete classes. Another question, if client has to create concrete factory for each new DatingApp added, that's double the amount of files added Also, why not just create datingApp instance directly from client code instead of creating an instance of factory and asking it to create DatingApp instance(it's like not using factory at all because the client has to deal with creation of concrete implementations )
SDE-2 at Atlassian | Ex-Microsoft
2 年The examples are pretty nice ! Now, people might be more excited about using this pattern rather than going out on a date ??
SWE @Google | Candidate Master at Codeforces | NSIT CSE '23
2 年Loved the article.
Versatile Professional: Solution Architect | Technical Lead | Project Manager | Fullstack Specialist in Java, JavaScript, Microservices, and Cloud
2 年Sounds interesting. Giving it a shot Nikhil Srivastava