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
}