This page is optimized for mobile devices, if you would prefer the desktop version just
click here
interface IChef {
String cookVeggie(Vegetarian h, Integer n);String cookMeat(Carnivore h, Integer n);
} |
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();
}} |
Ordering food from the chef
To order food from an IChef , a Vegetarian object simply calls cookVeggie, passing itself as one of the parameters, while a Carnivore object would call cookMeat, passing itself as one of the parameters as well. The Vegetarian and Carnivore objects only deal with the IChef object at the highest level of abstraction and do not care what the concrete IChef is. The polymorphism machinery guarantees that the correct method in the concrete IChef will be called and the appropriate kind of food will be returned to the AEater caller The table below shows the code for Vegetarian and Carnivore, and sample client code using these classes.
public class Vegetarian extends AEater {
// other methods elidedpublic String order(IChef c, int n) {
return c.cookVeggie(this, n);}
} |
public class Carnivore extends AEater {
// other methods elidedpublic String order(IChef c, int n) {
return c.cookMeat(this, n);}
} |
public void party(AEater e, IChef c, int n) {
System.out.println(e.order(c, n));}
// blah blah blah...AEater John = new Carnivore();
AEater Mary = new Vegetarian();party(Mary, ChefWong.Singleton, 2);
party(John,ChefZung.Singleton, 1); |
The above design is an example of what is called the visitor pattern.
- The abstract class AEater and its concrete subclasses are called the hosts. The method public String order(IChef c, Object n) is called the hook method. Each concrete subclasses of AEater knows exactly to call the appropriate method on the IChef parameter, but does know and need not how the IChef concretely perforns its task. This allows an open-ended number of ways to cook the appropriate kinds of food.
- The chef interface IChef and all of its concrete implementations are called visitors. When an IChef performs cookMeat/cookVeggie, it knows that its host is a Carnivore/Vegetarian and can only call the methods of Carnivore/Vegetarian to cook a dish. Java static type checking will flag an error should there be a call on the host to getCarot in the method cookMeat. This is makes the interaction between hosts (Vegetarian and Carnivore) and visitors (IChef and all of its concrete implementations) much more robust.
Read also:
OpenStax, Principles of object-oriented programming. OpenStax CNX. May 10, 2013 Download for free at http://legacy.cnx.org/content/col10213/1.37
Google Play and the Google Play logo are trademarks of Google Inc.