Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should the interface define implementation specific enum values?

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?

like image 711
Jitesh Avatar asked Jan 07 '16 17:01

Jitesh


2 Answers

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.

like image 94
Sriram Sakthivel Avatar answered Oct 21 '22 15:10

Sriram Sakthivel


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

like image 30
Leonardo Spina Avatar answered Oct 21 '22 15:10

Leonardo Spina