Consider the following organization of classes:
interface Restaurant
{
public void dine(Object dish);
}
class ItalianRestaurant implements Restaurant
{
public void dine(Object dish)
{
// eat with spoon and forks
}
}
class ChineseRestaurant implements Restaurant
{
public void dine(Object dish)
{
// eat with chopsticks
}
}
Since both the restaurants serve completely different sets of dishes, what will be the correct way (design-wise) to represent the type
of dish
in the interface?
Will it be a good design decision to define an Enum listing all the dishes -- Italian and Chinese -- as a part of the interface, and use that Enum as type
for dish
?
You've used Object
type for a Dish. Meaning anything can be considered as a Dish here.
Does that mean you can serve a Phone
or Pen
as a Dish? No; A dish is a dish. Why not create an abstraction for Dish as well?
Better design would be :
interface Restaurant
{
void dine(Dish dish);
}
Dish
could be an interface or an abstract class; Choose one which is more appropriate. Then every kind of dish served will go as a separate class which inherits/implements Dish
.
I think the most correct way would be defining a generic interface like
interface Restaurant<T> {
public void dine(T dish);
}
EDIT: well, I'm thinking in c#.... don't know if it suits
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With