惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Last Watchdog
The Last Watchdog
博客园_首页
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
美团技术团队
小众软件
小众软件
V
V2EX
博客园 - Franky
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
The GitHub Blog
The GitHub Blog
Microsoft Security Blog
Microsoft Security Blog
Attack and Defense Labs
Attack and Defense Labs
S
Security Affairs
Simon Willison's Weblog
Simon Willison's Weblog
I
Intezer
T
The Exploit Database - CXSecurity.com
有赞技术团队
有赞技术团队
S
Schneier on Security
人人都是产品经理
人人都是产品经理
Security Archives - TechRepublic
Security Archives - TechRepublic
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
K
Kaspersky official blog
PCI Perspectives
PCI Perspectives
AI
AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
罗磊的独立博客
O
OpenAI News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The Register - Security
The Register - Security
V
Vulnerabilities – Threatpost
GbyAI
GbyAI
博客园 - 【当耐特】
C
Cisco Blogs
大猫的无限游戏
大猫的无限游戏
Help Net Security
Help Net Security
Google DeepMind News
Google DeepMind News
S
Securelist
Application and Cybersecurity Blog
Application and Cybersecurity Blog
P
Proofpoint News Feed
博客园 - 三生石上(FineUI控件)
雷峰网
雷峰网
L
LangChain Blog
SecWiki News
SecWiki News
博客园 - 叶小钗
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V2EX - 技术
V2EX - 技术
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
J
Java Code Geeks
L
LINUX DO - 热门话题
Cisco Talos Blog
Cisco Talos Blog

博客园 - Vengen

【笔记】DataTable或IList使用GroupBy方法的lamda表达式 WSS学习笔记(3) – 在“用户控件”中使用SPGridView WSS学习笔记(2) - 用QuickPart部署“用户控件”来替代WebPart WSS学习笔记(1) - 创建简单的WebPart 用ILmerge工具将C#中的EXE和DLL文件合并成单个文件 修改WordPress文章索引列表的日期格式 Windows7部署Android开发环境傻瓜式教程(Eclipse+ADT) 如何使用C#访问WordPress的xmlrpc.php - Vengen - 博客园 Windows7部署WordPress傻瓜式教程(IIS7.5+MySQL+PHP+WordPress) C#面向对象设计模式学习笔记(10) - Facade 外观模式(结构型模式) C#面向对象设计模式学习笔记(9) - Decorator 装饰模式(结构型模式) C#面向对象设计模式学习笔记(8) - Composite 组合模式(结构型模式) C#面向对象设计模式学习笔记(7) - Bridge 桥接模式(结构型模式) C#面向对象设计模式学习笔记(5) - Prototype 原型模式(创建型模式) C#面向对象设计模式学习笔记(4) - Factory Method 工厂方法模式(创建型模式) C#面向对象设计模式学习笔记(3) - Builder 生成器模式(创建型模式) C#面向对象设计模式学习笔记(2) - Abstract Factory 抽象工厂模式(创建型模式) - Vengen C#面向对象设计模式学习笔记(1) - Singleton 单件模式(创建型模式) C#面向对象设计模式学习笔记 - 开篇
C#面向对象设计模式学习笔记(6) - Adapter 适配器模式(结构型模式)
Vengen · 2009-12-09 · via 博客园 - Vengen

适配,即在不改变原有实现的基础上,将不兼容的接口转换为兼容的接口。

将一个类的接口转换成客户程序希望的另一个接口;Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。

两种结构“对象适配器”和“类适配器”

interface IStack//客户期望的接口
{
    void Push(object item);
    object Pop();
    Object Peek();
}

1、对象适配器(推荐)

image 

class Adapter : IStack//适配对象
{
    ArrayList adpatee;//被适配的对象
    AnotherType adpatee2;//被适配的对象

    public Adapter()
    {
        adpatee = new ArrayList();
    }
    public void Push(object item)
    {
        adpatee.add(item);
    }
    object Pop()
    {
        return adpatee.RemoveAt(adpatee.Count - 1);
    }
    object Peek()
    {
        return adpatee[adpatee.Count - 1];
    }
}

2、类适配器(违背了一个类的职责,不是松耦合的解决方式,不提倡)

image 

class Adapter : ArrayList, IStack//适配对象
{
    public void Push(object item)
    {
        this.add(item);
    }
    object Pop()
    {
        return this.RemoveAt(adpatee.Count - 1);
    }
    object Peek()
    {
        return this[adpatee.Count - 1];
    }
}

3、灵活的结构:

Adapter模式可以实现的非常灵活,例如,完全可以将Adapter模式中的“现存对象”作为新的接口方法,来达到适配的目的。

class ExistingClass
{
    public void SpecificRequest1() { }
    public void SpecificRequest2() { }
}

//新环境所使用的接口
interface ITarget
{
    void Request();
}

//另外一个系统
class MySystem
{
    public void Process(ITarget target) { }
}

class Adapter : ITarget
{
    ExistingClass adaptee;
    public void Request()
    {
        adaptee.SpecificRequest1();
        adaptee.SpecificRequest2();
    }
}

4、例子:

集合类中对现有对象的排序(Adapter变体):现有对象未实现IComparable接口,实现一个排序适配器(继承IComparer接口),然后在其Compare方法中对两个对象进行比较。

class Employee
{
    int age;
    string name;
    public int Age
    {
        get { return this.age; }
        set { this.age = value; }
    }
    public string Name
    {
        get { return this.name; }
        set { this.name = value; }
    }
}
class EmployeeSortAdapter : IComparer
{
    public int Compare(object obj1, object obj2)
    {
        if (obj1.GetType() != typeof(Employee) || obj2.GetType() != typeof(Employee))
        {
            throw new Exception();
        }
        Employee e1 = (Employee)obj1;
        Employee e2 = (Employee)obj2;
        if (e1.Age == e2.Age)
        {
            return 0;
        }
        else if (e1.Age > e2.Age)
        {
            return 1;
        }
        else
        {
            return -1;
        }
    }
}
class App
{
    public static void Main()
    {
        Employee[] employees = new Employee[100];
        //...
        Array.Sort(employees, new EmployeeSortAdapter());
    }
}

要点:

1、Adapter模式主要应用于“希望复用一些现存的类,但是接口又与复用环境要求不一致的情况”,在遗留代码复用、类库适移等方面非常有用;

2、对象适配器采用“对象组合”的方式,更符合松耦合精神;类适配器采用“多继承”的实现方式,带来了不良的高耦合,所以一般不推荐使用;