I came across a post on Leetcode forum about OOP design and the user who posted claimed that he was asked that question in his / her interview.
Here's the implementation of the above problem that I came up with based on my understanding of the question. In an interview, I am sure we could clarify and ask questions and implementations may vary.
Here I tried to include the following:
- Admin will be able to change and add data of any product, i.e. coffee, soft drinks or could be anything.
- Screen class will be notified by Admin when Admin changes or adds any data and Screen class's responsibility is to only show and present said data.
- CoffeeMaker or SoftDrinksMaker are like API which will enable us to add different types of drink. SoftDrinksMaker is an extra API added to demonstrate that this API can be easily extendable and satisfies the condition that in future, Machine can support different types of drinks.
- Admin will be able to take input from User and add as many drinks as users want and it will update Screen automatically as I also implemented listener pattern.
Here's the implementation code in Kotlin:
In Main method, I added necessary code to test all functionality provided by API and different classes. In my opinion, this can be improved further; however, I could do this without any help, so I am content with that. In my opinion, this problem is designed to test your ability to understand abstraction and polymorphism concepts which are essential in an OOP design.
If you have any suggestions to improve or can find any mistakes in the implementation of OOP principles then please comment. Also feel free to ask questions if you want to learn, I am happy to explain.
0 Comments