VS 2008

一个已有的组件(类库)提供的接口与当前客户系统请求的接口不一致时,使用适配器模式,将已有组件的接口转换为客户系统请求的接口。

1. 模式UML图

Core Design Patterns(6) Adapter 适配器模式
2. 应用

    目前我们有一套现有的文本日志记录组件,提供了一套供客户端代码请求的接口。
    然而客户端代码请求的却是另外一套接口,为了复用现有的文本日志记录组件,我们使用适配器模式。

Core Design Patterns(6) Adapter 适配器模式

TextLogger.cs

Core Design Patterns(6) Adapter 适配器模式using System;
Core Design Patterns(6) Adapter 适配器模式
using System.Collections.Generic;
Core Design Patterns(6) Adapter 适配器模式
using System.Linq;
Core Design Patterns(6) Adapter 适配器模式
using System.Text;
Core Design Patterns(6) Adapter 适配器模式

ILogger.cs

Core Design Patterns(6) Adapter 适配器模式using System;
Core Design Patterns(6) Adapter 适配器模式
using System.Collections.Generic;
Core Design Patterns(6) Adapter 适配器模式
using System.Linq;
Core Design Patterns(6) Adapter 适配器模式
using System.Text;
Core Design Patterns(6) Adapter 适配器模式

LogAdapter.cs

Core Design Patterns(6) Adapter 适配器模式using System;
Core Design Patterns(6) Adapter 适配器模式
using System.Collections.Generic;
Core Design Patterns(6) Adapter 适配器模式
using System.Linq;
Core Design Patterns(6) Adapter 适配器模式
using System.Text;
Core Design Patterns(6) Adapter 适配器模式

Client

Core Design Patterns(6) Adapter 适配器模式using System;
Core Design Patterns(6) Adapter 适配器模式
using System.Collections.Generic;
Core Design Patterns(6) Adapter 适配器模式
using System.Linq;
Core Design Patterns(6) Adapter 适配器模式
using System.Text;
Core Design Patterns(6) Adapter 适配器模式
using DesignPattern.Adapter.BLL;
Core Design Patterns(6) Adapter 适配器模式

Output

Core Design Patterns(6) Adapter 适配器模式

3. 思考

应用中描述的是最普通的适配器模式的应用
继续扩展,可以有双向适配器、可插入式适配器等。

相关文章: