201802151200->深入浅出设计模式:c#适配器模式
2018-02-15 12:00
441 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _005适配器模式
{
#region 模式1
/*-------------------------------------适配接口-------------------------------------*/
public interface ITarget
{
void GetTargetInterface();
}
/*-------------------------------------适配接口-------------------------------------*/
/*-------------------------------------适配实体-------------------------------------*/
public class Adaptee
{
public void GetTargetData()
{
Console.WriteLine("已获取目标数据...");
}
}
/*-------------------------------------适配实体-------------------------------------*/
/*-------------------------------------适配器-------------------------------------*/
public class Adapter : Adaptee, ITarget
{
public void GetTargetInterface()
{
this.GetTargetData();
}
}
/*-------------------------------------适配器-------------------------------------*/
#endregion 模式1
#region 模式2
/*-------------------------------------适配器-------------------------------------*/
public class Adapter2 : ITarget
{
public Adaptee adaptee = new Adaptee();
public void GetTargetInterface()
{
adaptee.GetTargetData();
}
}
/*-------------------------------------适配器-------------------------------------*/
#endregion 模式2
internal class Program
{
private static void Main(string[] args)
{
ITarget target1 = new Adapter();
target1.GetTargetInterface();
Console.WriteLine("模式1适配器...");
ITarget target2 = new Adapter();
target2.GetTargetInterface();
Console.WriteLine("模式2适配器...");
/*
* 模式1利用调用基类来实现接口以达到适配的目的
*
* 模式2利用调用实例方法来达到适配目标
*/
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _005适配器模式
{
#region 模式1
/*-------------------------------------适配接口-------------------------------------*/
public interface ITarget
{
void GetTargetInterface();
}
/*-------------------------------------适配接口-------------------------------------*/
/*-------------------------------------适配实体-------------------------------------*/
public class Adaptee
{
public void GetTargetData()
{
Console.WriteLine("已获取目标数据...");
}
}
/*-------------------------------------适配实体-------------------------------------*/
/*-------------------------------------适配器-------------------------------------*/
public class Adapter : Adaptee, ITarget
{
public void GetTargetInterface()
{
this.GetTargetData();
}
}
/*-------------------------------------适配器-------------------------------------*/
#endregion 模式1
#region 模式2
/*-------------------------------------适配器-------------------------------------*/
public class Adapter2 : ITarget
{
public Adaptee adaptee = new Adaptee();
public void GetTargetInterface()
{
adaptee.GetTargetData();
}
}
/*-------------------------------------适配器-------------------------------------*/
#endregion 模式2
internal class Program
{
private static void Main(string[] args)
{
ITarget target1 = new Adapter();
target1.GetTargetInterface();
Console.WriteLine("模式1适配器...");
ITarget target2 = new Adapter();
target2.GetTargetInterface();
Console.WriteLine("模式2适配器...");
/*
* 模式1利用调用基类来实现接口以达到适配的目的
*
* 模式2利用调用实例方法来达到适配目标
*/
Console.ReadKey();
}
}
}
相关文章推荐
- 201802121524->深入浅出设计模式:c#简单工厂模式
- 201802141508->深入浅出设计模式:c#创建者模式
- 201802181505->深入浅出设计模式:c#装饰者模式
- 201802181721->深入浅出设计模式:c#享元模式
- 201802191230->深入浅出设计模式:c#代理模式
- 201802282138->深入浅出设计模式:c#访问者模式
- 201802132228->深入浅出设计模式:c#抽象工厂模式
- 201802151313->深入浅出设计模式:c#桥模式
- 201802181537->深入浅出设计模式:c#外观模式
- 201802272223->深入浅出设计模式:c#状态模式
- 201802272238->深入浅出设计模式:c#模板模式
- 201802141601->深入浅出设计模式:c#原型模式
- 深入浅出学习设计模式(C#实现)-适配器模式
- 201802170025->深入浅出设计模式:c#组合模式
- 201802191355->深入浅出设计模式:c#迭代器模式
- 201802272130->深入浅出设计模式:c#观察者模式
- 201802191355->深入浅出设计模式:c#解析器模式
- 201802140000->深入浅出设计模式:c#高并发下单例模式
- 201802262211->深入浅出设计模式:c#备忘录模式
- c#设计模式-适配器模式