适配器模式
/// 结构型设计模式:关注类与类之间的关系
///面向对象 -类交互才能产生功能
///纵向关系:继承/实现 关系肯定非常紧密 (关联关系是最强的)
///横向关系:依赖--关联--组合--聚合 关系是依次变强
/// 依赖: 是方法内部出现的新类型,另外三种是语义上的划分
/// 总结: 组合优于继承
首先编写了一个数据访问的接口
namespace 结构型设计模式
{
/// <summary>
/// 数据访问接口
/// </summary>
public interface Ihelper
{
void Add<T>();
void Delete<T>();
void Updata<T>();
void Query<T>();
}
}
编写另外一个接口,同时继承与Ihelper
namespace 结构型设计模式
{
internal class mysqllhelper : Ihelper
{
public void Add<T>()
{
Console.WriteLine("ADD");
}
public void Delete<T>()
{
Console.WriteLine("Delete");
}
public void Query<T>()
{
Console.WriteLine("Query");
}
public void Updata<T>()
{
Console.WriteLine("Updata");
}
}
}
namespace 结构型设计模式
{
internal class oraclelhelper : Ihelper
{
public void Add<T>()
{
throw new NotImplementedException();
}
public void Delete<T>()
{
throw new NotImplementedException();
}
public void Query<T>()
{
throw new NotImplementedException();
}
public void Updata<T>()
{
throw new NotImplementedException();
}
}
}
此时有一类与Ihelper提供的接口不一致。
namespace 结构型设计模式
{
//这一个类不继承Ihelper
public class sqllhelper
{
public void Addsql<T>()
{
Console.WriteLine("this is {0} add",this.GetType().Name);
}
public void Deletesql<T>()
{
Console.WriteLine("this is {0} add", this.GetType().Name);
}
public void Querysql<T>()
{
Console.WriteLine("this is {0} add", this.GetType().Name);
}
public void Updatasql<T>()
{
Console.WriteLine("this is {0} add", this.GetType().Name);
}
}
}
执行所有类的使用
因为sqllhelper没有提供Add、Delete、Query等方法。所以在此处直接使用的话必然异常
那么有想要所有的类都使用此种方式来达成统一调用。就需要使用适配器-组合的方式来进行
private void Form1_Load(object sender, EventArgs e)
{
Console.WriteLine();
{
Ihelper mysqllhelper = new mysqllhelper();
mysqllhelper.Add<int>();
mysqllhelper.Delete<int>();
mysqllhelper.Query<int>();
mysqllhelper.Updata<int>();
}
Console.WriteLine();
{
Ihelper oraclelhelper = new oraclelhelper();
oraclelhelper.Add<int>();
oraclelhelper.Delete<int>();
oraclelhelper.Query<int>();
oraclelhelper.Updata<int>();
}
Console.WriteLine();
{
//因为sqllhelper没有提供Add、Delete、Query等方法。所以在此处直接使用的话必然异常
//那么有想要所有的类都使用此种方式来达成统一调用。就需要使用适配器-组合的方式来进行
Ihelper sqllhelper = new sqllhelper();
sqllhelper.Add<int>();
sqllhelper.Delete<int>();
sqllhelper.Query<int>();
sqllhelper.Updata<int>();
}
}
这个时候使用适配器-继承的方式。
namespace 结构型设计模式
{
internal class sqllhelper继承 : sqllhelper, Ihelper
{
//此处 虽然sqllhelper 是别人封装好的 我们不能修改 但是通过这样的继承方式
//我们就可以使用Ihelper将sqllhelper包装起来
//在主程序中就可以实现统一调用
//此处继承使用的适配器 为类适配器 因为继承的sqllhelper 所以为这一类服务
public void Add<T>()
{
base.Addsql<T>();
}
public void Delete<T>()
{
base.Deletesql<T>();
}
public void Query<T>()
{
base.Querysql<T>();
}
public void Updata<T>()
{
base.Updatasql<T>();
}
}
}
这个时候使用适配器-组合的方式。 这样在主程序里就可以实现调用
///纵向关系:继承/实现 关系肯定非常紧密 (关联关系是最强的)
///横向关系:依赖--关联--组合--聚合 关系是依次变强
///依赖: 是方法内部出现的新类型,另外三种是语义上的划分
/// 总结: 组合优于继承
namespace 结构型设计模式
{
internal class sqllhelper组合 : Ihelper
{
//此处继承使用的适配器 为对象适配器 因为是为_sqlhelper这个对象服务的
/// <summary>
/// 1 -字段属性方式组合
/// </summary>
private sqllhelper _sqlhelper=new sqllhelper();
private sqllhelper _sqlhelperctor = null;
/// <summary>
/// 2 -构造函数方式组合,实例化一定会传入,但是对象可以选择
/// </summary>
/// <param name="sqlhelper"></param>
public sqllhelper组合 (sqllhelper sqlhelper)
{
this._sqlhelperctor = sqlhelper;
}
public sqllhelper组合()
{
}
/// <summary>
/// 3- 方法方式组合,对象可以选择,而且可有可无
/// </summary>
/// <param name="sqllhelper"></param>
public void Addsqlhelper(sqllhelper sqllhelper)
{
this._sqlhelperctor=sqllhelper;
}
public void Add<T>()
{
this._sqlhelper.Addsql<T>();
}
public void Delete<T>()
{
this._sqlhelper.Deletesql<T>();
}
public void Query<T>()
{
this._sqlhelper.Querysql<T>();
}
public void Updata<T>()
{
this._sqlhelper.Updatasql<T>();
}
}
}
适配器实现
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// 结构型设计模式:关注类与类之间的关系
///面向对象 -类交互才能产生功能
///纵向关系:继承/实现 关系肯定非常紧密 (关联关系是最强的)
///横向关系:依赖--关联--组合--聚合 关系是依次变强
/// 依赖: 是方法内部出现的新类型,另外三种是语义上的划分
/// 总结: 组合优于继承
private void Form1_Load(object sender, EventArgs e)
{
Console.WriteLine();
{
Ihelper mysqllhelper = new mysqllhelper();
mysqllhelper.Add<int>();
mysqllhelper.Delete<int>();
mysqllhelper.Query<int>();
mysqllhelper.Updata<int>();
}
Console.WriteLine();
{
Ihelper oraclelhelper = new oraclelhelper();
oraclelhelper.Add<int>();
oraclelhelper.Delete<int>();
oraclelhelper.Query<int>();
oraclelhelper.Updata<int>();
}
Console.WriteLine();
{
//因为sqllhelper没有提供Add、Delete、Query等方法。所以在此处直接使用的话必然异常
//那么有想要所有的类都使用此种方式来达成统一调用。就需要使用适配器-组合的方式来进行
Ihelper sqllhelper = new sqllhelper();
sqllhelper.Add<int>();
sqllhelper.Delete<int>();
sqllhelper.Query<int>();
sqllhelper.Updata<int>();
}
Console.WriteLine();
{
//使用适配器-继承的方式
Ihelper sqllhelper = new sqllhelper继承();
sqllhelper.Add<int>();
sqllhelper.Delete<int>();
sqllhelper.Query<int>();
sqllhelper.Updata<int>();
}
Console.WriteLine();
{
//使用适配器-组合的方式--字段
Ihelper sqllhelper = new sqllhelper组合();
sqllhelper.Add<int>();
sqllhelper.Delete<int>();
sqllhelper.Query<int>();
sqllhelper.Updata<int>();
//使用适配器-组合的方式--构造函数方法
sqllhelper A = new sqllhelper();
Ihelper sqllhelper1 = new sqllhelper组合(A);
sqllhelper1.Add<int>();
//使用适配器-组合的方式--方法
sqllhelper B = new sqllhelper();
sqllhelper组合 sqllhelper2 = new sqllhelper组合();
sqllhelper2.Addsqlhelper(B);
sqllhelper2.Add<int>();
}
}
}