我觉得bridge就形式上来说和adapter差不多咯, 只是确切的把功能和实现分离
功能部分:
public abstract class MoneyFactory
{
public virtual void MakeMoney()
{
}
}
{
public virtual void MakeMoney()
{
}
}
public class ChinaMF : MoneyFactory
{
private MoneyMakeImp mmi;
public ChinaMF(MoneyMakeImp mmi)
{
this.mmi = mmi;
}
public override void MakeMoney()
{
System.Console.Write("The ChinaMF is making money\t");
mmi.MakeMoney();
}
}
{
private MoneyMakeImp mmi;
public ChinaMF(MoneyMakeImp mmi)
{
this.mmi = mmi;
}
public override void MakeMoney()
{
System.Console.Write("The ChinaMF is making money\t");
mmi.MakeMoney();
}
}
实现部分:
public abstract class MoneyMakeImp
{
public virtual void MakeMoney()
{
}
}
{
public virtual void MakeMoney()
{
}
}
public class ChinaMakeImp : MoneyMakeImp
{
public override void MakeMoney()
{
System.Console.Write("RMBs are successfully made!\n");
}
}
{
public override void MakeMoney()
{
System.Console.Write("RMBs are successfully made!\n");
}
}
测试程序:
public static void Main()
{
MoneyMakeImp mm = new ChinaMakeImp();
MoneyFactory mf = new ChinaMF(mm);
mf.MakeMoney();
System.Console.Read();
}
Bridge Demo
{
MoneyMakeImp mm = new ChinaMakeImp();
MoneyFactory mf = new ChinaMF(mm);
mf.MakeMoney();
System.Console.Read();
}