• 设计模式之桥接模式


      1,多重继承变化封装

      2,桥接模式:解决多维度的变化

      有一个手机抽象类如下:

     1     /// <summary>
     2     /// 抽象父类
     3     /// </summary>
     4     public abstract class BasePhone
     5     {
     6         /// <summary>
     7         /// 操作系统
     8         /// </summary>
     9         public abstract string System();
    10 
    11         /// <summary>
    12         /// 系统版本
    13         /// </summary>
    14         public abstract string Version();
    15         /// <summary>
    16         /// 打电话
    17         /// </summary>
    18         public abstract void Call();
    19         /// <summary>
    20         /// 发短信
    21         /// </summary>
    22         public abstract void Text();
    23     }

      (1)现在我们想要一个iPhone和Galaxy,则需要代码如下:

     1  public class iPhone : BasePhone
     2     {
     3         public override string System()
     4         {
     5             return "IOS";
     6         }
     7         public override string Version()
     8         {
     9             return "9.3";
    10         }
    11         public override void Call()
    12         {
    13             Console.WriteLine("Use OS{0}.{1}.{2} Call", this.GetType().Name, this.System(), this.Version());
    14         }
    15 
    16         public override void Text()
    17         {
    18             Console.WriteLine("Use OS{0}.{1}.{2} Text", this.GetType().Name, this.System(), this.Version());
    19         }
    20     }
    21 
    22     public class Galaxy:BasePhone
    23     {
    24         public override string System()
    25         {
    26             return "Android";
    27         }
    28 
    29         public override string Version()
    30         {
    31             return "6.0";
    32         }
    33 
    34         public override void Call()
    35         {
    36             Console.WriteLine("Use OS{0}.{1}.{2} Call", this.GetType().Name, this.System(), this.Version());
    37         }
    38 
    39         public override void Text()
    40         {
    41             Console.WriteLine("Use OS{0}.{1}.{2} Text", this.GetType().Name, this.System(), this.Version());
    42         }
    43     }

      调用如下:

     1             {
     2                 BasePhone phone=new iPhone();
     3                 phone.Call();
     4                 phone.Text();
     5             }
     6             {
     7                 BasePhone phone=new Galaxy();
     8                 phone.Call();
     9                 phone.Text();
    10             }

      (2)如果现在我们想要刷机,将iPhone手机装Android系统,将Galaxy装IOS系统,则需要代码如下:

     1   /// <summary>
     2     /// 使用Android系统的iPhone手机
     3     /// </summary>
     4     public class iPhoneAndroid:BasePhone
     5     {
     6         public override string System()
     7         {
     8             return "Android";
     9         }
    10 
    11         public override string Version()
    12         {
    13             return "6.0";
    14         }
    15 
    16         public override void Call()
    17         {
    18             Console.WriteLine("Use OS{0}.{1}.{2} Call", this.GetType().Name, this.System(), this.Version());
    19         }
    20 
    21         public override void Text()
    22         {
    23             Console.WriteLine("Use OS{0}.{1}.{2} Text", this.GetType().Name, this.System(), this.Version());
    24         }
    25     }
    26 
    27     public class GalaxyIOS : BasePhone
    28     {
    29         public override string System()
    30         {
    31             return "IOS";
    32         }
    33         public override string Version()
    34         {
    35             return "9.3";
    36         }
    37         public override void Call()
    38         {
    39             Console.WriteLine("Use OS{0}.{1}.{2} Call", this.GetType().Name, this.System(), this.Version());
    40         }
    41 
    42         public override void Text()
    43         {
    44             Console.WriteLine("Use OS{0}.{1}.{2} Text", this.GetType().Name, this.System(), this.Version());
    45         }
    46     }

      如果还有其它系统的手机,比如WinPhone,则每一款手机将会有iPhone,iPhoneAndroid,iPhoneWinPhone,如果操作系统为M,手机类型为N,则需要多的类的个数为M*N,随着系统和手机类型的增加,将会造成类泛滥,所以现在将变化进行封装,

      (3)桥接模式

      代码如下:

     1     public interface ISystem
     2     {
     3         string System();
     4         string Version();
     5     }
     6 
     7     public class AndroidSystem : ISystem
     8     {
     9         public string System()
    10         {
    11             return "Android";
    12         }
    13         public string Version()
    14         {
    15             return "6.0";
    16         }
    17     }
    18 
    19     public class IOSSystem: ISystem
    20     {
    21         public string System()
    22         {
    23             return "IOS";
    24         }
    25         public string Version( )
    26         {
    27             return "9.3";
    28         }
    29     }
    30 
    31     public class WinphoneSystem : ISystem
    32     {
    33         public string System()
    34         {
    35             return "Winphone";
    36         }
    37         public string Version()
    38         {
    39             return "10.0";
    40         }
    41     }

      BasePhoneBridge:

     1     /// <summary>
     2     /// 抽象父类
     3     /// </summary>
     4     public abstract class BasePhoneBridge
     5     {
     6         /// <summary>
     7         /// 操作系统
     8         /// </summary>
     9         public ISystem SystemVersion { get; set; }
    10         ///// <summary>
    11         ///// 操作系统
    12         ///// </summary>
    13         //public abstract string System();
    14 
    15         ///// <summary>
    16         ///// 系统版本
    17         ///// </summary>
    18         //public abstract string Version();
    19         /// <summary>
    20         /// 打电话
    21         /// </summary>
    22         public abstract void Call();
    23         /// <summary>
    24         /// 发短信
    25         /// </summary>
    26         public abstract void Text();
    27     }
    GalaxyBridge:
     1     public class GalaxyBridge : BasePhoneBridge
     2     {
     3         public override void Call()
     4         {
     5             Console.WriteLine("Use OS{0}.{1}.{2} Call", this.GetType().Name, this.SystemVersion.System(), this.SystemVersion.Version());
     6         }
     7         public override void Text()
     8         {
     9             Console.WriteLine("Use OS{0}.{1}.{2} Text", this.GetType().Name, this.SystemVersion.System(), this.SystemVersion.Version());
    10         }
    11     }

      iPhoneBridge :

     1     public class iPhoneBridge : BasePhoneBridge
     2     {
     3         public override void Call()
     4         {
     5             Console.WriteLine("Use OS{0}.{1}.{2} Call", this.GetType().Name, this.SystemVersion.System(), this.SystemVersion.Version());
     6         }
     7         public override void Text()
     8         {
     9             Console.WriteLine("Use OS{0}.{1}.{2} Text", this.GetType().Name, this.SystemVersion.System(), this.SystemVersion.Version());
    10         }
    11     }

      LumiaBridge:

     1     public class LumiaBridge : BasePhoneBridge
     2     {
     3         public override void Call()
     4         {
     5             Console.WriteLine("Use OS{0}.{1}.{2} Call", this.GetType().Name, this.SystemVersion.System(), this.SystemVersion.Version());
     6         }
     7         public override void Text()
     8         {
     9             Console.WriteLine("Use OS{0}.{1}.{2} Text", this.GetType().Name, this.SystemVersion.System(), this.SystemVersion.Version());
    10         }
    11     }

      调用方式:

     1             Console.WriteLine("**************Bridge***************");
     2             ISystem android = new AndroidSystem();
     3             ISystem ios = new IOSSystem();
     4             ISystem winphone = new WinphoneSystem();
     5             {
     6                 BasePhoneBridge phone = new GalaxyBridge();
     7                 phone.SystemVersion = android;
     8                 phone.Call();
     9                 phone.Text();
    10             }
    11             {
    12                 BasePhoneBridge phone = new GalaxyBridge();
    13                 phone.SystemVersion = ios;
    14                 phone.Call();
    15                 phone.Text();
    16             }
    17             {
    18                 BasePhoneBridge phone = new GalaxyBridge();
    19                 phone.SystemVersion = winphone;
    20                 phone.Call();
    21                 phone.Text();
    22             }    

      效果:

      此时,子类数量为操作系统M+手机类型N 个。

      桥接模式缺点:使用麻烦,对于上端来说需要了解更多细节(需要知道怎么初始化操作系统)

  • 相关阅读:
    html中的块级元素、行内元素
    ptyhon_opencv 图像的基本操作
    正则表达式总结 2017.1.6
    HashMap 中的 entrySet()使用方法 2016.12.28
    (转)Redis持久化的几种方式
    负数与二进制换转方法
    (转)2019JAVA面试题附答案(长期更新)
    Java后端技术面试汇总(第一套)
    (转)Dubbo服务暴露过程源码分析
    Dubbo消费方服务调用过程源码分析
  • 原文地址:https://www.cnblogs.com/shangec/p/10284897.html
Copyright © 2020-2023  润新知