C#橋接模式完整實例
更新時間:2015年07月16日 12:05:57 作者:宋勇野
這篇文章主要介紹了C#橋接模式,以實例形式較為詳細的分析了C#橋接模式的實現(xiàn)原理與相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了C#橋接模式實現(xiàn)方法。分享給大家供大家參考。具體如下:
C#代碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { public class HandAddressList:HandSetSoft { public override void Run() { Console.WriteLine("運行手機通訊錄"); } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { public class HandBrandM:HandSetBrand { public override void Run() { soft.Run(); } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { public class HandBrandN:HandSetBrand { public override void Run() { soft.Run(); } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { public abstract class HandSetBrand { protected HandSetSoft soft; public void SetHandSetSoft(HandSetSoft soft) { this.soft = soft; } public abstract void Run(); } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { public class HandSetGame:HandSetSoft { public override void Run() { Console.WriteLine("運行手機游戲"); } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { public class HandSetMP3:HandSetSoft { public override void Run() { Console.WriteLine("運行手機MP3"); } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { public abstract class HandSetSoft { public abstract void Run(); } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { HandSetBrand ab; ab = new HandBrandN(); ab.SetHandSetSoft(new HandSetGame()); ab.Run(); ab.SetHandSetSoft(new HandAddressList()); ab.Run(); ab = new HandBrandM(); ab.SetHandSetSoft(new HandSetGame()); ab.Run(); ab.SetHandSetSoft(new HandAddressList()); ab.Run(); ab.SetHandSetSoft(new HandSetMP3()); ab.Run(); Console.ReadKey(); } } }
HandAddressList.cs如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { public class HandAddressList:HandSetSoft { public override void Run() { Console.WriteLine("運行手機通訊錄"); } } }
HandBrandM.cs如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { public class HandBrandM:HandSetBrand { public override void Run() { soft.Run(); } } }
HandBrandN.cs如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { public class HandBrandN:HandSetBrand { public override void Run() { soft.Run(); } } }
HandSetBrand.cs如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { public abstract class HandSetBrand { protected HandSetSoft soft; public void SetHandSetSoft(HandSetSoft soft) { this.soft = soft; } public abstract void Run(); } }
HandSetGame.cs如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { public class HandSetGame:HandSetSoft { public override void Run() { Console.WriteLine("運行手機游戲"); } } }
HandSetMP3.cs如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { public class HandSetMP3:HandSetSoft { public override void Run() { Console.WriteLine("運行手機MP3"); } } }
HandSetSoft.cs如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { public abstract class HandSetSoft { public abstract void Run(); } }
Program.cs如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { HandSetBrand ab; ab = new HandBrandN(); ab.SetHandSetSoft(new HandSetGame()); ab.Run(); ab.SetHandSetSoft(new HandAddressList()); ab.Run(); ab = new HandBrandM(); ab.SetHandSetSoft(new HandSetGame()); ab.Run(); ab.SetHandSetSoft(new HandAddressList()); ab.Run(); ab.SetHandSetSoft(new HandSetMP3()); ab.Run(); Console.ReadKey(); } } }
希望本文所述對大家的C#程序設計有所幫助。
相關(guān)文章
C#實現(xiàn)數(shù)據(jù)去重的方式總結(jié)
這篇文章主要來和大家一起來討論一下關(guān)于C#數(shù)據(jù)去重的常見的幾種方式,每種方法都有其特點和適用場景,感興趣的小伙伴可以了解一下2023-07-07