适配器模式:解决重构的问题,新东西和旧系统不吻合,通过组合/继承进行配
适配器:插座,电源适配器,做个转接的
程序员已经确定好规范IHelper,新增了一个RedisHelper----第三方,二者规范不一致,就是没实现接口
/// <summary> /// 数据访问接口 /// </summary> public interface IHelper { void Add<T>(); void Delete<T>(); void Update<T>(); void Query<T>(); } public class MysqlHelper : IHelper { public void Add<T>() { Console.WriteLine("This is {0} Add", this.GetType().Name); } public void Delete<T>() { Console.WriteLine("This is {0} Delete", this.GetType().Name); } public void Update<T>() { Console.WriteLine("This is {0} Update", this.GetType().Name); } public void Query<T>() { Console.WriteLine("This is {0} Query", this.GetType().Name); } } /// <summary> /// 第三方提供的 openstack servicestack /// 不能修改 /// </summary> public class RedisHelper { public RedisHelper() { Console.WriteLine($"构造RedisHelper"); } public void AddRedis<T>() { Console.WriteLine("This is {0} Add", this.GetType().Name); } public void DeleteRedis<T>() { Console.WriteLine("This is {0} Delete", this.GetType().Name); } public void UpdateRedis<T>() { Console.WriteLine("This is {0} Update", this.GetType().Name); } public void QueryRedis<T>() { Console.WriteLine("This is {0} Query", this.GetType().Name); } }
继承:既满足了现有的规范,又没有去修改RedisHelper,适配器,类适配器
public class RedisHelperInherit : RedisHelper, IHelper { public RedisHelperInherit() { Console.WriteLine($"构造{this.GetType().Name}"); } public void Add<T>() { base.AddRedis<T>(); } public void Delete<T>() { base.DeleteRedis<T>(); } public void Query<T>() { base.QueryRedis<T>(); } public void Update<T>() { base.UpdateRedis<T>(); } }
组合:对象适配器
public class RedisHelperObject : IHelper { public RedisHelperObject() { Console.WriteLine($"构造{this.GetType().Name}"); } private RedisHelper _RedisHelper = new RedisHelper();//属性注入 声明写死 一定有 public RedisHelperObject(RedisHelper redisHelper)//构造函数 可以替换(需要抽象) 一定有(不考虑其他构造函数) { this._RedisHelper = redisHelper; } public void SetObject(RedisHelper redisHelper)//方法注入 可以替换(需要抽象) 不一定有 { this._RedisHelper = redisHelper; } public void Add<T>() { this._RedisHelper.AddRedis<T>(); } public void Delete<T>() { this._RedisHelper.DeleteRedis<T>(); } public void Query<T>() { this._RedisHelper.QueryRedis<T>(); } public void Update<T>() { this._RedisHelper.UpdateRedis<T>(); } }
组合优于继承吗?
二者都会先构造一个RedisHelper
1、但是,继承有强侵入,父类的东西子类必须有
2、灵活性,继承只为一个类服务,组合可以面向抽象诶多个类型服务
//继承 既满足现有的规范调用,又没有修改RedisHelper //类适配器模式 Console.WriteLine("*****************************"); { IHelper helper = new RedisHelperInherit(); helper.Add<Program>(); helper.Delete<Program>(); helper.Update<Program>(); helper.Query<Program>(); } //组合 既满足现有的规范调用,又没有修改RedisHelper //对象适配器 Console.WriteLine("*****************************"); { IHelper helper = new RedisHelperObject(); helper.Add<Program>(); helper.Delete<Program>(); helper.Update<Program>(); helper.Query<Program>(); } //组合优于继承? //二者都会先构造一个RedisHelper,但是继承是强侵入,父类的东西子类必须有 //灵活性,继承只为一个类服务;组合可以面向抽象为多个类型服务 { RedisHelperInherit redisHelperInherit = new RedisHelperInherit(); redisHelperInherit.DeleteRedis<Program>(); } { RedisHelperObject redisHelperObject = new RedisHelperObject(); redisHelperObject.Add<Program>(); }