It's not a secret anymore!
The moose likes Java in General and the fly likes Need info on how to design Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Need info on how to design" Watch "Need info on how to design" New topic

Need info on how to design

Sriram Sharma
Ranch Hand

Joined: Apr 12, 2006
Posts: 126
Hi All,

This is a specific interview question that my friend got while attending an interview.
Sharing it here to understand different views on how to design the system.
Here goes the question...

There is a node/site named "International Recipes"
It has 3 branches namely "India", "Australia" and "Canada"
Each of these have three branches namely "Vegetarian" and "Non-Vegetarian"
Under Vegetarian there are two branches namely "Potato Recipes" and "Tomato Recipes"
Under Non-Vegetarian, there are two branches namely "Chicken Recipes" and "Mutton Recipes"

Each of these nodes, including the primary node can have subscribers.
If any recipe is added to any one node/branch, immediately subscriber of the relevant node should get intimation.

How do we design a java code for this?


[Thumbnail for Recipe_Design.JPG]

Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3151

So here is my design:

Then anything which can be subscribed has to implement Subscribable. So here goes the interface

Then I would consider that the countries can be grouped into Regions and each of the country can actually have a list of recipes available in that country. But before that, we need to have a Recipe class, each of the recipes can extend this class. As of now I am thinking that there's nothing unique or different between veg and non veg recipes except that each of the recipe will have it own method of preparation. So I would consider having an attribute to indicate whether its a veg or non veg. Also anyone can subscribe to the recipe. So it goes on something like:

Or another possible approach would be to use Marker interface for indicating if its a Vegetarian or NonVegetarian.

Then there is Region, India extends Region, Australia extends Region, Canada extends Region. And each region can also be subscribed. And each region would have a list of its own recipes, so we can define them something like:

The advantage of using Subscribable interface is that being able to Subscribe and being a recipe are 2 different things, so merging them into one would be like having multiple responsibilities for a single class. Also recipes being arranged in a hierarchy of countries would cause problems if the same recipe would be used in different countries. So I chose using composition of recipes in Countries over putting them in a hierarchy. Moreover Recipe and Country dont confirm to IS-A principle.

This would be my first cut design. Can be improved as and when we get more details about the application.

Mohamed Sanaulla | My Blog
I agree. Here's the link:
subject: Need info on how to design
It's not a secret anymore!