You are here:   ArielOrtiz.com > Software Design and Architecture > Lab 7: Singleton and Decorator Patterns

Lab 7: Singleton and Decorator Patterns

Objectives

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 7: Singleton and Decorator Patterns
    # Date: 12-Oct-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!"
      end
        
      def roar
        'Grrr!'
      end    
            
    end
    

    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!
          Tigger.new
        end
        assert_equal("I'm the only one!", t.to_s)
        assert_equal('Grrr!', t.roar)
      end
      
    end
  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:

    Beverages
    Name Price
    Dark Roast Coffee $0.99
    Espresso $1.99
    House Blend Coffee $0.89
    Condiments
    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 = 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.

    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 = 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
    

Deliverables

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

Due date is Tuesday, October 18.

Evaluation

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 (ariel.ortiz@itesm.mx)
Made with Django | Licensed under Creative Commons | Valid HTML5 | Valid CSS