The Mediator Pattern of Design Model调停者模式/中介者模式

1 Mediator Pattern definition 调停者模式定义
        The Mediator Pattern defines an object that encapsulates how a set of objects interact. This promotes loose coupling,keeping the objects from refering to each other explicitly, and lets u vary their interaction indepently
        调停者模式定义了一个对象,其将多个对象间的交互封装起来形成一个类,使得对象之间的交互不要需要显式的相互参考,使得对象之间松散耦合,从而使得对象之间可以独立的交互。

2 Mediator Benefits
    when there are many interaction among objects, and each operations of one object depends explictly on other objects. Under this curcusmtance, when one object's behavious is changed, it will result in many other changes of the involved objects. To prevent this, we can apply Mediator Pattern to loosely decouple them.
     (1) change n-to-n net-architecture relationship among objects to 1-to-n star-architecture which regards the Mediator as a roll, thus maintains their interaction easy.
    (2) Abstract the behaviours and collabrations among objects
调停者模式的优点:
        当对象间存在很多交互行为,并且这些交互行为相互引用时,对象之间的关系就会变得纷繁复杂。一个对象行为的修改会影响到其他对象的引用。为了解决这种问题,我们可以采用Mediator 模式,来实现对象间的松散耦合。
    (1)将多对多的网状结构关系转化为以调停者为中心的,星型的一对多的关系,使对象间的关系更易于维护
      (2)将对象的行为和协作抽象化

3 Application Demo应用举例
        (1) WTO : 
        WTO change the buseness trade  relationship among contries into  such relationship of countries to WTO and WTO to coutries, which is in chage of mediate among coutries.
        WTO组织:    
        WTO组织将国家之间的商业贸易关系转化为国家与WTO组织之间的关系。WTO起到一个调停者的作用。
        (2) 4 Girls MaJiang Gamble:
       4 girls is Majing grambling. they are confused how much they should give to each other. then they can set up a mediator. The loser just needs to give responding money to Mediator,  The winner just gets responding money from Mdeiator
         4个MM打麻将事件
        4个MM打麻将,4个人谁该给谁多少钱都是糊涂账。这时候可以设立一个调停者,输的人把钱给Mediator,嬴的人丛Mediator拿钱即可。
        (3) House Rent Agency
        Many people wanna lend out their  houses, at the same time, many people wanna rent houses. therefore, we set up House Rent Agency as Mediator to deal with such operations.
        房屋中介结构
        有人想出租房屋,有人想要租房屋。这是房屋中介机构就是一个Mediator 角色,负责房东和租客之间的沟通。

  4. classic  Mediator Pattern included  objects:
        (1) abstract Mediator : define an interface that   is from colleague object to mediator object, in generan ,it's an event method.
        (2) concrete Mediator: implement all abstract methods that are declared by abstract Mediator. It should know all concrete-colleague class. and it can receive message from one concrete-message class, then send message to other concrete-message class.
        (3)abstract colleague:define an interface from Mediator to colleague object.
        (4) concrete colleague
      调停者模式包括以下对象:
        (1)抽象调停者:定义同事对象到调停对象的接口,通常是一个事件方法
        (2)具体调停者:实现抽象调停者申明的方法,从具体同事接受消息,并向另外的同事类对象发送消息。
          (3)抽象同事类:定义从调停者到同事类之间的接口
            (4)具体同事类


5 Mediator Side-effect
    decrease the complex among colleagues while increase the comlex for Mediator.
调停者模式缺点
    虽然降低了同事类间的复杂性但是增加了调停者对象的复杂性。

6 Mediator Pattern Code implementation:
part I: Mediator Pattern Hirarchy Diagram for the following codes:

part II: implemantation
using System;
namespace DesignPattern.Mediator

    
public abstract class AbstractMediator 
    

        
public abstract void ColleagueChanged(AbstractColleague ac); 
    }
 

    
public abstract class AbstractColleague 
    
{
        
private AbstractMediator mediator;

        
public AbstractColleague(AbstractMediator mediator) 
        
{
            
this.mediator=mediator;
        }


        
public AbstractMediator Mediator 
        

            
get{return this.mediator;}
        }


        
public abstract void Action();

        
public void Change() 
        
{
            
this.mediator.ColleagueChanged(this);
        }

    }


    
public class ConcreteMediator : AbstractMediator 
    
{
        
private ColleagueA colleagueA;
        
private ColleagueB colleagueB;

        
public override void ColleagueChanged(AbstractColleague ac) 
        
{
            
this.colleagueA.Action();
            
this.colleagueB.Action();
        }


        
public void CreateConcreteMediator() 
        
{
            
this.colleagueA=new ColleagueA(this);
            
this.colleagueB=new ColleagueB(this);
        }


        
public ColleagueA ColleagueA 
        

            
get{return this.colleagueA;} 
        }


        
public ColleagueB ColleagueB 
        

            
get{return this.colleagueB;}
        }

    }


    
public class ColleagueA : AbstractColleague 
    

        
public ColleagueA(AbstractMediator mediator):base(mediator){}

        
public override void Action() 
        
{
            Console.WriteLine(
"Action From ColleagueA");
        }

    }


    
public class ColleagueB : AbstractColleague 
    

        
public ColleagueB(AbstractMediator mediator):base(mediator){}

        
public override void Action() 
        
{
            Console.WriteLine(
"Action From ColleagueB"); 
       }
 
   }


    
public class Client 
    
{
        
public static void Main() 
        
{
            ConcreteMediator cm
=new ConcreteMediator();
            cm.CreateConcreteMediator();
            AbstractColleague c1
=cm.ColleagueA;
            AbstractColleague c2
=cm.ColleagueB;
         
   cm.ColleagueChanged(c1);
 
        }

    }


}

execution result is the following shown:

欢迎大家补充

原文地址:https://www.cnblogs.com/Winston/p/1183763.html