参考http://www.cnblogs.com/kym/archive/2009/04/01/1426812.html
http://blog.csdn.net/abcdwxc/archive/2008/02/03/2079334.aspx
意图:将抽象部分与实现部分分离,使它们都可以独立的变化。
================================================================
常规模式下:我们会创建36个类,每个类负责绘制相应颜色的某图形...如大号红蜡笔类,大号绿蜡笔类,大号蓝蜡笔类等....
桥接模式下:只需要毛笔和颜料接口,然后大,中,小号蜡笔实现毛笔接口,而红,蓝,绿等颜色实现颜料接口...这样一来只需要16个类即可实现....
我的例子:
using System.Drawing;
//颜色 接口
namespace bridge
{
public abstract class colorin
{
//public Color c;
public abstract Color setcolor();
}
}
using System.Drawing;
namespace bridge
{
public class redcolor:colorin
{
public override Color setcolor()
{
return Color.FromArgb(255, 0, 0);
}
}
}
using System.Drawing;
namespace bridge
{
class greencolor:colorin
{
public override Color setcolor()
{
return Color.FromArgb(0, 255, 0);
}
}
}
using System.Drawing;
namespace bridge
{
class bluecolor:colorin
{
public override Color setcolor()
{
return Color.FromArgb(0, 0, 255);
}
}
}
//字体 接口
namespace bridge
{
public abstract class fontin
{
public abstract int getfontsize();
}
}
namespace bridge
{
public class fontsmall:fontin
{
public override int getfontsize()
{
return 10;
}
}
}
namespace bridge
{
public class fontmedium:fontin
{
public override int getfontsize()
{
return 20;
}
}
}
namespace bridge
{
class fontlarge:fontin
{
public override int getfontsize()
{
return 30;
}
}
}
客户端程序:
namespace bridge
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
label1.ForeColor=((colorin)Assembly.Load("bridge").CreateInstance("bridge." + comboBox1.Text)).setcolor();
int fontsize = ((fontin)Assembly.Load("bridge").CreateInstance("bridge." + comboBox2.Text)).getfontsize();
label1.Font = new Font(label1.Font.FontFamily, fontsize);
}
}
}