设计模式c#语言描述——适配器模式
来源:互联网 发布:app生成软件 编辑:程序博客网 时间:2024/06/05 07:49
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
*本文参考了《JAVA与模式》的部分内容,适合于设计模式的初学者。
适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。包括类的适配器模式和对象的适配器模式两种不同的形式。
类的适配器模式把被适配的类的API转换成为目标类的API,其静态结构图如下:
模式的角色如下:
目标(Target)角色:这就是所期待得到的接口。注意这里讨论的是类的适配器模式,因此目标不可以是类。
源(Adaptee)角色:现有需要适配的接口。
适配器(Adapter)角色:把源接口转换成目标接口。
Target;
public interface Target
{
void sampleOperation1();
void sampleOperation2();//源类不包含的方法
}// END INTERFACE DEFINITION Target
Adaptee;
public class Adaptee
{
public void sampleOperation1()
{
}
}// END CLASS DEFINITION Adaptee
Adapter;
public class Adapter : Adaptee,Target
{
public void sampleOperation2()
{
}
}// END CLASS DEFINITION Adapter
类的适配器模式的效果:
使用一个具体类把源适配到目标中,这样一来,如果源以及源的子类都使用此类适配,就形不通了。
由于适配器类是源的子类,因此可以在适配器类中置换掉(Override)源的一些方法。
与类的适配器模式相似,对象的适配器模式把被适配的类的API转换成目标类的API,与类的适配器模式不同,对象的适配器模式不是使用继承关系连接到Adaptee类,而是使用委派关系,类图如下所示:
Target;
public interface Target
{
void sampleOperation1();
void sampleOperation2();
}// END INTERFACE DEFINITION Target
Adaptee;
public class Adaptee
{
public void sampleOperation1()
{
}
}// END CLASS DEFINITION Adaptee
Adapter:
public class Adapter : Target
{
private Adaptee adaptee;
public void Adapter(Adaptee adaptee)
{
this.adaptee=adaptee;
}
public void sampleOperation1()
{
adaptee.sampleOperation1();
}
public void sampleOperation2()
{
}
}// END CLASS DEFINITION Adapter
对象适配器模式的效果:
一个适配器可以把多种不同的源适配到同一个目标。也就是说,同一个适配器可以把源类和它的子类都适配到目标接口。
与类的适配器相比,要想置换源类的方法就不容易。如果一定要置换源类的方法,就只好先做一个源类的子类,将方法置换掉,再把源类的子类当作真正的源进行适配。
设计模式c#语言描述——适配器模式';return true">
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
设计模式c#语言描述——适配器(Adapter)模式 *本文参考了《JAVA与模式》的部分内容,适合于设计模式的初学者。
适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。包括类的适配器模式和对象的适配器模式两种不同的形式。
类的适配器模式把被适配的类的API转换成为目标类的API,其静态结构图如下:
模式的角色如下:
目标(Target)角色:这就是所期待得到的接口。注意这里讨论的是类的适配器模式,因此目标不可以是类。
源(Adaptee)角色:现有需要适配的接口。
适配器(Adapter)角色:把源接口转换成目标接口。
Target;
public interface Target
{
void sampleOperation1();
void sampleOperation2();//源类不包含的方法
}// END INTERFACE DEFINITION Target
Adaptee;
public class Adaptee
{
public void sampleOperation1()
{
}
}// END CLASS DEFINITION Adaptee
Adapter;
public class Adapter : Adaptee,Target
{
public void sampleOperation2()
{
}
}// END CLASS DEFINITION Adapter
类的适配器模式的效果:
使用一个具体类把源适配到目标中,这样一来,如果源以及源的子类都使用此类适配,就形不通了。
由于适配器类是源的子类,因此可以在适配器类中置换掉(Override)源的一些方法。
与类的适配器模式相似,对象的适配器模式把被适配的类的API转换成目标类的API,与类的适配器模式不同,对象的适配器模式不是使用继承关系连接到Adaptee类,而是使用委派关系,类图如下所示:
Target;
public interface Target
{
void sampleOperation1();
void sampleOperation2();
}// END INTERFACE DEFINITION Target
Adaptee;
public class Adaptee
{
public void sampleOperation1()
{
}
}// END CLASS DEFINITION Adaptee
Adapter:
public class Adapter : Target
{
private Adaptee adaptee;
public void Adapter(Adaptee adaptee)
{
this.adaptee=adaptee;
}
public void sampleOperation1()
{
adaptee.sampleOperation1();
}
public void sampleOperation2()
{
}
}// END CLASS DEFINITION Adapter
对象适配器模式的效果:
一个适配器可以把多种不同的源适配到同一个目标。也就是说,同一个适配器可以把源类和它的子类都适配到目标接口。
与类的适配器相比,要想置换源类的方法就不容易。如果一定要置换源类的方法,就只好先做一个源类的子类,将方法置换掉,再把源类的子类当作真正的源进行适配。
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- 设计模式c#语言描述——适配器模式
- 设计模式c#语言描述——适配器模式
- 设计模式c#语言描述——适配器模式
- 设计模式-适配器模式(Go语言描述)
- 设计模式-适配器模式(Go语言描述)
- 设计模式c#语言描述——建造者模式
- 设计模式c#语言描述——建造者模式、适配器模式、合成(Composite)模式
- 设计模式c#语言描述——合成(Composite)模式
- 设计模式C#描述——抽象工厂模式
- 设计模式C#描述之——简单工厂模式
- 设计模式之适配器模式--java描述
- 【C#设计模式-适配器模式】
- C#设计模式--适配器模式
- 设计模式c#语言描述——建造者模式
- 设计模式c#语言描述——建造者模式
- C# 设计模式 之 适配器模式(Adapter)
- c#设计模式之[适配器模式]
- C#设计模式——适配器模式
- Counting Records in an SQL Table by G.F. Weis Gfw
- VSS资料(我为公司写的,部分是ICP来的)
- VB.Net基本语句(推荐)
- 一个数据库操作类的实例(二.Provider类)
- 我已经解决了,以下是解决的类的详细信息:
- 设计模式c#语言描述——适配器模式
- 在ASP.NET下实现数字和字符相混合的验证码(C# Code)
- 利用WebService技术实现远程数据库存取
- 利用PocketSOAP链接webservice(1)---.net
- 窗体显示有模式与无模式之分...
- 初尝Visual Perl和Perl.Net
- 在ASP.NET下实现数字和字符相混合的验证码(vb.net Code)
- 设计模式c#语言描述——建造者模式
- 让Asp.NET的DataGrid可排序、可选择、可分页