风动破

导航

设计模式之Mediator中介者

参考:http://www.cnblogs.com/BeyondAnyTime/archive/2012/08/30/2663922.html 

 Mediator中介者模式定义: 用一个中介对象来封装一系列关于对象交互行为. 为何使用Mediator? 各个对象之间的交互操作非常多;每个对象的行为操作都依赖彼此对方,修改一个对象的行为,同时会涉及到修改很多其他对象的行为,如果使用Mediator模式,可以使各个对象间的耦合松散,只需关心和 Mediator的关系,使多对多的关系变成了一对多的关系,可以降低系统的复杂性,提高可修改扩展性.

每个成员都必须知道Mediator,并且和 Mediator联系,而不是和其他成员联系. 至此,Mediator模式框架完成,可以发现Mediator模式规定不是很多,大体框架也比较简单,但实际使用起来就非常灵活. Mediator模式在事件驱动类应用中比较多,例如界面设计GUI.;聊天,消息传递等,在聊天应用中,需要有一个MessageMediator,专门负责request/reponse之间任务的调节. 

public class Colleague

    {
        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private string content;
        public string Content
        {
            get { return content; }
            set { content = value; }
        }
        public Colleague(string name) 
        {
            this.name = name;
        }
        public Colleague(string name,string content)
        {
            this.name = name;
            this.content = content;
        }
        public delegate void UIHandle(string content);
        public UIHandle uiHandle;
        public virtual void Talk() { }
    }
    public class Student :Colleague
    {
        public Student(string name):base(name) {    }
        public override void Talk()
        {
            uiHandle(Content);
        }
    }
    public class Teacher : Colleague
    {
        public Teacher(string name): base(name) {    }
        public override void Talk()
        {
            uiHandle(Content);
        } 
    }
    /// <summary>
    /// 中介群
    /// </summary>
    public class Mediator
    {
        public Queue<Colleague> queueList;
        public Mediator() {
            queueList = new Queue<Colleague>();
        }
        public virtual void AddColleague(Colleague coll) {
            queueList.Enqueue(coll);
        }
        /// <summary>
        /// 广播
        /// </summary>
        public virtual void Notify(Colleague coll){}
        /// <summary>
        /// 私聊
        /// </summary>
        public  virtual void Chart(Colleague c1 ,Colleague c2) {}
    }
    /// <summary>
    /// qq群平台
    /// </summary>
    public class QQMediator : Mediator {
        public override void Notify(Colleague coll) 
        {
            //coll.Talk();
            foreach(Colleague col in queueList){
                col.Content = coll.Content;
                col.Talk();
            }
        }
        public override void Chart(Colleague c1, Colleague c2) 
        {
            c2.Content = c1.Content;
            c1.Talk();
            c2.Talk();
        }
    }

 

===========UI部分===================== 

public partial class MainWindow:Window {
        private QQMediator qq;
        private Student stu1, stu2;
        private Teacher tea1;
        private UINotify notify;
        public MainWindow()
        {
            InitializeComponent();
            qq = new QQMediator();
            stu1 = new Student("同学A");
            stu2 = new Student("同学B");
            tea1 = new Teacher("老师A");
            qq.AddColleague(stu1);
            qq.AddColleague(stu2);
            qq.AddColleague(tea1);
            notify = new UINotify();
            notify.Add(stu1, SetTextBlock1);
            notify.Add(stu2, SetTextBlock2);
            notify.Add(tea1, SetTextBlock3);
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            stu1.Content = t1.Text;
            if (radio1.IsChecked == true)
                qq.Chart(stu1, stu2);
            else
                qq.Notify(stu1);
        }
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            stu2.Content = t2.Text;
            if (radio1.IsChecked == true)
                qq.Chart(stu2, stu1);
            else
            qq.Notify(stu2);
        }
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            tea1.Content = t3.Text;
            qq.Notify(tea1);
        }
        private void radio1_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton temp = (RadioButton)sender;
            if (temp.IsChecked == null) return;            
        }
        public void SetTextBlock1(string str)
        {
            block1.Text += str;
        }
        public void SetTextBlock2(string str)
        {
            block2.Text += str;
        }
        public void SetTextBlock3(string str)
        {
            block3.Text += str;
        }
    }

 ===============================

注册委托

public class UINotify

 

    {
        public void Add(Colleague coll, Colleague.UIHandle control) 
        {
            coll.uiHandle = control;
        }
    }

posted on 2015-04-28 19:37  风动破  阅读(120)  评论(0)    收藏  举报