這篇文章主要介紹了C#橋接模式,以實例形式較為詳細的分析了C#橋接模式的實現原理與相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了C#橋接模式實現方法。分享給大家供大家參考。具體如下:
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#程序設計有所幫助。
新聞熱點
疑難解答