Some time have passed since I wrote blog post on GoF Design Pattern. Not sure what are actual reasons for this. I guess passing MS exams, but maybe I was lazy.
I had been thinking about good example for Mediator Design Pattern and could not find anything better than standard example with UI and cooperation of different components, but this was before I thought about neurons and their cooperation and in few moments I realized amazing thing. Our brain is mediator to different body parts. It just amazingly fits description.
Just try to imagine what would happen if each of our body part knows about each other. This means that if you see something great and you want to touch it, your eye has to know about your hands and manipulate them to do this. What if you would need to defense by hands because you got hurt in chest. In this case your chest would need to know about hands and manipulate them. This can be applicable to any part of your body that collects information from world and then some reaction should be done by one or many other body parts. Cooperation many-to-many doesn’t work for our body. But it often happens that developers write code that works just as was described above. Sometimes it works, but sometimes it just creates crappy spaghetti mess. Our body has one central system that does all the analyzing for us and manages reactions. Very often this is good practice for code we write.
MEDIATOR
Mediator is design pattern that centralizes cooperation between components thus decoupling interaction between them.
Mediator gracefully simplifies understanding of interaction between components. This gives better maintainability for future, but because logic is centralized it can become very complex and huge, as human brain is.
Here below is demonstration of the demo application I wrote:
image
So as you can see brain know how to handle different things and how to react on them and with which body parts. How does this happen?
Brain (Mediator) knows about each body part (Colleague), so it actually has properties or fields, like Ear, Eye, Hand, Leg, etc. Each of the body parts knows its brain and can signal that something has happened.

Colleague base class (knows its brain)

        // Colleague
        class BodyPart
        {
            private readonly Brain _brain;

            public BodyPart(Brain brain)
            {
                _brain = brain;
            }

            public void Changed()
            {
                _brain.SomethingHappenedToBodyPart(this);
            }
        }

Concrete implementation of Colleague can look as following:

    class Ear : BodyPart
    {
        private string _sounds = string.Empty;

        public Ear(Brain brain)
            : base(brain)
        {
        }

        public void HearSomething()
        {
            Console.WriteLine("Enter what you hear:");
            _sounds = Console.ReadLine();

            Changed();
        }

        public string GetSounds()
        {
            return _sounds;
        }
    }

So it basically can HearSomething and can provide its status by GetSounds method. Some other body parts can have some reactions, as simple Face implementation below:

    class Face : BodyPart
    {
        public Face(Brain brain)
            : base(brain)
        {
        }

        public void Smile()
        {
            Console.WriteLine("FACE: Smiling...");
        }
    }

Mediator (aka Brain)

As it should be expected Mediator is bit huge class and it has to handle all the complexity of interaction between body parts. You might want to change something in this class, I do not expect you to see this as ideal brain, but I hope you get an idea how it works.
    // Mediator
    class Brain
    {
        public Brain()
        {
            CreateBodyParts();
        }

        private void CreateBodyParts()
        {
            Ear = new Ear(this);
            Eye = new Eye(this);
            Face = new Face(this);
            Hand = new Hand(this);
            Leg = new Leg(this);
        }

        public Ear Ear { get; private set; }
        public Eye Eye { get; private set; }
        public Face Face { get; private set; }
        public Hand Hand { get; private set; }
        public Leg Leg { get; private set; }

        public void SomethingHappenedToBodyPart(BodyPart bodyPart)
        {
            if (bodyPart is Ear)
            {
                string heardSounds = ((Ear)bodyPart).GetSounds();

                if (heardSounds.Contains("stupid"))
                {
                    // attacking offender
                    Leg.StepForward();
                    Hand.HitPersonNearYou();
                    Leg.Kick();
                }
                else if (heardSounds.Contains("cool"))
                {
                    Face.Smile();
                }
            }
            else if (bodyPart is Eye)
            {
                // brain can analyze what you see and
                // can react appropriately using different body parts
            }
            else if (bodyPart is Hand)
            {
                var hand = (Hand)bodyPart;

                bool hurtingFeeling = hand.DoesItHurt();
                if (hurtingFeeling)
                {
                    Leg.StepBack();
                }

                bool itIsNice = hand.IsItNice();
                if (itIsNice)
                {
                    Leg.StepForward();
                    Hand.Embrace();
                }
            }
            else if (bodyPart is Leg)
            {
                // leg can also feel something if you would like it to
            }
        }
    }

Personally I liked example that I’ve prepared. Hope there is no such example over internet, but who knows.

UML

In addition here below is some UML stuff. You can click on image to see it bigger.

image

My design patterns table

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone