这仍然是适配器模式吗?
我偶然发现了这个类,想知道 XYZAdapter 是否是正确的名称。我知道适配器模式是如何工作的,但这个解决方案有点不同:我不是实现 DataTable 接口并映射适当的方法调用,而是通过复制值并公开该对象来创建一个新的 DataTable 对象。看起来就是这样:
class Adapter
{
private NodeList list;
DataTable table { get { return CreateTable(); } }
Adapter(NodeList nl)
{
list = nl;
}
private DataTable CreateTable()
{
// Fetch Data in NodeList, create a Table and return it
// needs to be splitted in smaller methods ;D
}
}
通常我都是这样做的,但是 DataTable 接口非常大:
class Adapter : DataTable
{
private NodeList list;
DataTable table { get { return CreateTable(); } }
Adapter(NodeList nl)
{
list = nl;
}
// Here are all the DataTable methods mapped to NodeList
}
提前致谢!
I stumbled upon this class and was wondering if XYZAdapter might be the correct name. I know how the adapter pattern works, but this solution is a bit different: Instead of implementing the DataTable interface and mapping the appropriate method calls, im creating a new DataTable object by copying the values and expose this object. Thats how it looks:
class Adapter
{
private NodeList list;
DataTable table { get { return CreateTable(); } }
Adapter(NodeList nl)
{
list = nl;
}
private DataTable CreateTable()
{
// Fetch Data in NodeList, create a Table and return it
// needs to be splitted in smaller methods ;D
}
}
Usually im doing it this way, but the DataTable interface is enormus:
class Adapter : DataTable
{
private NodeList list;
DataTable table { get { return CreateTable(); } }
Adapter(NodeList nl)
{
list = nl;
}
// Here are all the DataTable methods mapped to NodeList
}
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
适配器将不兼容的接口适配为兼容的接口。例如,它将圆形钉子转换/包裹成方形钉子,以便它适合方形槽。
从技术上讲,您的解决方案不是适配器模式 - 它更像是翻译器或转换器。主要区别在于您的适配器无法在需要 DataTable 实例的方法中替换。
An adapter adapts a non-compliant interface into a compliant one. e.g. it transforms/wraps a circular peg to form a square peg, so that it fits a square slot.
Your solution is not technically the adapter pattern - it's more of a translator or a converter. The key difference being that your adapter cannot be substituted in methods expecting an instance of a DataTable.