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. }
posted @ 2009-12-30 15:08  咋攒都五块  阅读(172)  评论(0编辑  收藏  举报