首页 > 编程 > C# > 正文

C#桥接模式完整实例

2019-10-29 21:40:50
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了C#桥接模式,以实例形式较为详细的分析了C#桥接模式的实现原理与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了C#桥接模式实现方法。分享给大家供大家参考。具体如下:

C#代码如下:

 

 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. namespace ConsoleApplication1 
  6. { 
  7. public class HandAddressList:HandSetSoft 
  8. { 
  9. public override void Run() 
  10. { 
  11. Console.WriteLine("运行手机通讯录"); 
  12. } 
  13. } 
  14. } 
  15. using System; 
  16. using System.Collections.Generic; 
  17. using System.Linq; 
  18. using System.Text; 
  19. namespace ConsoleApplication1 
  20. { 
  21. public class HandBrandM:HandSetBrand 
  22. { 
  23. public override void Run() 
  24. { 
  25. soft.Run(); 
  26. } 
  27. } 
  28. } 
  29. using System; 
  30. using System.Collections.Generic; 
  31. using System.Linq; 
  32. using System.Text; 
  33. namespace ConsoleApplication1 
  34. { 
  35. public class HandBrandN:HandSetBrand 
  36. { 
  37. public override void Run() 
  38. { 
  39. soft.Run(); 
  40. } 
  41. } 
  42. } 
  43. using System; 
  44. using System.Collections.Generic; 
  45. using System.Linq; 
  46. using System.Text; 
  47. namespace ConsoleApplication1 
  48. { 
  49. public abstract class HandSetBrand 
  50. { 
  51. protected HandSetSoft soft; 
  52. public void SetHandSetSoft(HandSetSoft soft)  
  53. { 
  54. this.soft = soft; 
  55. } 
  56. public abstract void Run(); 
  57. } 
  58. } 
  59. using System; 
  60. using System.Collections.Generic; 
  61. using System.Linq; 
  62. using System.Text; 
  63. namespace ConsoleApplication1 
  64. { 
  65. public class HandSetGame:HandSetSoft 
  66. { 
  67. public override void Run() 
  68. { 
  69. Console.WriteLine("运行手机游戏"); 
  70. } 
  71. } 
  72. } 
  73. using System; 
  74. using System.Collections.Generic; 
  75. using System.Linq; 
  76. using System.Text; 
  77. namespace ConsoleApplication1 
  78. { 
  79. public class HandSetMP3:HandSetSoft 
  80. { 
  81. public override void Run() 
  82. { 
  83. Console.WriteLine("运行手机MP3"); 
  84. } 
  85. } 
  86. } 
  87. using System; 
  88. using System.Collections.Generic; 
  89. using System.Linq; 
  90. using System.Text; 
  91. namespace ConsoleApplication1 
  92. { 
  93. public abstract class HandSetSoft 
  94. { 
  95. public abstract void Run(); 
  96. } 
  97. } 
  98. using System; 
  99. using System.Collections.Generic; 
  100. using System.Linq; 
  101. using System.Text; 
  102. namespace ConsoleApplication1 
  103. { 
  104. class Program 
  105. { 
  106. static void Main(string[] args) 
  107. { 
  108. HandSetBrand ab; 
  109. ab = new HandBrandN(); 
  110. ab.SetHandSetSoft(new HandSetGame()); 
  111. ab.Run(); 
  112. ab.SetHandSetSoft(new HandAddressList()); 
  113. ab.Run(); 
  114. ab = new HandBrandM(); 
  115. ab.SetHandSetSoft(new HandSetGame()); 
  116. ab.Run(); 
  117. ab.SetHandSetSoft(new HandAddressList()); 
  118. ab.Run(); 
  119. ab.SetHandSetSoft(new HandSetMP3()); 
  120. ab.Run(); 
  121. Console.ReadKey(); 
  122. } 
  123. } 
  124. } 

HandAddressList.cs如下:

 

 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. namespace ConsoleApplication1 
  6. { 
  7. public class HandAddressList:HandSetSoft 
  8. { 
  9. public override void Run() 
  10. { 
  11. Console.WriteLine("运行手机通讯录"); 
  12. } 
  13. } 
  14. } 

HandBrandM.cs如下:

 

 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. namespace ConsoleApplication1 
  6. { 
  7. public class HandBrandM:HandSetBrand 
  8. { 
  9. public override void Run() 
  10. { 
  11. soft.Run(); 
  12. } 
  13. } 
  14. } 

HandBrandN.cs如下:

 

 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. namespace ConsoleApplication1 
  6. { 
  7. public class HandBrandN:HandSetBrand 
  8. { 
  9. public override void Run() 
  10. { 
  11. soft.Run(); 
  12. } 
  13. } 
  14. } 

HandSetBrand.cs如下:

 

 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. namespace ConsoleApplication1 
  6. { 
  7. public abstract class HandSetBrand 
  8. { 
  9. protected HandSetSoft soft; 
  10. public void SetHandSetSoft(HandSetSoft soft)  
  11. { 
  12. this.soft = soft; 
  13. } 
  14. public abstract void Run(); 
  15. } 
  16. } 

HandSetGame.cs如下:

 

 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. namespace ConsoleApplication1 
  6. { 
  7. public class HandSetGame:HandSetSoft 
  8. { 
  9. public override void Run() 
  10. { 
  11. Console.WriteLine("运行手机游戏"); 
  12. } 
  13. } 
  14. } 

HandSetMP3.cs如下:

 

 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. namespace ConsoleApplication1 
  6. { 
  7. public class HandSetMP3:HandSetSoft 
  8. { 
  9. public override void Run() 
  10. { 
  11. Console.WriteLine("运行手机MP3"); 
  12. } 
  13. } 
  14. } 

HandSetSoft.cs如下:

 

 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. namespace ConsoleApplication1 
  6. { 
  7. public abstract class HandSetSoft 
  8. { 
  9. public abstract void Run(); 
  10. } 
  11. } 

Program.cs如下:

 

 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. namespace ConsoleApplication1 
  6. { 
  7. class Program 
  8. { 
  9. static void Main(string[] args) 
  10. { 
  11. HandSetBrand ab; 
  12. ab = new HandBrandN(); 
  13. ab.SetHandSetSoft(new HandSetGame()); 
  14. ab.Run(); 
  15. ab.SetHandSetSoft(new HandAddressList()); 
  16. ab.Run(); 
  17. ab = new HandBrandM(); 
  18. ab.SetHandSetSoft(new HandSetGame()); 
  19. ab.Run(); 
  20. ab.SetHandSetSoft(new HandAddressList()); 
  21. ab.Run(); 
  22. ab.SetHandSetSoft(new HandSetMP3()); 
  23. ab.Run(); 
  24. Console.ReadKey(); 
  25. } 
  26. } 
  27. } 

希望本文所述对大家的C#程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表