接口模式 - 1.1 适配器模式 --代码实现(C#)
2009-08-16 11:56
441 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
IDoSome_target idosome = new DoSome_Adapter();
idosome.request();
Console.ReadKey();
IDoSome_target2 idosome2 = new DoSome_Adapter2();
idosome2.request();
Console.ReadKey();
IDoSome_target3 idosome3 = new DoSome_Adapter3();
idosome3.request();
Console.ReadKey();
}
}
# region 类适配器模式
/// <summary>
/// 当客户(Client)已存在固定接口(IDoSome_target)时,而新开发的类的(DoSome)方法名或者方法不符合客户调用的时候,
/// 需要创建一个中间类(DoSome_Adapter)来调用已经存在的类,可以使用类适配器模式
/// </summary>
interface IDoSome_target
{
void request();
}
public class DoSome
{
public void specialRequest()
{
Console.WriteLine("reponse to special request!");
}
}
public class DoSome_Adapter:DoSome,IDoSome_target
{
public void request()
{
this.specialRequest();
}
}
#endregion
# region 对象适配器模式
/// <summary>
/// 当客户(Client) 未定义接口,而是一个类(Target),新开发的类的(Adaptee)方法名或者方法不符合客户调用的时候,
/// 需要创建一个中间类(Adapter)来调用已经存在的类,可以使用对象适配器模式
/// </summary>
public class IDoSome_target2
{
public virtual void request() { Console.WriteLine("2--"); }
}
public class DoSome2
{
public void specialRequest()
{
Console.WriteLine("2---reponse to special request!");
}
}
public class DoSome_Adapter2 :IDoSome_target2
{
private DoSome2 _DoSome2= new DoSome2();
public override void request()
{
_DoSome2.specialRequest();
}
}
#endregion
# region 对象适配器模式2
/// <summary>
/// 当客户(Client) 未定义接口,而是一个类(Target),新开发的类的(Adaptee)方法名或者方法不符合客户调用的时候,
/// 需要创建一个中间类(Adapter)来调用已经存在的类,可以使用对象适配器模式
/// </summary>
public abstract class IDoSome_target3
{
public abstract void request();
}
public class DoSome3
{
public void specialRequest()
{
Console.WriteLine("3---reponse to special request!");
}
}
public class DoSome_Adapter3 : IDoSome_target3
{
private DoSome3 _DoSome3 = new DoSome3();
public override void request()
{
_DoSome3.specialRequest();
}
}
#endregion
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
IDoSome_target idosome = new DoSome_Adapter();
idosome.request();
Console.ReadKey();
IDoSome_target2 idosome2 = new DoSome_Adapter2();
idosome2.request();
Console.ReadKey();
IDoSome_target3 idosome3 = new DoSome_Adapter3();
idosome3.request();
Console.ReadKey();
}
}
# region 类适配器模式
/// <summary>
/// 当客户(Client)已存在固定接口(IDoSome_target)时,而新开发的类的(DoSome)方法名或者方法不符合客户调用的时候,
/// 需要创建一个中间类(DoSome_Adapter)来调用已经存在的类,可以使用类适配器模式
/// </summary>
interface IDoSome_target
{
void request();
}
public class DoSome
{
public void specialRequest()
{
Console.WriteLine("reponse to special request!");
}
}
public class DoSome_Adapter:DoSome,IDoSome_target
{
public void request()
{
this.specialRequest();
}
}
#endregion
# region 对象适配器模式
/// <summary>
/// 当客户(Client) 未定义接口,而是一个类(Target),新开发的类的(Adaptee)方法名或者方法不符合客户调用的时候,
/// 需要创建一个中间类(Adapter)来调用已经存在的类,可以使用对象适配器模式
/// </summary>
public class IDoSome_target2
{
public virtual void request() { Console.WriteLine("2--"); }
}
public class DoSome2
{
public void specialRequest()
{
Console.WriteLine("2---reponse to special request!");
}
}
public class DoSome_Adapter2 :IDoSome_target2
{
private DoSome2 _DoSome2= new DoSome2();
public override void request()
{
_DoSome2.specialRequest();
}
}
#endregion
# region 对象适配器模式2
/// <summary>
/// 当客户(Client) 未定义接口,而是一个类(Target),新开发的类的(Adaptee)方法名或者方法不符合客户调用的时候,
/// 需要创建一个中间类(Adapter)来调用已经存在的类,可以使用对象适配器模式
/// </summary>
public abstract class IDoSome_target3
{
public abstract void request();
}
public class DoSome3
{
public void specialRequest()
{
Console.WriteLine("3---reponse to special request!");
}
}
public class DoSome_Adapter3 : IDoSome_target3
{
private DoSome3 _DoSome3 = new DoSome3();
public override void request()
{
_DoSome3.specialRequest();
}
}
#endregion
}
相关文章推荐
- 转载-----C# 设计模式 - 1 接口模式 - 1.1 适配器模式
- C# 设计模式 - 1 接口模式 - 1.1 适配器模式
- 设计模式-代理模式与适配器模式实现代码重用以及策略模式的使用
- c#短信接口代码实现(发短信)
- 《第一行代码Java》接口实现代理设计模式(Proxy)笔记与代码
- 改进C#代码之22:通过定义并实现接口替代继承
- 《模式――工程化实现及扩展》(设计模式C# 版)《连贯接口 Fluent Interface》――“自我检验"参考答案
- 改进C#代码之24:通过定义并实现接口替代继承
- php中使用接口实现工厂设计模式的代码
- 设计模式C#实现(二)——适配器模式
- 设计模式----单例模式UML图和代码实现(C#&JAVA)
- 《模式——工程化实现及扩展》(设计模式C# 版)《适配器模式 Adapter》——“自我检验"参考答案
- 改进C#代码之25:用事件模式实现通知
- 【出版直播】博客园征途系列,《设计模式——基于C#的工程化实现与扩展》电子书、示例代码发布,互动网预订开始
- 深入浅出学习设计模式(C#实现)-适配器模式
- winform程序三个窗体间同步数据(五):接口实现多态减少代码(观察者模式)
- 研磨设计模式解析及python代码实现——(三)适配器模式(Adapter)
- C#代码:用事件模式实现通知
- 创作型---原型模式(C# ICloneable接口的实现)
- C# 两行代码实现 延迟加载的单例模式(线程安全)