C++ Programmer's Cookbook

{C++ 基础} {C++ 高级} {C#界面,C++核心算法} {设计模式} {C#基础}

模式设计c#--行为型--mediator

名称 Mediator
结构 o_mediator.bmp
意图 用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
适用性
  • 一组对象以定义良好但是复杂的方式进行通信。产生的相互依赖关系结构混乱且难以理解。
  • 一个对象引用其他很多对象并且直接与这些对象通信,导致难以复用该对象。
  • 想定制一个分布在多个类中的行为,而又不想生成太多的子类。


namespace  Mediator_DesignPattern
{
    
using  System;

    
class  Mediator 
    
{
        
private  DataProviderColleague dataProvider;
        
private  DataConsumerColleague dataConsumer;
        
public   void  IntroduceColleagues(DataProviderColleague c1, DataConsumerColleague c2)
        
{
            dataProvider 
=  c1;
            dataConsumer 
=  c2;            
        }

        
        
public   void  DataChanged()
        
{
            
int  i  =  dataProvider.MyData;
            dataConsumer.NewValue(i);
        }

    }


    
class  DataConsumerColleague 
    
{
        
public   void  NewValue( int  i)
        
{
            Console.WriteLine(
" New value {0} " , i);
        }

    }


    
class  DataProviderColleague
    
{
        
private  Mediator mediator;
        
private   int  iMyData = 0 ;
        
public   int  MyData 
        
{
            
get  
            
{
                
return  iMyData;
            }

            
set  
            
{
                iMyData 
=  value;
            }

        }

        
public  DataProviderColleague(Mediator m)
        
{
            mediator 
=  m;
        }


        
public   void  ChangeData()
        
{
            iMyData 
=   403 ;

            
//  Inform mediator that I have changed the data
             if  (mediator  !=   null )
                mediator.DataChanged();    
        }
        
    }


    
///   <summary>
    
///     Summary description for Client.
    
///   </summary>

     public   class  Client
    
{
        
public   static   int  Main( string [] args)
        
{            
            Mediator m 
=   new  Mediator();
            DataProviderColleague c1 
=   new  DataProviderColleague(m);
            DataConsumerColleague c2 
=   new  DataConsumerColleague();
            m.IntroduceColleagues(c1,c2);

            c1.ChangeData();

            
return   0 ;
        }

    }

}

很好的例子:聊天室:

// Mediator pattern -- Real World example  


using System;
using System.Collections;

namespace DoFactory.GangOfFour.Mediator.RealWorld
{
  
  
// MainApp test application 

  
class MainApp
  
{
    
static void Main()
    
{
      
// Create chatroom 
      Chatroom chatroom = new Chatroom();

      
// Create participants and register them 
      Participant George = new Beatle("George");
      Participant Paul 
= new Beatle("Paul");
      Participant Ringo 
= new Beatle("Ringo");
      Participant John 
= new Beatle("John") ;
      Participant Yoko 
= new NonBeatle("Yoko");

      chatroom.Register(George);
      chatroom.Register(Paul);
      chatroom.Register(Ringo);
      chatroom.Register(John);
      chatroom.Register(Yoko);

      
// Chatting participants 
      Yoko.Send ("John""Hi John!");
      Paul.Send (
"Ringo""All you need is love");
      Ringo.Send(
"George""My sweet Lord");
      Paul.Send (
"John""Can't buy me love");
      John.Send (
"Yoko""My sweet love") ;

      
// Wait for user 
      Console.Read();
    }

  }


  
// "Mediator" 

  
abstract class AbstractChatroom
  
{
    
public abstract void Register(Participant participant);
    
public abstract void Send(
      
string from, string to, string message);
  }


  
// "ConcreteMediator" 

  
class Chatroom : AbstractChatroom
  
{
    
private Hashtable participants = new Hashtable();

    
public override void Register(Participant participant)
    
{
      
if (participants[participant.Name] == null)
      
{
        participants[participant.Name] 
= participant;
      }


      participant.Chatroom 
= this;
    }


    
public override void Send(
      
string from, string to, string message)
    
{
      Participant pto 
= (Participant)participants[to];
      
if (pto != null)
      
{
        pto.Receive(from, message);
      }

    }

  }


  
// "AbstractColleague" 

  
class Participant
  
{
    
private Chatroom chatroom;
    
private string name;

    
// Constructor 
    public Participant(string name)
    
{
      
this.name = name;
    }


    
// Properties 
    public string Name
    
{
      
getreturn name; }
    }


    
public Chatroom Chatroom
    
{
      
set{ chatroom = value; }
      
getreturn chatroom; }
    }


    
public void Send(string to, string message)
    
{
      chatroom.Send(name, to, message);
    }


    
public virtual void Receive(
      
string from, string message)
    
{
      Console.WriteLine(
"{0} to {1}: '{2}'",
        from, Name, message);
    }

  }


  
//" ConcreteColleague1" 

  
class Beatle : Participant
  
{
    
// Constructor 
    public Beatle(string name) : base(name) 
    

    }


    
public override void Receive(string from, string message)
    
{
      Console.Write(
"To a Beatle: ");
      
base.Receive(from, message);
    }

  }


  
//" ConcreteColleague2" 

  
class NonBeatle : Participant
  
{
    
// Constructor 
    public NonBeatle(string name) : base(name) 
    

    }


    
public override void Receive(string from, string message)
    
{
      Console.Write(
"To a non-Beatle: ");
      
base.Receive(from, message);
    }

  }

}

 

posted on 2006-01-03 16:10 梦在天涯 阅读(699) 评论(0)  编辑 收藏 引用 所属分类: Design pattern


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

EMail:itech001#126.com

导航

统计

  • 随笔 - 461
  • 文章 - 4
  • 评论 - 746
  • 引用 - 0

常用链接

随笔分类

随笔档案

收藏夹

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

积分与排名

  • 积分 - 1783945
  • 排名 - 5

最新评论

阅读排行榜