• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > C#设计模式编程中运用适配器模式结构实战演练

C#设计模式编程中运用适配器模式结构实战演练

作者:Libing 字体:[增加 减小] 来源:互联网 时间:2017-05-28

Libing 通过本文主要向大家介绍了c站,欲情 c max,维生素c,奔驰c200,85度c等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

 在实际的软件系统设计和开发中,为了完成某项工作需要购买一个第三方的库来加快开发。这带来一个问题,在应用程序中已经设计好的功能接口,与这个第三方提供的接口不一致。为了使得这些接口不兼容的类可以在一起工作,适配器模式提供了一种接口的适配机制。


  适配器模式的设计思想在生活中经常会应用到,如我们在给手机充电的时候,不可能直接在220V电源上直接充电,而是用手机充电器转换成手机需要的电压才可以正常充电,否则就不可以完成充电,这个充电器就起到了适配的作用。

适配器模式结构实现

1.类适配器结构实现

2016217155956456.png (367×596)

ITarget.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.AdapterPattern.Structural.ClassAdapter
{
 public interface ITarget
 {
  void Request();
 }
}  Adaptee.cs:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.AdapterPattern.Structural.ClassAdapter
{
 public class Adaptee
 {
  public void SpecificRequest()
  {
   Console.WriteLine("Called SpecificRequest()");
  }
 }
}  Adapter.cs:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.AdapterPattern.Structural.ClassAdapter
{
 public class Adapter : Adaptee, ITarget
 {
  public void Request()
  {
   this.SpecificRequest();
  }
 }
}  Client.cs:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.AdapterPattern.Structural.ClassAdapter
{
 public class Client
 {
  static void Main(string[] args)
  {
   ITarget t = new Adapter();
   t.Request();
  }
 }
}  
</div>

运行输出:

Called SpecificRequest()
请按任意键继续. . .

</div>

2.对象适配器结构实现

  Client需要调用Request方法,而Adaptee并没有该方法,为了使Client能够使用Adaptee类,需要提供一个类Adapter。这个类包含了一个Adaptee的实例,将Client与Adaptee衔接起来。

ITarget.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.AdapterPattern.Structural.ObjectAdapter
{
 public interface ITarget
 {
  void Request();
 }
}  
</div>

Target.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.AdapterPattern.Structural.ObjectAdapter
{
 public class Target : ITarget
 {
  public virtual void Request()
  {
   Console.WriteLine("Called Target Request()");
  }
 }
}  
</div>

Adaptee.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.AdapterPattern.Structural.ObjectAdapter
{
 public class Adaptee
 {
  public void SpecificRequest()
  {
   Console.WriteLine("Called SpecificRequest()");
  }
 }
} 

</div>

 Adapter.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.AdapterPattern.Structural.ObjectAdapter
{
 public class Adapter : Target
 {
  private Adaptee _adaptee = new Adaptee();

  public override void Request()
  {
   _adaptee.SpecificRequest();
  }
 }
} 

</div>

 Client.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.AdapterPattern.Structural.ObjectAdapter
{
 public class Client
 {
  static void Main(string[] args)
  {
   ITarget t = new Adapter();
   t.Request();
  }
 }
}

</div>


适配器模式实践应用

以手机充电的电源适配器为例,用适配器模式的解决方案。

2016217160138027.png (367×596)

1.类适配器结构实现
ITarget.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.AdapterPattern.Practical.ClassAdapter
{
 public interface ITarget
 {
  void GetPower();
 }
}  Power.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.AdapterPattern.Practical.ClassAdapter
{
 public class Power
 {
  public void GetPower220V()
  {
   Console.WriteLine("从电源中得到220V的电压");
  }
 }
}  Adapter.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.AdapterPattern.Practical.ClassAdapter
{
 public class Adapter : Power, ITarget
 {
  public void GetPower()
  {
   this.GetPower220V();
   Console.WriteLine("得到手机的充电电压!");
  }
 }
}  Client.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.AdapterPattern.Practical.ClassAdapter
{
 public class Client
 {
  static void Main(string[] args)
  {
   Console.WriteLine("手机:");
   ITarget t = new Adapter();
   t.GetPower();
  }
 }
}  
</div>

运行输出:

手机:
从电源中得到220V的电压
得到手机的充电电压!
请按任意键继续. . .

</div>

2.对象适配器结构实现
ITarget.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.AdapterPattern.Practical.ObjectAdapter
{
 public interface ITarget
 {
  void GetPower();
 }
} 
</div>

 Power.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.AdapterPattern.Practical.ObjectAdapter
{
 public class Power
 {
  public void GetPower220V()
  {
   Console.WriteLine("从电源中得到220V的电压");
  }
 }
} 
</div>

 Adapter.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.AdapterPattern.Practical.ObjectAdapter
{
 public class Adapter : ITarget
 {
  public Power _power;
  public Adapter(Power power)
  {
   this._power = power;
  }

  /// <summary>
  /// 得到想要的电压
  /// </summary>
  public void GetPower()
  {
   _power.GetPower220V();
   Console.WriteLine("得到手机的充电电压!");
  }
 }
} 
</div>

 Client.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.AdapterPattern.Practical.ObjectAdapter
{
 public class Client
 {
  static void Main(string[] args)
  {
   Console.WriteLine("手机:");
   ITarget t = new Adapter(new Power());
   t.GetPower();
  }
 }
}

</div>

适配器模式的优缺点
在引言部分已经提出,适配器模式用来解决现有对象与客户端期待接口不一致的问题,下面详细总结下适配器两种形式的优缺点。
1.类的适配器模式:
优点:
可以在不修改原有代码的基础上来复用现有类,很好

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C# 检索不区分大小写并高亮显示实例详解
  • C#实现Base64处理的加密解密,编码解码示例
  • C# SqlHelper应用开发学习
  • C#多线程经典示例(吃苹果)
  • C#使用Windows Service的简单教程(创建、安装、卸载、调试)
  • C# 6.0 的知识梳理
  • C#向PPT文档插入图片以及导出图片的实例
  • C#使用Jquery zTree实现树状结构显示 异步数据加载
  • C#清理非托管对象实例分析
  • C#双缓冲技术实例详解

相关文章

  • 2017-05-28C#中将DataTable转化成List<T>的方法解析
  • 2017-05-28c#中禁用windows的任务管理器的方法
  • 2017-05-28C#小程序15位转18位身份证号代码
  • 2017-05-28C#多维数组学习使用
  • 2017-05-28C#中timer定时器用法实例
  • 2017-05-28浅谈Async和Await如何简化异步编程(几个实例让你彻底明白)
  • 2017-05-28轻松学习C#的属性
  • 2017-05-28c#分页显示服务器上指定目录下的所有图片示例
  • 2017-05-28C#将dll打包到程序中的具体实现
  • 2017-05-28C#中系统时间和UNIX时间戳互相转换

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • c# DataTable与不同结构实体类转换的方法实例
    • C#实现利用Windows API读写INI文件的方法
    • 用C#在本地创建一个Windows帐户(DOS命令)
    • C#验证控件validator的简单使用
    • C# 排序算法之堆排序
    • C#使用foreach语句遍历二维数组的方法
    • C#常用目录文件操作类实例
    • Unity UGUI教程之实现滑页效果
    • 比较全的一个C#操作word文档示例
    • C# 委托(跨窗体操作控件)实例流程讲解

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有