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

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

问题描述

我想向我的团队演示适配器模式的使用.我在网上阅读了很多书籍和文章.每个人都在举一个有助于理解概念的例子(形状、存储卡、电子适配器等),但没有真正的案例研究.

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?

附言我尝试在 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.

推荐答案

Adapter 的许多例子都是微不足道的或不切实际的 (矩形与传统矩形、棘轮与套接字SquarePeg 与 RoundPeg鸭对土耳其).更糟糕的是,许多没有显示针对不同 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 的多个实现(adaptees).我们希望避免硬编码我们的域层代码来处理计算税收、售后、授权信用卡请求等的不同可能方式.这些都是可能会有所不同的外部模块,我们无法修改它们代码.适配器允许我们在适配器中进行硬编码,而我们的领域层代码总是使用相同的接口(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天全站免登陆