S/W Design and Architecture

Decorator Pattern

Objectives

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 directory called decorator and a subdirectory src within it. Inside the src folder create two files called: coffee.rb, and coffee_test.rb.

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

    # Decorator Pattern
    # Date: 20-Apr-2020
    # 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:

    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.

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

    # 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
    
  4. Write and generate your program’s documentation as described in: Documenting Ruby Programs.

Deliverables

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

Upload Instructions

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

Request PIN

Only one team member needs to upload the file.

Due date is Monday, April 20.

Evaluation

This activity will be evaluated using the following criteria:

50% Implementation of program requirements.
50% Documentation.
1 The program and/or documentation was plagiarized.