Design Pattern Eight: Intermediary Pattern

       Mediator Pattern:

       Definition: Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently. (Use a mediator object to encapsulate a series of object interactions, mediator Or make the objects do not need to interact explicitly, so that the coupling is loose, and the interaction between them can be changed independently).

      The general class diagram is as follows:

          

      The intermediary model mainly consists of the following parts:

      1. Mediator abstracts the intermediary role and defines a unified interface for communication between colleagues.

      2. Concrete Mediator The role of a specific mediator, which coordinates the roles of colleagues to achieve collaborative behavior, so it must depend on the roles of colleagues

      3. Colleague role, every colleague role knows the intermediary role, and when communicating with other colleague roles, it must cooperate through the intermediary role.

      advantage:

      Reduce the dependencies between classes, and change the original one-to-many dependencies into one-to-one dependencies. At the same time, classes only rely on intermediaries, reducing dependencies and coupling.

      shortcoming:

      In the intermediary model, the intermediary role assumes more responsibilities, so once there is a problem with this intermediary object, the entire system will be greatly affected.

      accomplish:

      

 /// <summary>  
    /// 抽象玩家类  
    /// </summary>  
    public abstract class AbstractPlayer
    {

        // 玩家生命值  
        public double HP { get; set; }

        // 遭受攻击时候,生命值变化
        public abstract void ChangeHP(int num, AbstractMediator Mediator);
    }
    /// <summary>  
    /// 中介者抽象类  
    /// </summary>  
    public abstract class AbstractMediator
    {
        protected AbstractPlayer A;
        protected AbstractPlayer B;

        public AbstractMediator(AbstractPlayer a, AbstractPlayer b)
        {
            A = a;
            B = b;
        }

        // A被攻击  
        public abstract void AEmbattled(int num);
        /// B被攻击  
        public abstract void BEmbattled(int num);
    }

    /// <summary>  
    /// 具体中介者  
    /// </summary>  
    public class MediatorPater : AbstractMediator
    {
        private AbstractPlayer a;
        private AbstractPlayer b;

        public MediatorPater(AbstractPlayer a, AbstractPlayer b) : base(a, b) { }


        public override void AEmbattled(int num)
        {
            A.HP -= num;
        }

        public override void BEmbattled(int num)
        {
            B.HP -= num ;
        }
    }

    /// <summary>  
    /// 玩家A 
    /// </summary>  
    public class PlayerA : AbstractPlayer
    {
        //  遭受攻击时候,A生命值减少 
        public override void ChangeHP(int num, AbstractMediator mediator)
        {
            mediator.AEmbattled(num);
        }
    }

    /// <summary>  
    /// 玩家B   
    /// </summary>  
    public class PlayerB : AbstractPlayer
    {
        // 遭受攻击时候,b生命值减少 
        public override void ChangeHP(int num, AbstractMediator mediator)
        {
            mediator.BEmbattled(num);
        }
    } 
      Range of use:

      1. There is an interdependence relationship between N objects

      2. Multiple objects have dependencies, but the dependent behavior is uncertain or may change. In this case, it is generally recommended to use the intermediary model to reduce the spread of risks caused by changes

      3. Product development, such as MVC framework.

  

     Well, that's all for this chapter. Welcome everyone to join the QQ group: 280993838. Or pay attention to my official account:


Guess you like

Origin blog.csdn.net/zjw1349547081/article/details/54862549