You are here:   ArielOrtiz.com > Software Design and Architecture > Lab 3: Abstract Factory and Singleton Patterns

Lab 3: Abstract Factory and Singleton 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 4 and 5 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: abstractfactory_singleton.zip. Any new files you create must go inside the corresponding folder.

  1. Carefully study the following NUnit text fixture:

    namespace Headfirst.Factory.Pizzaaf {
        
        using NUnit.Framework;
        using System;
        
        [TestFixture]
        public class TestAbstractFactory {
        
            static readonly string EOL = Environment.NewLine;     
            PizzaStore store;
        
            [SetUp]
            public void Init() {
                store = new TecPizzeria();
            }
        
            [Test]
            public void TestVeggie() {
                Pizza p = store.OrderPizza("veggie");
                Assert.AreEqual("---- Tec Vegetarian Pizza ----" + EOL
                    + "Yummy stuffed crust style dough" + EOL
                    + "Tomato sauce with plum tomatoes" + EOL 
                    + "Shredded Parmesan" + EOL 
                    + "Jalapeno, Black Olives, Onion, Red Pepper, Mushrooms" + EOL, 
                    p.ToString());
            }
        
            [Test]
            public void TestPepperoni() {
                Pizza p = store.OrderPizza("pepperoni");
                Assert.AreEqual("---- Tec Pepperoni Pizza ----" + EOL
                    + "Yummy stuffed crust style dough" + EOL
                    + "Tomato sauce with plum tomatoes" + EOL 
                    + "Shredded Parmesan" + EOL 
                    + "Jalapeno, Black Olives, Onion, Red Pepper, Mushrooms" + EOL
                    + "Sliced Pepperoni" + EOL, 
                    p.ToString());
            }
        
            [Test]
            public void TestCheese() {
                Pizza p = store.OrderPizza("cheese");
                Assert.AreEqual("---- Tec Cheese Pizza ----" + EOL
                    + "Yummy stuffed crust style dough" + EOL
                    + "Tomato sauce with plum tomatoes" + EOL 
                    + "Shredded Parmesan" + EOL, 
                    p.ToString());
            }
        
            [Test]
            public void TestClam() {
                Pizza p = store.OrderPizza("clam");
                Assert.AreEqual("---- Tec Clam Pizza ----" + EOL
                    + "Yummy stuffed crust style dough" + EOL
                    + "Tomato sauce with plum tomatoes" + EOL 
                    + "Shredded Parmesan" + EOL 
                    + "Fresh Clams from Long Island Sound" + EOL, 
                    p.ToString());
            }
        }
    }

    Write the following new classes:

    • Headfirst.Factory.Pizzaaf.TecPizzaIngredientFactory
    • Headfirst.Factory.Pizzaaf.TecPizzeria
    • Any other you consider appropriate

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

  2. Read sections 5.3.3 and 5.3.4 of [BISHOP]. Using the provided design and implementation guidelines, convert the following class into a singleton. The modified class must be thread-safe and use lazy instantiation.

    namespace Headfirst.Singleton {
    
        public class Tigger {
                
            public override string ToString() {
                return "I'm the only one!";
            }
        
            public string Roar() {
                return "Grrr!";
            }
        }
    }

    Check your solution using the following test case:

    namespace Headfirst.Singleton {
        
        using NUnit.Framework;
        
        [TestFixture]
        public class TestSingleton {
         
            [Test]
            public void TestTigger() {
                Tigger t = Tigger.UniqueInstance;
                Assert.AreSame(Tigger.UniqueInstance, t);
                Assert.AreEqual("I'm the only one!", t.ToString());
                Assert.AreEqual("Grrr!", t.Roar());
            }
        }
    }

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