You are here:   ArielOrtiz.com > Software Design and Architecture > Lab 1: Strategy Pattern

Lab 1: Strategy Pattern

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 chapter 1 of [FREEMAN]. You will need to have installed Mono and NUnit. It can be developed individually or in pairs.

  1. Create a directory called chapter01 and place inside it the contents of this file: strategy.zip. Any new files you create must also go into this directory.
  2. Write a class called Headfirst.Strategy.MexicanQuack that implements the quack behavior interface. Its quack method must return the following string: "Quack, Quack, Ajua".
  3. Write a class called Headfirst.Strategy.RancheroDuck that inherits from the duck class. Its flying behavior must be provided by the fly with wings class, while its quack behavior must be provided by the Mexican quack class. Its display method must return the following string: "I'm a Ranchero duck".
  4. Write a class called Headfirst.Strategy.ComposableDuck that also inherits from the duck class. The constructor for this class should receive as parameters both the flying and quacking behaviors. Its display method should return a string like this: "I'm a duck composed of X and Y", where X and Y are the names of the flying and quacking behavior classes, respectively.
  5. Check your code using the following test cases:

    namespace Headfirst.Strategy {
        
        using NUnit.Framework;
        
        [TestFixture]    
        public class TestStrategy {
            
            [Test]
            public void TestMexicanQuack() {
                IQuackBehavior q = new MexicanQuack();
                Assert.AreEqual("Quack, Quack, Ajua", q.Quack());            
            }
        
            [Test]
            public void TestRubberDuck() {
                Duck d = new RubberDuck();
                Assert.AreEqual("I'm a rubber duckie", d.Display());
                Assert.AreEqual("I can't fly", d.PerformFly());
                Assert.AreEqual("Squeak", d.PerformQuack());
            }
        
            [Test]
            public void TestMallardDuck() {
                Duck d = new MallardDuck();
                Assert.AreEqual("I'm a real Mallard duck", d.Display());
                Assert.AreEqual("I'm flying!!", d.PerformFly());
                Assert.AreEqual("Quack", d.PerformQuack());            
            }
        
            [Test]
            public void TestRancheroDuck() {
                Duck d = new RancheroDuck();
                Assert.AreEqual("I'm a Ranchero duck", d.Display());
                Assert.AreEqual("I'm flying!!", d.PerformFly());
                Assert.AreEqual("Quack, Quack, Ajua", d.PerformQuack());
            }
            
            [Test]
            public void TestComposableDuck() {
                IFlyBehavior f = new FlyNoWay();
                IQuackBehavior q = new SimpleQuack();
                Duck d = new ComposableDuck(f, q);
                Assert.AreEqual(
                    "I'm a duck composed of Headfirst.Strategy.FlyNoWay and " 
                    + "Headfirst.Strategy.SimpleQuack", 
                    d.Display());
                Assert.AreEqual("I can't fly", d.PerformFly());
                Assert.AreEqual("Quack", d.PerformQuack());
            }
        }
    }

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