适配器模式,超简单的一个设计模式。
GOF的《设计模式》中解释如下:
将一个类的接口转换成客户希望的另外一个接口,Adapter使原本由于接口不兼容而不能一起工作的类可以一起工作
写个简单的例子如下:
Code Snippers
- public interface IShape
- {
- public void SetLocation(Location location);
- public Location GetLocation();
- }
- class Square : IShape
- {
- public void SetLocation(Location location)
- {
- throw new NotImplementedException();
- }
- public Location GetLocation()
- {
- throw new NotImplementedException();
- }
- }
- class XXCircle
- {
- public void SetLocation(Location location)
- {
- throw new NotImplementedException();
- }
- public Location GetLocation()
- {
- throw new NotImplementedException();
- }
- }
- class Circle : IShape
- {
- XXCircle myXXCircle;
- public Circle(XXCircle myXXCircle)
- {
- this.myXXCircle = myXXCircle;
- }
- public Circle()
- {
- this.myXXCircle = new XXCircle();
- }
- #region IShape Members
- public void SetLocation(Location location)
- {
- myXXCircle.SetLocation(location);
- }
- public Location GetLocation()
- {
- return myXXCircle.GetLocation();
- }
- #endregion
- }