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 her开发者_开发知识库e? Describe it?
Anybody can give me some hints? You can either give a brief idea or detailed description. Thank you guys.
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.
I would do something like this..
class FoodCart extends Vendor {
List<FoodItem> foodItems;
List<Meal> meals;
}
class Vendor {
List<SellableItem> sellableItems;
int saleRevenue;
}
class SellableItem {
int price;
int quantity;
/**
* @return true if the item is sold out
*/
public boolean soldOut() {
return quantity == 0;
}
}
class FoodItem extends SellableItem {
Date expirationDate;
//more food-specific variables
}
class Meal {
FoodItem[] foodItems;
/**
* The meal cannot be sold if any one component making up the meal is sold out.
* @return true, if none of this meal's components is sold out.
*/
boolean sellable() {
boolean result = true;
for (int i = 0; i < foodItems.getLength(); i++) {
result &= !foodItems[i].soldOut();
}
return result;
}
}
And there it is. Drinks are FoodItems as well. Tell me what you think.
精彩评论