适配器模式 c# 适配器模式
人气:0想了解c# 适配器模式的相关内容吗,在本文为您仔细讲解适配器模式的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:适配器模式,下面大家一起来学习吧。
结构图: ![](https://img.jbzj.com/file_images/article/201210/20121029140439227.gif)
客户可以对接的接口类:
复制代码 代码如下:
class Target
{
public virtual void Request()
{
Console.WriteLine("普通请求!");
}
}
客户需要使用适配器才能使用的接口:
复制代码 代码如下:
class Adaptee
{
public void SpecialRequest()
{
Console.WriteLine("特殊请求!");
}
}
适配器的定义:继承与Target类
复制代码 代码如下:
class Adapter : Target
{
Adaptee ad = new Adaptee();
public override void Request()
{
ad.SpecialRequest();
}
}
主函数的调用:
复制代码 代码如下:
class Program
{
static void Main(string[] args)
{
Target ta = new Target();
ta.Request();
Target sta = new Adapter();
sta.Request();
Console.ReadKey();
}
}
原本不可以使用的接口,通过适配器之后可以使用了。
加载全部内容