You are here:   ArielOrtiz.com > Software Design and Architecture > Lab 2: Observer and Decorator Patterns

Lab 2: Observer and Decorator Patterns

Objectives

During this lab session:

This activity helps the student 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 lab is based on chapters 2 and 3 of [FREEMAN]. It can be developed individually or in pairs.

The following ZIP file contains the files and folders you'll be needing for this lab: observer_decorator.zip. Any new files you create must go inside the corresponding folder.

  1. Carefully study the following NUnit text fixture:

    namespace Headfirst.Observer.Weather {
    
        using System;    
        using NUnit.Framework;
        
        [TestFixture]
        public class TestObservers {
            
            static readonly string EOL = Environment.NewLine; 
    
            [Test]
            public void TestDisplays() {
                var weatherData = new WeatherData();
                
                new CurrentConditionsDisplay(weatherData);
                new StatisticsDisplay(weatherData);
                new ForecastDisplay(weatherData);
                new CelsiusDisplay(weatherData);
                                        
                Assert.AreEqual(
                    "Current conditions: 80.0F degrees and 65.0% humidity" + EOL 
                    + "Avg/Max/Min temperature = 80.0/80.0/80.0" + EOL
                    + "Forecast: Improving weather on the way!" + EOL
                    + "Temperature: 26.67 degrees Celsius" + EOL,
                    weatherData.SetMeasurements(80, 65, 30.4f));
                
                Assert.AreEqual(
                    "Current conditions: 82.0F degrees and 70.0% humidity" + EOL
                    + "Avg/Max/Min temperature = 81.0/82.0/80.0" + EOL
                    + "Forecast: Watch out for cooler, rainy weather" + EOL
                    + "Temperature: 27.78 degrees Celsius" + EOL,
                    weatherData.SetMeasurements(82, 70, 29.2f));
                
                Assert.AreEqual(
                    "Current conditions: 78.0F degrees and 90.0% humidity" + EOL
                    + "Avg/Max/Min temperature = 80.0/82.0/78.0" + EOL
                    + "Forecast: More of the same" + EOL
                    + "Temperature: 25.56 degrees Celsius" + EOL,
                    weatherData.SetMeasurements(78, 90, 29.2f));            
            }
        }
    }

    Write the class Headfirst.Observer.Weather.CelsiusDisplay so that it behaves exactly as expected by the unit test. Do not modify any other part of the code.

    TIP 1: The formula to convert degrees Fahrenheit into Celsius is: °C = (°F - 32) 5 / 9.

    TIP 2: Use the String.Format static method in order to define how data elements should be formatted inside a string.

  2. Carefully study this NUnit text fixture:

    namespace Headfirst.Decorator.Starbuzz {
        
        using NUnit.Framework;
        using System;
        
        [TestFixture]
        public class TestDecorator {
    
            [Test]
            public void TestCafeDeOlla() {            
                Beverage beverage = new CafeDeOlla();            
                beverage = new CinnamonWithSugar(beverage);
                Assert.AreEqual("Café de Olla, Cinnamon with Sugar", 
                    beverage.Description);
                Assert.AreEqual(1.75, beverage.Cost(), 2);
            }
        
            [Test]
            public void TestDecaf() {
                Beverage beverage = new Decaf();
                beverage = new Milk(beverage);
                beverage = new CinnamonWithSugar(beverage);
                Assert.AreEqual("Decaf Coffee, Milk, Cinnamon with Sugar", 
                    beverage.Description);
                Assert.AreEqual(1.40, beverage.Cost(), 2);
            }
        
            [Test]
            public void TestDarkRoast() {
                Beverage beverage = new DarkRoast();
                beverage = new Mocha(beverage);
                beverage = new Mocha(beverage);
                beverage = new Whip(beverage);
                beverage = new CinnamonWithSugar(beverage);
                Assert.AreEqual("Dark Roast Coffee, Mocha, Mocha, Whip, Cinnamon with Sugar", 
                    beverage.Description);
                Assert.AreEqual(1.74, beverage.Cost(), 2);
            }
        }
    }

    You need to write two classes:

    • Headfirst.Decorator.Starbuzz.CafeDeOlla
    • Headfirst.Decorator.Starbuzz.CinnamonWithSugar

    Make sure that they behave exactly as expected by the unit tests. Do not modify any other part of the code.

Deliverables

To hand in your lab work, follow these instructions:

Evaluation

This activity will be evaluated using the following criteria:

100 The code works as requested.
60-90 The code works, but has some flaws.
20-50 The code doesn't work, but it seams that some amount of time was spent on it.
DA The program was plagiarized.
© 1996-2009 by Ariel Ortiz (ariel.ortiz@itesm.mx)
Made with Django | Licensed under Creative Commons | Valid XHTML | Valid CSS