Intermediary model-the Security Council acts as an intermediary

  Mediator mode (Mediator) definition : Use a mediator object to encapsulate a series of object interactions. The intermediary makes each object do not need to explicitly refer to each other, thereby loosening the coupling, and can independently change the interaction between them. The intermediary model is also called the mediation model, which is a typical application of Dimit's law.

  Use scenario :

  • Scenarios where a set of objects communicate in a well-defined but complex way
  • When you want to customize a behavior that is distributed in multiple classes, but do not want to generate too many subclasses

  Advantages :

  • Each of the classes performs their duties, in accordance with Dimit's law.
  • Reduce the coupling between objects, making the objects easy to be reused independently.
  • The one-to-many association between objects is transformed into one-to-one association, which improves the flexibility of the system and makes the system easy to maintain and expand.

  Disadvantages :

  • Due to the centralization, the intermediary bears the complexity of the interaction between the objects. When there are more colleagues, the intermediary becomes more bloated, complicated and difficult to maintain.

  Structure :

  • Abstract mediator (Mediator) role: It is the interface of the mediator and provides abstract methods for colleague object registration and forwarding colleague object information.
  • Concrete mediator role: implement the mediator interface, define a List to manage colleague objects, and coordinate the interaction between the roles of colleagues, so it depends on the colleague role.
  • Abstract colleague class (Colleague) role: define the interface of the colleague class, save the intermediary object, provide the abstract method of colleague object interaction, and realize the common functions of all colleague classes that affect each other.
  • Concrete Colleague role: It is the implementer of the abstract colleague class. When it needs to interact with other colleague objects, the intermediary object is responsible for subsequent interactions.
    Security Council
      Code background : The United States suspects that Iraq has nuclear weapons and threatens to launch a war. The United Nations Security Council mediates and conveys the words of the two countries.

Abstract intermediary: United Nations agency

    abstract class UnitedNations//联合国机构
    {
    
    
        //声明
        public abstract void Declare(string message, Country colleague);
    }

Specific intermediary: Security Council

    class UnitedNationsSecurityCouncil : UnitedNations//安理会了解所有国家,所以拥有美国和伊拉克的对象属性
    {
    
    
        private USA colleague1;
        private Iraq colleague2;
        //美国
        public USA Colleague1
        {
    
       set{
    
    colleague1=value;} }
        //伊拉克
        public Iraq Colleague2
        {
    
    
            set {
    
     colleague2 = value; }
        }

        //声明
        public override void Declare(string message, Country colleague)//重写实现两个对象间的通信
        {
    
    
            if (colleague == colleague1)
            {
    
    
                colleague2.GetMessage(message);
            }
            else
            {
    
    
                colleague1.GetMessage(message);
            }
        }
    }

Abstract colleague class: country

    abstract class Country  //国家
    {
    
    
        protected UnitedNations mediator;
        public Country (UnitedNations mediator)
        {
    
    
            this.mediator = mediator;
        }
    }

Specific colleague categories: United States, Iraq

    class USA:Country//美国
    {
    
    
        public USA(UnitedNations mediator):base(mediator)
        {
    
     }
        //声明
        public void Declare(string message)
        {
    
    
            mediator.Declare(message, this);
        }
        //获得消息
        public void GetMessage(string message)
        {
    
    
            Console.WriteLine("美国获得对方信息:"+message);
        }
    }
    //伊拉克
    class Iraq:Country
    {
    
    
        public Iraq(UnitedNations mediator):base(mediator)
        {
    
     }
        //声明
        public void Declare(string message)
        {
    
    
            mediator.Declare(message,this);
        }
        //获得消息
        public void GetMessage(string message)
        {
    
    
            Console.WriteLine("伊拉克获得对方信息:"+message);
        }
    }

Client:

        static void Main(string[] args)
        {
    
    
            UnitedNationsSecurityCouncil UNSC = new UnitedNationsSecurityCouncil();

            USA c1 = new USA(UNSC);
            Iraq c2 = new Iraq(UNSC);

            UNSC.Colleague1 = c1;//安理会传递话语
            UNSC.Colleague2 = c2;

            c1.Declare("不准研制核武器,否则要发动战争!");
            c2.Declare("我们没有核武器,管好你自己。");

            Console.Read();
        }
    }

Insert picture description here

Guess you like

Origin blog.csdn.net/CharmaineXia/article/details/111190535