模拟一个场景: 披萨订餐系统
系列:经典风味 精选尊享 物超所值
尺寸:9' 12'
饼皮:手拍 薄脆 烤盘
酱料:意式比萨酱 美式BBQ酱 韩式泡菜酱
1. 上端来指定系列,尺寸,饼皮和酱料
static void Main(string[] args) { try { { Console.WriteLine("************************************************************"); TipcalPizzaBridge pizza = new TipcalPizzaBridge() { size = new SizeSmall(), sause = new KoreanSauce(), bake = new BakeThick() }; pizza.MakePizza(); } { Console.WriteLine("************************************************************"); KingPizzaBridge pizza = new KingPizzaBridge() { size = new SizeBig(), sause = new ItalianSauce(), bake = new BakeManual() }; pizza.MakePizza(); } { Console.WriteLine("************************************************************"); EconomicPizzaBridge pizza = new EconomicPizzaBridge() { size = new SizeBig(), sause = new BBQSauce(), bake = new BakeThin() }; pizza.MakePizza(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadKey(); } }
2. 在系列中定义桥接,类的组合
namespace BridgePattern.Pizza { class TipcalPizzaBridge:IPizza { public ISize size { get; set; } public ISauce sause { get; set; } public IBake bake { get; set; } public void MakePizza() { Console.WriteLine("Customer need a TipcalPizza"); size.PizzaSize(); sause.Sauce(); bake.Bake(); } } }