C#桥接模式完整实例
本文实例讲述了C#桥接模式实现方法。分享给大家供大家参考。具体如下:
C#代码如下:
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespaceConsoleApplication1 { publicclassHandAddressList:HandSetSoft { publicoverridevoidRun() { Console.WriteLine("运行手机通讯录"); } } } usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespaceConsoleApplication1 { publicclassHandBrandM:HandSetBrand { publicoverridevoidRun() { soft.Run(); } } } usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespaceConsoleApplication1 { publicclassHandBrandN:HandSetBrand { publicoverridevoidRun() { soft.Run(); } } } usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespaceConsoleApplication1 { publicabstractclassHandSetBrand { protectedHandSetSoftsoft; publicvoidSetHandSetSoft(HandSetSoftsoft) { this.soft=soft; } publicabstractvoidRun(); } } usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespaceConsoleApplication1 { publicclassHandSetGame:HandSetSoft { publicoverridevoidRun() { Console.WriteLine("运行手机游戏"); } } } usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespaceConsoleApplication1 { publicclassHandSetMP3:HandSetSoft { publicoverridevoidRun() { Console.WriteLine("运行手机MP3"); } } } usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespaceConsoleApplication1 { publicabstractclassHandSetSoft { publicabstractvoidRun(); } } usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespaceConsoleApplication1 { classProgram { staticvoidMain(string[]args) { HandSetBrandab; ab=newHandBrandN(); ab.SetHandSetSoft(newHandSetGame()); ab.Run(); ab.SetHandSetSoft(newHandAddressList()); ab.Run(); ab=newHandBrandM(); ab.SetHandSetSoft(newHandSetGame()); ab.Run(); ab.SetHandSetSoft(newHandAddressList()); ab.Run(); ab.SetHandSetSoft(newHandSetMP3()); ab.Run(); Console.ReadKey(); } } }
HandAddressList.cs如下:
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespaceConsoleApplication1 { publicclassHandAddressList:HandSetSoft { publicoverridevoidRun() { Console.WriteLine("运行手机通讯录"); } } }
HandBrandM.cs如下:
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespaceConsoleApplication1 { publicclassHandBrandM:HandSetBrand { publicoverridevoidRun() { soft.Run(); } } }
HandBrandN.cs如下:
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespaceConsoleApplication1 { publicclassHandBrandN:HandSetBrand { publicoverridevoidRun() { soft.Run(); } } }
HandSetBrand.cs如下:
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespaceConsoleApplication1 { publicabstractclassHandSetBrand { protectedHandSetSoftsoft; publicvoidSetHandSetSoft(HandSetSoftsoft) { this.soft=soft; } publicabstractvoidRun(); } }
HandSetGame.cs如下:
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespaceConsoleApplication1 { publicclassHandSetGame:HandSetSoft { publicoverridevoidRun() { Console.WriteLine("运行手机游戏"); } } }
HandSetMP3.cs如下:
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespaceConsoleApplication1 { publicclassHandSetMP3:HandSetSoft { publicoverridevoidRun() { Console.WriteLine("运行手机MP3"); } } }
HandSetSoft.cs如下:
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespaceConsoleApplication1 { publicabstractclassHandSetSoft { publicabstractvoidRun(); } }
Program.cs如下:
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespaceConsoleApplication1 { classProgram { staticvoidMain(string[]args) { HandSetBrandab; ab=newHandBrandN(); ab.SetHandSetSoft(newHandSetGame()); ab.Run(); ab.SetHandSetSoft(newHandAddressList()); ab.Run(); ab=newHandBrandM(); ab.SetHandSetSoft(newHandSetGame()); ab.Run(); ab.SetHandSetSoft(newHandAddressList()); ab.Run(); ab.SetHandSetSoft(newHandSetMP3()); ab.Run(); Console.ReadKey(); } } }
希望本文所述对大家的C#程序设计有所帮助。