You are here: > S/W Design and Architecture > Decorator Pattern

Decorator Pattern


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.

Activity Description

This activity can be developed individually or in pairs.

This example was taken from [FREEMAN] pp. 79-98.

  1. Create a folder called decorator. Inside this folder, create two files called: coffee.rb, and tc_coffee.rb.

    All Ruby source files must start with a comment containing the lab's title, date, and the authors' personal information. For example:

    # Decorator Pattern
    # Date: 17-Oct-2014
    # Authors:
    #          A00456654 Thursday Rubinstein 
    #          A01160611 Anthony Stark
  2. 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:

    Name Price
    Dark Roast Coffee $0.99
    Espresso $1.99
    House Blend Coffee $0.89
    Name Price
    Mocha $0.20
    Whip $0.10
    Soy $0.15
    Milk $0.10

    In our design, the condiments will decorate the beverages. The following Ruby code demonstrates how your code could be used:

    beverage =
    beverage =
    beverage =
    puts beverage.description
    puts beverage.cost

    For this example, the expected output should be:

    Dark Roast Coffee, Mocha, Whip

    The following image depicts the way the decorators work when the cost method is called from the above code.

    A decorated beverage.

    The following UML diagram shows the class hierarchy. Each class needs to implement one or several of these methods: initialize, description, and cost.

    Beverage class hierarchy.

    All these classes should be placed in the coffee.rb source file.

  3. The following unit tests verify the correct behavior of your classes. Place the test class in the tc_coffee.rb source file.

    # File: tc_coffee.rb
    require 'test/unit'
    require 'coffee'
    class MyTest < Test::Unit::TestCase
      def test_espresso
        beverage =
        assert_equal("Espresso", beverage.description)
        assert_equal(1.99, beverage.cost)
      def test_dark_roast
        beverage =
        beverage =
        beverage =
        beverage =
        beverage =
        assert_equal("Dark Roast Coffee, Milk, Mocha, Mocha, Whip", beverage.description)
        assert_equal(1.59, beverage.cost)
      def test_house_blend
        beverage =
        beverage =
        beverage =
        beverage =
        assert_equal("House Blend Coffee, Soy, Mocha, Whip", beverage.description)
        assert_equal(1.34, beverage.cost)


Create a compressed tarball file with the full contents of the decorator directory. 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

✔ Upload Instructions

To deliver the decorator.tgz file, please provide the following information:

Request PIN

Due date is Sunday, October 19.