Design Pattern - Adapter Pattern

适配器模式,超简单的一个设计模式。

GOF的《设计模式》中解释如下:

将一个类的接口转换成客户希望的另外一个接口,Adapter使原本由于接口不兼容而不能一起工作的类可以一起工作

写个简单的例子如下:

Code Snippers
  1. public interface IShape
  2. {
  3.     public void SetLocation(Location location);
  4.  
  5.     public Location GetLocation();
  6. }
  7.  
  8. class Square : IShape
  9. {
  10.     public void SetLocation(Location location)
  11.     {
  12.         throw new NotImplementedException();
  13.     }
  14.  
  15.     public Location GetLocation()
  16.     {
  17.         throw new NotImplementedException();
  18.     }
  19. }
  20.  
  21. class XXCircle
  22. {
  23.     public void SetLocation(Location location)
  24.     {
  25.         throw new NotImplementedException();
  26.     }
  27.     public Location GetLocation()
  28.     {
  29.         throw new NotImplementedException();
  30.     }
  31. }
  32.  
  33. class Circle : IShape
  34. {
  35.     XXCircle myXXCircle;
  36.  
  37.     public Circle(XXCircle myXXCircle)
  38.     {
  39.         this.myXXCircle = myXXCircle;
  40.     }
  41.  
  42.     public Circle()
  43.     {
  44.         this.myXXCircle = new XXCircle();
  45.     }
  46.     
  47.     #region IShape Members
  48.  
  49.     public void SetLocation(Location location)
  50.     {
  51.         myXXCircle.SetLocation(location);
  52.     }
  53.  
  54.     public Location GetLocation()
  55.     {
  56.         return myXXCircle.GetLocation();
  57.     }
  58.  
  59.     #endregion
  60. }

转载于:https://www.cnblogs.com/5kuai/archive/2009/12/30/1636058.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值