前言:
中介者用于解决多个对象有多对多的关系时,使用一个中介者类来处理对象之间的复杂的关系,从而让代码不会显得非常乱,如下:
当点击华氏温度升高的时候,摄氏温度升高,并且温度调节按钮也发生变化;同样当调机温度调节的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();
}
}
}