Search code examples
design-patternsoop

An interview question: hot dog, chips, drink, meal (OO Design, Design Pattern. )


Design a system about a food cart. Suppose you got hot dog, chips and drinks to sell and customers can also make it a meal.

1.Design a system for this situation.

2 What Design Patterns can you use here? Describe it?

Anybody can give me some hints? You can either give a brief idea or detailed description. Thank you guys.


Solution

  • FoodCartFactory that can produce multiple instances of FoodCart.

    FoodCart is the main class.

    You need to use IoC, since the cart should be indifferent to what type of food is sold. The cart has a hash table of instances of descendants of FoodItem abstract class, which represents the various items sold.

    Using DI would also be a good idea, since the prices can vary depending on location and so on, so the factory has to configure the cart with a PolicyManager object that controls the prices.

    Also, a FoodCartController would ensure that the cart is stocked with the right items.

    You also need a CartCustomer object representing the target of the FoodSale message, which will contain the item, amount and price (including appropriate taxes, of course).

    You also need some business logic, in a class called ProfitMaximizer, where the calculations of what is sold and how much, in order to pass the necessary FoodNeeded messages to the FoodController.

    Of course, the ProfitMaximizer can't work well without some sort of analytics, probably in a class called FoodStatistics, which tracks sales, trends, margins, customer number and so on.

    You also need some kind of diagnostics support, represented by class called FoodCartLogger, to collect information for diagnosing and troubleshooting problems with the cart.

    Obviously, you also need to be concerned with the performance of the cart, so FoodCartPerfMonitor is waranted.

    Then again, we should not skip over the possibility that this cart needs to serve customers in various location around the world, so you obviously need ResourceManager and FoodCartLocalizer.

    You also need a subsystem to track the available capital, based on the income and expenses flow. Let's call it FoodCartAccountManager.

    And if you have any profit left, it should be automatically invested to diversify the income, so of course ExchangeTrader class wouldn't be out of question.

    Since the cart is manipulated by a person, you will also need Vendor class. Vendors tend to stay relatively shortly, so you will have a lot of churn you'll have to manage with a VendorManager class.

    Of course, all these classes are necessary to control one cart; if you need to design a system that can deal with multiple carts, you'll have to add few other components. On the bright side, though, lot of the classes above obviously can deal with more than one cart, so you can reuse them.

    I think that about covers the basic design.