我想向我的团队演示Adapter Pattern的用法。我在网上读了很多书和文章。每个人都在举一个例子,这些例子有助于理解概念(形状、存储卡、电子适配器等),但没有真正的案例研究。
您能分享一些Adapter模式的案例研究吗?
附注:我尝试在stackoverflow上搜索现有问题,但没有找到答案,因此将其作为新问题发布。如果你知道这个问题已经有答案了,请重定向。
发布于 2012-11-11 00:19:54
许多适配器的示例都是琐碎的或不现实的(Rectangle vs. LegacyRectangle, Ratchet vs. Socket、SquarePeg vs RoundPeg、Duck vs. Turkey)。更糟糕的是,许多人没有显示用于不同适配器 (someone cited Java's Arrays.asList as an example of the adapter pattern)的多适配器。将只有一个类的接口调整为与另一个类配合使用似乎是GoF适配器模式的一个薄弱示例。此模式使用继承和多态性,因此可以期待一个很好的示例来展示不同适配器的多个适配器实现。
我找到的最好的例子是在Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development (3rd Edition)的26章。以下图片来自本书的FTP站点上提供的教师材料。
第一个示例展示了应用程序如何使用功能相似的多个实现(适配器)(例如,税务计算器、会计模块、信用授权服务等)。但是有不同的API。我们希望避免硬编码我们的域层代码,以处理不同可能的方式来计算税收,售后,授权信用卡请求等。这些都是外部模块,可能会有所不同,我们不能修改代码。适配器允许我们在适配器中进行硬编码,而我们的域层代码总是使用相同的接口( IWhateverAdapter接口)。

我们在上图中看不到实际的适配者。但是,下图显示了如何在IAccountingAdapter接口中对postSale(...)进行多态调用,从而通过SOAP将销售发布到IAccountingAdapter系统。

发布于 2015-08-27 22:15:10
如何把一个法国人变成一个普通人。
public interface IPerson
{
string Name { get; set; }
}
public interface IFrenchPerson
{
string Nom { get; set; }
}
public class Person : IPerson
{
public string Name { get; set; }
}
public class FrenchPerson : IFrenchPerson
{
public string Nom { get; set; }
}
// that is a service that we want to use with our French person
// we cannot or don't want to change the service contract
// therefore we need 'l'Adaptateur'
public class PersonService
{
public void PrintName(IPerson person)
{
Debug.Write(person.Name);
}
}
public class FrenchPersonAdapter : IPerson
{
private readonly IFrenchPerson frenchPerson;
public FrenchPersonAdapter(IFrenchPerson frenchPerson)
{
this.frenchPerson = frenchPerson;
}
public string Name
{
get { return frenchPerson.Nom; }
set { frenchPerson.Nom = value; }
}
} 示例
var service = new PersonService();
var person = new Person();
var frenchPerson = new FrenchPerson();
service.PrintName(person);
service.PrintName(new FrenchPersonAdapter(frenchPerson));发布于 2015-07-17 07:10:53
将一个接口转换为另一个接口。
适配器模式的任何真实示例
为了连接电源,我们在世界各地有不同的接口。使用Adapter,我们可以像wise一样轻松地连接。

https://stackoverflow.com/questions/11079605
复制相似问题