20061016

A Conversation While Contemplating Buying Skis using a Mediator Pattern

using System.Collections.Generic;
using NUnit.Framework;
 
namespace jonas.patterns.behavioral.mediator
{
    [TestFixture]
    public class MediatorTest
    {
        [Test]
        public void ShouldBuySkis()
        {
            Store pacesetterSkiShop = new Store();
            Person suzie = new SalesPerson("Suzie");
            Person jonas = new Customer("Jonas");
 
            pacesetterSkiShop.Add(suzie);
            pacesetterSkiShop.Add(jonas);
 
            suzie.Message = "Finding what you are looking for?";
            suzie.Talk(jonas);
            Assert.AreEqual("Sales Person Suzie says: Finding what you are looking for?", jonas.Recieve(suzie));
 
            jonas.Message = "I'm looking for skis. Can you help me out?";
            jonas.Talk(suzie);
            Assert.AreEqual("Customer Jonas says: I'm looking for skis. Can you help me out?", suzie.Recieve(jonas));
 
            suzie.Message = "Sure what kind are you looking at?";
            suzie.Talk(jonas);
            Assert.AreEqual("Sales Person Suzie says: Sure what kind are you looking at?", jonas.Recieve(suzie));
 
            jonas.Message = "Actually, I just want your number...";
            jonas.Talk(suzie);
            Assert.AreEqual("Customer Jonas says: Actually, I just want your number...", suzie.Recieve(jonas));
 
            suzie.Message = "Get the hell out of this store!";
            suzie.Talk(jonas);
            Assert.AreEqual("Sales Person Suzie says: Get the hell out of this store!", jonas.Recieve(suzie));
        }
    }
 
    internal class Customer : Person
    {
        public Customer(string name) : base(name)
        {
        }
 
        public override string Recieve(Person otherPerson)
        {
            return string.Format("Sales Person {0}", base.Recieve(otherPerson));
        }
    }
 
    internal class SalesPerson : Person
    {
        public SalesPerson(string name) : base(name)
        {
        }
 
        public override string Recieve(Person otherPerson)
        {
            return string.Format("Customer {0}", base.Recieve(otherPerson));
        }
    }
 
    internal class Person
    {
        private string name;
        private string message;
        private Store store;
 
        public Person(string name)
        {
            this.name = name;
        }
 
        public string Name
        {
            get { return name; }
        }
 
        public string Message
        {
            get { return message; }
            set { message = value; }
        }
 
        public Store Store
        {
            set { store = value; }
        }
 
        public void Talk(Person otherPerson)
        {
            store.Talk(otherPerson, this);
        }
 
        public virtual string Recieve(Person otherPerson)
        {
            return string.Format("{0} says: {1}", otherPerson.Name, otherPerson.Message);
        }
    }
 
    internal class Store
    {
        private Dictionary<string, Person> people;
 
        public Store()
        {
            people = new Dictionary<string, Person>();
        }
 
        public void Add(Person person)
        {
            if (!people.ContainsKey(person.Name))
            {
                people.Add(person.Name, person);
                person.Store = this;
            }
        }
 
        public void Talk(Person from, Person to)
        {
            if (people.ContainsKey(to.Name))
                to.Recieve(from);
        }
    }
}

0 comments: