I have a scenario where I am designing a system for a retailer. This is not a proper live application but just a scenario to check whether my OO design skills are correct and whether I am thinking correctly. I am still learning here. I am doing this in c#.
The scenario is this:
A retailer selling stationary products wants to design a system that will select the best price from his various fixed number of suppliers and make an order from that supplier. For sake of simplicity, I cut the stationary products down to just one product from the same company, which is an XYZ pen. Each of the suppliers will provide a quote for the XYZ pen when asked and the retailer system selects the best price from the various sellers and places an order from that seller.
Approach 1:
- Create an abstract class for supplier and an implementation for each supplier.
- Each supplier implementation to have a PlaceOrder() method and a cost property.
- DataLayer sets the cost property for each supplier implementation.
- Create a CheckBestRetailer class that evaluates each implementation for the best price and places an order on the appropriate implementation.
Approach 2:
- Create a list of type supplier with Cost property and a PlaceOrder() method.
- For each supplier the data layer adds new supplier type to list and sets the cost details it got from the database.
- CheckBestRetailer class loops thru that list and evaluates each object for the best price and places an order on the appropriate implementation.
Of the two above, I feel approach 1 is closer to OOP but provided I have a fixed number of suppliers. If the number of suppliers can change depending on data retrieved from database then Approach 2 is more better.
What do you think?
I may not have the best scenario here for testing my OOAD. Would love to have some sample scenarios that I can work with too… if possible with design hints.
Thanks for your time.
Option 1 will be more appropriate if you have different behaviour for each supplier, in this case it seems that going with option 2 is a better choice as its simpler.
What I usually do when I don’t know how to solve something is:
for any of the above it doesn’t matter if you are wrong or not, is kind of a brainstorming to understand better what you have to achieve and what options you find to do it.
Every time you encounter something that seems to be difficult, I abstract it by either creating a method that returns you the answer you need or by creating a new class if that seems to make better sense.
As an exercise I try to think that the ‘difficult/complex’ parts will be coded by someone else and by delegating to a method or class I separate that from the part of the problem I’m focusing right now.
hth