You are here

The Chef

26 July, 2019 - 09:51
Available under Creative Commons-ShareAlike 4.0 International License. Download for free at http://cnx.org/contents/402b20ad-c01f-45f1-9743-05eadb1f710e@37.6

The chef is represented as an interface IChef with two methods, one to cook a vegetarian dish and one to cook a meat dish, as shown in the table below.

Table 3.8 Top-level abstract denition
interface IChef {
String cookVeggie(Vegetarian h, Integer n);
String cookMeat(Carnivore h, Integer n);
}
Table 3.9 Concrete implementations

public class ChefWong implements IChef {

 
public static final ChefWong Singleton
= new ChefWong();
 
private ChefWong() {}

public String cookVeggie(
Vegetarian h, Integer n) {

return  n + " portion(s) of " +
h.getCarrot() + ", " +
h.getSalt();
}

public String cookMeat(
Carnivore h, Integer n) {

return  n + " portion(s) of " +
h.getMeat() + ", " +
h.getPepper();
}
}

 
public class ChefZung implements IChef {

         public static final ChefZung Singleton
                = new ChefZung();

          private ChefZung() {}

          public String cookVeggie(
                 Vegetarian h, Integer n) {

          return n + " portion(s) of " +
                      h.getCorn() + ", " +
                      h.getSalt();
          }

          public String cookMeat(
                Carnivore h, Integer n) {

                 return n + " portion(s) of " +
                             h.getChicken() + ", " +
                             h.getPepper() +
                             ", " + h.getSalt();
          }
}