适配器 - 适配器模式的任何实例 [英] adapter-Any real example of Adapter Pattern

查看:458
本文介绍了适配器 - 适配器模式的任何实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向我的团队演示使用适配器模式。我在线阅读了许多书籍和文章。每个人都引用一个例子来理解概念(形状,记忆卡,电子适配器等),但没有真正的案例研究。

I want to demonstrate use of Adapter Pattern to my team. I've read many books and articles online. Everyone is citing an example which are useful to understand the concept (Shape, Memory Card, Electronic Adapter etc.), but there is no real case study.

分享适配器模式的任何案例研究?

Can you please share any case study of Adapter Pattern?

ps我尝试在stackoverflow上搜索现有的问题,但没有找到答案,将其作为一个新问题发布。

p.s. I tried searching existing questions on stackoverflow, but did not find the answer so posting it as a new question. If you know there's already an answer for this, then please redirect.

推荐答案

适配器的许多示例是微不足道的或不切实际的(矩形与LegacyRectangle,棘轮与套接字 SquarePeg vs RoundPeg Duck vs. Turkey )。更糟糕的是,许多用户不会为不同的Adaptees显示多个适配器有人引用Java的Arrays.asList作为适配器模式的示例)。使用只有一个类的接口与另一个类似的接口似乎是GoF适配器模式的一个很弱的例子。这种模式使用继承和多态,所以人们期待一个很好的例子来显示不同适应者的适配器的多个实现。

Many examples of Adapter are trivial or unrealistic (Rectangle vs. LegacyRectangle, Ratchet vs. Socket, SquarePeg vs RoundPeg, Duck vs. Turkey). Worse, many don't show multiple Adapters for different Adaptees (someone cited Java's Arrays.asList as an example of the adapter pattern). Adapting an interface of only one class to work with another seems a weak example of the GoF Adapter pattern. This pattern uses inheritance and polymorphism, so one would expect a good example to show multiple implementations of adapters for different adaptees.

我发现最佳示例应用UML和模式:面向对象分析与设计与迭代开发的介绍(第3版)。以下图像来自本书的FTP站点上提供的教师资料。

The best example I found is in Chapter 26 of Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development (3rd Edition). The following images are from the instructor material provided on an FTP site for the book.

第一个显示应用程序如何使用功能相似的多个实现(适配器)(例如税务计算器,会计模块,信用授权服务等),但有不同的API。我们希望避免对我们的域层代码进行硬编码,以处理不同的可能方式来计算税收,售后服务,授权信用卡请求等。这些都是外部模块可能会有所不同,我们无法修改码。适配器允许我们在适配器中进行硬编码,而我们的域层代码总是使用相同的接口(IWhateverAdapter接口)。

The first one shows how an application can use multiple implementations (adaptees) that are functionally similar (e.g., tax calculators, accounting modules, credit authorization services, etc.) but have different APIs. We want to avoid hard-coding our domain-layer code to handle the different possible ways to calculate tax, post sales, authorize credit card requests, etc. Those are all external modules that might vary, and for which we can't modify the code. The adapter allows us to do the hard-coding in the adapter, whereas our domain-layer code always uses the same interface (the IWhateverAdapter interface).

我们在上图中看不到实际适应。然而,下图显示了如何在IAccountingAdapter接口中对 postSale(...)进行多态调用,这导致通过SOAP将销售过帐发布到SAP系统。

We don't see in the above figure the actual adaptees. However, the following figure shows how a polymorphic call to postSale(...) in the IAccountingAdapter interface is made, which results in a posting of the sale via SOAP to an SAP system.

这篇关于适配器 - 适配器模式的任何实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆