You are here: > Software Design and Architecture > Lab 6: Singleton and Decorator Patterns

Lab 6: Singleton and Decorator Patterns


During this lab session:

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

The lab activities can be developed individually or in pairs.

The lab report must be developed individually.

  1. Create a folder called singleton_decorator. Inside this folder, create four files called: tigger.rb, test_tigger.rb, coffee.rb, and test_coffee.rb.

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

    # Lab 6: Singleton and Decorator Patterns
    # Date: 1-Mar-2011
    # Authors:
    # 456654  Anthony Stark 
    # 1160611 Thursday Rubinstein
  2. You are given the following Ruby class that models the famous Tigger character from Winnie The Pooh:

    class Tigger
      def to_s
        return "I'm the only one!"
      def roar

    Convert this class into a singleton using whatever technique you find fit. Place the resulting class in the tigger.rb source file.

  3. Check your solution using the following test case (place the test in the source file test_tigger.rb):

    require 'test/unit'
    require './tigger'
    class TiggerTest < Test::Unit::TestCase
      def test_tigger
        t = Tigger.instance
        assert_same(t, Tigger.instance)
        assert_raise(NoMethodError) do       # "new" method should be private!

        assert_equal("I'm the only one!", t.to_s)
        assert_equal('Grrr!', t.roar)
  4. This example was taken from [FREEMAN] pp. 79-98. 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.

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

    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)


To hand in your individual lab work, follow these instructions.

Due date is Monday, March 7.


This activity will be evaluated using the following criteria:

50% Implementation of functional requirements.
50% Lab report.
DA The program and/or report was plagiarized.
© 1996-2011 by Ariel Ortiz (
Made with Django | Licensed under Creative Commons | Valid XHTML | Valid CSS