During this activity:
This activity helps students develop the following skills, values and attitudes: ability to analyze and synthesize, capacity for identifying and solving problems, and efficient use of computer systems.
This activity must be developed in the pre-assigned teams of two.
This example was taken from [FREEMAN] pp. 79-98.
Create a directory called
decorator and a subdirectory
src within it. Inside the
src folder create two files called:
All Ruby source files must start with a comment containing title, date, and the authors' personal information. For example:
# Decorator Pattern # Date: 21-Apr-2022 # Authors: # A01160611 Thursday Rubinstein # A01777771 Stephen Strange
Write the code that allows us to model coffee beverages using the Decorator pattern. The following tables show what beverages and condiments are available, and their corresponding prices:
|Dark Roast Coffee||$0.99|
|House Blend Coffee||$0.89|
In our design, the condiments will decorate the beverages. The following Ruby code demonstrates how your code could be used:
beverage = DarkRoast.new beverage = Mocha.new(beverage) beverage = Whip.new(beverage) puts beverage.description puts beverage.cost
For this example, the expected output should be:
Dark Roast Coffee, Mocha, Whip 1.29
The following image depicts the way the decorators work when the
cost method is called from the above code.
The following UML diagram shows the class hierarchy. Each class
needs to implement one or several of these methods:
All these classes should be placed in the
The following unit tests verify the correct behavior of your
classes. Place the test class in the
# File: coffee_test.rb require 'minitest/autorun' require 'coffee' class CoffeeTest < Minitest::Test def test_espresso beverage = Espresso.new assert_equal("Espresso", beverage.description) assert_equal(1.99, beverage.cost) end def test_dark_roast beverage = DarkRoast.new beverage = Milk.new(beverage) beverage = Mocha.new(beverage) beverage = Mocha.new(beverage) beverage = Whip.new(beverage) assert_equal("Dark Roast Coffee, Milk, Mocha, Mocha, Whip", beverage.description) assert_equal(1.59, beverage.cost) end def test_house_blend beverage = HouseBlend.new beverage = Soy.new(beverage) beverage = Mocha.new(beverage) beverage = Whip.new(beverage) assert_equal("House Blend Coffee, Soy, Mocha, Whip", beverage.description) assert_equal(1.34, beverage.cost) end end
Create a compressed tarball file with the full contents of the
decorator directory (including the generated HTML documentation in the doc subdirectory). Call this file
decorator.tgz. From a terminal, you can use the following command to create this file (make sure to run it at the same level where the
decorator folder resides):
tar czf decorator.tgz decorator
To deliver the
decorator.tgz file, please provide the following information:
Only one team member needs to upload the file.
Due date is Thursday, April 21.
This activity will be evaluated using the following criteria:
|50%||Implementation of program requirements.|
|1||The program and/or documentation was plagiarized.|