c#设计模式——中介者模式

前言:
中介者用于解决多个对象有多对多的关系时,使用一个中介者类来处理对象之间的复杂的关系,从而让代码不会显得非常乱,如下:
在这里插入图片描述
当点击华氏温度升高的时候,摄氏温度升高,并且温度调节按钮也发生变化;同样当调机温度调节的trackbar时,华氏温度的textbox和摄氏温度的textbox的值都要发生变化。
1、抽象中介者类

 abstract  class Mediator
    {

      
        public abstract void ComponentChange(Component c);

      
    }

2、具体中介者类

class ConcreteMediator:Mediator 
    {

        public FahrenheitAddButton fab;
        public FahrenheitDecButton fdb;
        public FahrenheitDisplayTextBox fdtb;
        public CelsiusAddButton cab;
        public CelsiusDecButton cdb;
        public CelsiusDisplayTextBox cdtb;
        public CelsiusTrackBar ctb;



        public override void ComponentChange(Component c)
        {
            if(c==fab )
            {
                double initialValue;
                double faValue = double.TryParse(fdtb.c.Text, out initialValue) ? initialValue + 1 : 0;
                fdtb.Update(faValue);
                cdtb.Update((faValue - 32) / 1.8);
                ctb.Update((faValue - 32) / 1.8);
            }
            else if(c==fdb )
            {
                double initialValue;
                double faValue = double.TryParse(fdtb.c.Text, out initialValue) ? initialValue -1 : 0;
                fdtb.Update(faValue);
                cdtb.Update((faValue - 32) / 1.8);
                ctb.Update((faValue - 32) / 1.8);
            }

            else if(c==cab )
            {
                double initialValue;
                double ceValue = double.TryParse(cdtb.c.Text, out initialValue) ? initialValue + 1 : 0;
                fdtb.Update(ceValue * 1.8 + 32);
                cdtb.Update(ceValue);
                ctb.Update(ceValue);
            }
            else if(c==cdb )
            {
                double initialValue;
                double ceValue = double.TryParse(cdtb.c.Text, out initialValue) ? initialValue - 1 : 0;
                fdtb.Update(ceValue * 1.8 + 32);
                cdtb.Update(ceValue);
                ctb.Update(ceValue);
            }
            else if(c==ctb )
            {
                double ceValue = ((TrackBar)ctb.c).Value ;
                fdtb.Update(ceValue * 1.8 + 32);
                cdtb.Update(ceValue);
                ctb.Update(ceValue);
            }

            else if(c==fdtb )
            {
                double initialValue;
                double faValue = double.TryParse(fdtb.c.Text, out initialValue) ? initialValue : 0;
                fdtb.Update(faValue);
                cdtb.Update((faValue - 32) / 1.8);
                ctb.Update((faValue - 32) / 1.8);
            }
            else if(c==cdtb )
            {
                double initialValue;
                double ceValue = double.TryParse(cdtb.c.Text, out initialValue) ? initialValue : 0;
                fdtb.Update(ceValue * 1.8 + 32);
                cdtb.Update(ceValue);
                ctb.Update(ceValue);
            }
        }

    }

3、抽象同事类

 abstract class Component
    {
        Mediator mediator;
        public Control c;
        public Component(Mediator mediator, Control c)
        {
            this.mediator = mediator;
            this.c = c;
        }
        public abstract void Update(double value);
        public void Change()//转发调用
        {
            mediator.ComponentChange(this);
        }
    }

4、

class FahrenheitAddButton:Component
    {

        public FahrenheitAddButton (Mediator mediator,Control c):base(mediator ,c)
        {

        }
        public override void Update(double value)
        {
            
        }
    }
 class FahrenheitDecButton:Component
    {

        public FahrenheitDecButton(Mediator mediator,Control c):base(mediator,c )
        {

        }
        public override void Update(double value)
        { 
            
        }

    }
  class FahrenheitDisplayTextBox:Component
    {

        public FahrenheitDisplayTextBox(Mediator mediator,Control c):base(mediator,c)
        {

        }


        public override void Update(double value )
        {
          
            TextBox  textBox =(TextBox)c;
            textBox.Text = value.ToString("f2");
        }
        
    }
 class CelsiusAddButton:Component 
    {
       
        public CelsiusAddButton (Mediator mediator,Control c):base (mediator ,c)
        {

        }

        public override void Update(double value)
        {
           
        }

    }
 class CelsiusDecButton:Component 
    {
        public CelsiusDecButton (Mediator mediator ,Control c):base(mediator ,c)
        {

        }
        public override void Update(double value)
        {
            
        }
    }
class CelsiusDisplayTextBox:Component 
    {
        public CelsiusDisplayTextBox (Mediator mediator,Control c):base(mediator ,c)
        {

        }

        public override void Update(double  value)
        {
           
          
            TextBox textBox = (TextBox)c;
            textBox.Text =value .ToString("f2");
        }
     


    }
class CelsiusTrackBar : Component
    {

        public CelsiusTrackBar(Mediator mediator, Control c) : base(mediator, c)
        {
        }
        public override void Update(double value)
        {
            TrackBar trackBar = (TrackBar)c;
            trackBar.Value =Convert .ToInt32 (value) ;
        }
    }

调用:

 public partial class Form1 : Form
    {


        ConcreteMediator cm = new ConcreteMediator();
        FahrenheitAddButton fAB;
        FahrenheitDecButton fDB;
        FahrenheitDisplayTextBox fDTB;

        CelsiusAddButton cAB;
        CelsiusDecButton cDB;
        CelsiusDisplayTextBox cDTB;
        CelsiusTrackBar cTB;
        public Form1()
        {
            InitializeComponent();
            fAB = new FahrenheitAddButton(cm, btnAddFa);
            cm.fab = fAB;
            fDB = new FahrenheitDecButton(cm, btnDecFa);
            cm.fdb = fDB;
            fDTB = new FahrenheitDisplayTextBox(cm, tbxFa);
            cm.fdtb = fDTB;

            cAB = new CelsiusAddButton(cm, btnAddCe);
            cm.cab = cAB;
            cDB = new CelsiusDecButton(cm, btnDecCe);
            cm.cdb = cDB;
            cDTB = new CelsiusDisplayTextBox(cm, tbxCe);
            cm.cdtb = cDTB;

            cTB = new CelsiusTrackBar(cm, tbCe);
            cm.ctb = cTB;





        }

        private void button1_Click(object sender, EventArgs e)
        {

            fAB.Change();

        }



        private void btnDecFa_Click(object sender, EventArgs e)
        {
            fDB.Change();
        }

        private void btnAddCe_Click(object sender, EventArgs e)
        {
            cAB.Change();
        }

        private void btnDecCe_Click(object sender, EventArgs e)
        {
            cDB.Change();
        }

        private void tbCe_Scroll(object sender, EventArgs e)
        {
            cTB.Change();
        }





        private void tbxFa_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                fDTB.Change();
            }

        }

        private void tbxCe_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                cDTB.Change();
            }
        }
    }
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

c#上位机

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值