适配器模式如何工作?

发布于 2024-11-03 00:12:41 字数 371 浏览 0 评论 0原文

我最近在学期课程中读到了有关适配器模式的内容。我已经理解了提供通用接口以便第三方API可以在我们的系统中工作的概念。

但是它在编码中如何工作,假设我有两个第三方 SDK,例如:

A(String name)

A1(String Firstname,String Lastname)

现在我生成称为 IAAdapater 的通用接口。现在编写代码,现在我的问题是,如何通过 IAdapater 调用 A(String name)A1(String Firstname,String Lastname)

如果我错了请纠正我!

I recently read about Adapter pattern in my semester course. I have understood the concept of providing the common interface so that the third party APIs can work in our system.

But how does it will work in coding, lets say that i have two third party SDK, such as:

A(String name)

A1(String Firstname,String Lastname)

Now i produce the common interface called as, say, IAAdapater. And now write the code in that, now my question is, how does one call A(String name) or A1(String Firstname,String Lastname) through IAAdapater?

Correct me if I'm wrong!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧街凉风 2024-11-10 00:12:41

编辑我认为另一个答案更接近您正在寻找的内容 - 我实际上在这里描述了一个外观。

您的 IAAdapter 需要公开方法 A(String name) 和 A1(String Firstname, String Lastname)

然后您需要一个委托给底层对象的接口的实现。

IAAdaptor{
A(string name);
A1(string firstname, string lastnae);
}

Adaptor:IAAdaptor{
A(string name){}
A1(string firstname, string lastnae){}
}

Edit I think the other answer is closer to what you are looking for - Im actually describing a Facade here.

Your IAAdapter needs to expose the methods A(String name) and A1(String Firstname, String Lastname)

You then need an implementation of this interface that delegates to the underlying objects.

IAAdaptor{
A(string name);
A1(string firstname, string lastnae);
}

Adaptor:IAAdaptor{
A(string name){}
A1(string firstname, string lastnae){}
}
我不是你的备胎 2024-11-10 00:12:41
interface IAAdapater {
  A(firstname, lastname)
}

// Adapter for A SDK
A_adapter implements IAAdapter {
  A(firstname, lastname) {
    call A(firstname + ' ' + lastname)
  }
}

// Adapter for A1 SDK
A1_adapter implements IAAdapter {
  A(firstname, lastname) {
    call A1(firstname, lastname)
  }
}

现在您只需担心一个 API:名字、姓氏。不同的适配器将处理其余的事情。

call A_adapter.A('will', 'smith');
call A1_adapter.A('will', 'smith');

如果我误解了这个问题,请告诉我。

interface IAAdapater {
  A(firstname, lastname)
}

// Adapter for A SDK
A_adapter implements IAAdapter {
  A(firstname, lastname) {
    call A(firstname + ' ' + lastname)
  }
}

// Adapter for A1 SDK
A1_adapter implements IAAdapter {
  A(firstname, lastname) {
    call A1(firstname, lastname)
  }
}

Now you only have to worry about one API: firstname, lastname. The different adapters will handle the rest.

call A_adapter.A('will', 'smith');
call A1_adapter.A('will', 'smith');

Please let me know if I misunderstood the question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文