<< Chapter < Page Chapter >> Page >

2. to cook or not to cook

Mary is a vegetarian.  She only cooks and eats vegetarian food.  John is carnivorous.  He cooks and eats meat!  If Mary wants to eat broccoli and cheese, she can learn how to cook broccoli and cheese.  If she wants corn of the cob, she can learn how to cook corn on the cob.  The same goes for John.  If he wants to eat greasy hamburger, he can learn how to cook greasy hamburger.  If he wants to eat fatty hotdog, he can learn how to cook fatty hotdog.  Every time John and Mary want to eat something new, they can learn how to cook it.  This requires that John and Mary to each have a very big head in order to learn all the recipes.

But wait, there are people out there called chefs!  These are very special kinds of chefs catering only to vegetarians and carnivores.  These chefs only know how to cook two dishes: one vegetarian dish and one meat dish.  All John and Mary have to do is to know how to ask such a chef to cook their favorite dish.  Mary will only order the vegetarian dish, while John will only order the meat dish!

How do we model the vegetarian, the carnivore, the chef, the two kinds of dishes the chef cooks, and how the customer orders the appropriate kind of dish from the chef?

The food

To simplify the problem, let's treat food as String.  (In a more sophisticated setting, we may want to model food as some interface with veggie and meat as sub-interface.)

The food consumers

Vegetarians and carnivores are basically the same animals.  They have the basic ingredients such as salt and pepper to cook food.  They differ in the kind of raw materials they stock to cook their foods and in the way they order food from a chef.  Vegetarians and Carnivores can provide the materials to cook but do not know how to cook!  In order to get any cooked meal, they have to ask a chef to cook for them. We model them as two concrete subclasses of an abstract class called AEater . AEater has two concrete methods, getSalt and getPepper , and an abstract method called order , as shown in the table below.

Top-level abstract definition
public abstract class AEater { public String getSalt() {return "salt"; }public String getPepper() { return "pepper";} /*** Orders n portions of appropriate food from restaurant r. */public abstract String order(IChef r, Integer n); // NO CODE BODY!}
Concrete implementations
public class Vegetarian extends AEater{ public String getBroccoli() {return "broccoli"; }public String getCorn() { return "corn";} public String order(IChef c, Object n) {// code to be discussed later; }} public class Carnivore extends AEater{ public String getMeat() {return "steak"; }public String getChicken() { return "cornish hen";} public String getDog() {return "polish sausage"; }public String order(IChef c, Object n) { // code to be discussed later;} }

The chef

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.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  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.

Notification Switch

Would you like to follow the 'Principles of object-oriented programming' conversation and receive update notifications?

Ask