谈谈桥接模式
先打个比方,设计模式犹如古代武术家的内功,内功的修为决定着写出的程序是否容易扩展,是否便于维护,体现了高内聚,松耦合的设计方法。桥接模式将对象的抽象与对象的实现分离。现在分享一下我的小作。
代码
//=================design by mahong ==================
//================design date 2009-11-28===============
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BridgePattern
{
//抽象类大杯、中杯、小杯,即对象的抽象
public abstract class Drink
{
//定义对象的实现接口
SetMilkOrNot setOrNot;
public void SetTheType()
{
setOrNot = SetMilkOrNotSingleton.GetTheMilkIsSetOrNot();
}
public SetMilkOrNot GetTheType()
{
return setOrNot;
}
//定义方法,此方法调用4次,表明是大杯,2次表明是中杯
public abstract void PourDrink();
}
//抽象类是否加奶,即对象的实现
public abstract class SetMilkOrNot
{
public abstract void SetOrNot();
}
//中杯
public class MediumCup : Drink
{
public MediumCup()
{
SetTheType();
}
public override void PourDrink()
{
SetMilkOrNot son = GetTheType();
for (int i = 0; i < 2; i++)
{
son.SetOrNot();
}
}
}
//大杯
public class BigCup : Drink
{
public BigCup()
{
SetTheType();
}
public override void PourDrink()
{
SetMilkOrNot son = GetTheType();
for (int i = 0; i < 4; i++)
{
son.SetOrNot();
}
}
}
//加奶
public class AddMilk : SetMilkOrNot
{
public AddMilk() { }
public override void SetOrNot()
{
Console.WriteLine("加奶");
}
}
//不加奶
public class UnAddMilk : SetMilkOrNot
{
public UnAddMilk() { }
public override void SetOrNot()
{
Console.WriteLine("不加奶");
}
}
//单例,静态创建加不加奶的对象
public class SetMilkOrNotSingleton
{
public static SetMilkOrNot isSetOrNot;
public SetMilkOrNotSingleton(SetMilkOrNot son)
{
isSetOrNot = son;
}
public static SetMilkOrNot GetTheMilkIsSetOrNot()
{
return isSetOrNot;
}
}
class Program
{
static void Main(string[] args)
{
//加奶
SetMilkOrNotSingleton add = new SetMilkOrNotSingleton(new AddMilk());
//大杯加奶
BigCup big = new BigCup();
big.PourDrink();
Console.WriteLine("=====================");
//中杯加奶
MediumCup mediun = new MediumCup();
mediun.PourDrink();
Console.Read();
//不加奶
SetMilkOrNotSingleton unAdd = new SetMilkOrNotSingleton(new UnAddMilk());
//大杯加奶
BigCup big1 = new BigCup();
big1.PourDrink();
Console.WriteLine("=====================");
//中杯加奶
MediumCup mediun1 = new MediumCup();
mediun1.PourDrink();
Console.ReadLine();
Console.Read();
}
}
}
//================design date 2009-11-28===============
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BridgePattern
{
//抽象类大杯、中杯、小杯,即对象的抽象
public abstract class Drink
{
//定义对象的实现接口
SetMilkOrNot setOrNot;
public void SetTheType()
{
setOrNot = SetMilkOrNotSingleton.GetTheMilkIsSetOrNot();
}
public SetMilkOrNot GetTheType()
{
return setOrNot;
}
//定义方法,此方法调用4次,表明是大杯,2次表明是中杯
public abstract void PourDrink();
}
//抽象类是否加奶,即对象的实现
public abstract class SetMilkOrNot
{
public abstract void SetOrNot();
}
//中杯
public class MediumCup : Drink
{
public MediumCup()
{
SetTheType();
}
public override void PourDrink()
{
SetMilkOrNot son = GetTheType();
for (int i = 0; i < 2; i++)
{
son.SetOrNot();
}
}
}
//大杯
public class BigCup : Drink
{
public BigCup()
{
SetTheType();
}
public override void PourDrink()
{
SetMilkOrNot son = GetTheType();
for (int i = 0; i < 4; i++)
{
son.SetOrNot();
}
}
}
//加奶
public class AddMilk : SetMilkOrNot
{
public AddMilk() { }
public override void SetOrNot()
{
Console.WriteLine("加奶");
}
}
//不加奶
public class UnAddMilk : SetMilkOrNot
{
public UnAddMilk() { }
public override void SetOrNot()
{
Console.WriteLine("不加奶");
}
}
//单例,静态创建加不加奶的对象
public class SetMilkOrNotSingleton
{
public static SetMilkOrNot isSetOrNot;
public SetMilkOrNotSingleton(SetMilkOrNot son)
{
isSetOrNot = son;
}
public static SetMilkOrNot GetTheMilkIsSetOrNot()
{
return isSetOrNot;
}
}
class Program
{
static void Main(string[] args)
{
//加奶
SetMilkOrNotSingleton add = new SetMilkOrNotSingleton(new AddMilk());
//大杯加奶
BigCup big = new BigCup();
big.PourDrink();
Console.WriteLine("=====================");
//中杯加奶
MediumCup mediun = new MediumCup();
mediun.PourDrink();
Console.Read();
//不加奶
SetMilkOrNotSingleton unAdd = new SetMilkOrNotSingleton(new UnAddMilk());
//大杯加奶
BigCup big1 = new BigCup();
big1.PourDrink();
Console.WriteLine("=====================");
//中杯加奶
MediumCup mediun1 = new MediumCup();
mediun1.PourDrink();
Console.ReadLine();
Console.Read();
}
}
}
以上代码量并不多,但反应了桥接模式的大概设计思想理念。