Share via


Mediator Pattern

Mediator pattern ensures that the components are loosely coupled, such that they don't call each others explicitly, rather they always use a separate Mediator implementation to do those jobs. 

http://2.bp.blogspot.com/_DSP5FXX4Isw/S_RdaLEI4vI/AAAAAAAAC98/YKKIxzyrbic/s320/mediator.JPG

public interface  IComponent
    {
        void SetState(object state);
    }
    public class  Component1 : IComponent
    {
        #region IComponent Members
 
        public void  SetState(object  state)
        {
            //Do Nothing
            throw new  NotImplementedException();
        }
 
        #endregion
    }
 
    public class  Component2 : IComponent
    {
 
        #region IComponent Members
 
        public void  SetState(object  state)
        {
            //Do nothing
            throw new  NotImplementedException();
        }
 
        #endregion
    }
 
    public class  Mediator // Mediages the common tasks
    {
        public IComponent Component1 { get; set; }
        public IComponent Component2 { get; set; }
 
        public void  ChageState(object  state)
        {
            this.Component1.SetState(state);
            this.Component2.SetState(state);
        }
    }

Here you can see the mediator Registers all the Components within it and then calls its method when required.