& Timothy Budd
Chapter 26 – An Introduction to Design Patterns
Your email address:
Your student ID number:
Once this form has been customized for your
institution, you can use this button to send your lab work. Be sure to read the instructions before starting your work.
To gain experience with
the advantages of
the pattern concept
several common design
patterns: ITERATOR, ADAPTER, TEMPLATE METHOD, STRATEGY, and COMPOSITE
patterns used in the
standard C++ library
putting patterns to
work in a complex program
Assume you are writing software for a restaurant. In
preparation for this task and subsequent problems within this lab, you will
need to implement two classes. One for representing the
nutritional value of an item of food and the other for representing a drink.
First, create a base class called FoodItem. This
class should contain a number of items such as calorie count, amount of fat,
amount of sodium, and amount of carbohydrates. Include constructors and
accessor functions as indicated by the class definition provided below. Also,
include a single function called printNutritionLabel(). This function should neatly print out a nutrition label
for the food to the screen. The foodName variable
should store the name of the food represented by the object. (i.e., french fries, hamburger, etc.).
FoodItem(string foodName, double iSodium, double iFat,
double iCarbohydrates, double iSugar,
Now, implement a class called DrinkItem.
A drink item is very similar to a FoodItem. Implement
the DrinkItem class according to the definition
DrinkItem(string drinkName, int iCalories, double iCarbs,
Supply your answer in the space below
You will now get some practice in writing adapter
patterns. A drink may be considered a type of food. Therefore, it may be useful
for you to create an adapter for converting a DrinkItem
to a FoodItem. Define and implement such as class in
the space below.
Write a short main()
function to test the three classes written in the previous problems within this
lab. You should create both drink and food items and test
your adapter class for converting from drink to food.
P3. More Testing
Modify your main()
function from Problem P2 such that it tests the new functionality.
Insert all code in the space below.
P4. Composite Classes
In this problem, you will learn
to program with the composite pattern. Define and implement a ValueMeal class that can store a number of FoodItem objects as specified by the constructor. The ValueMeal class is a composite class. The ValueMeal should contain a function called printNutritionLabel() that adds up the various nutrition
fields from each class that ValueMeal is composed of
and then prints this totaled nutrition label neatly to the screen.
Do not forget to send your answers when you are