使用“适配器”图案
据我了解,适配器模式的目标是使用某些接口(向客户端开放)调用某些类方法。为了实现适配器模式,我们需要实现一些接口(供客户端使用),还需要扩展一些类,客户端在调用接口方法时需要调用哪些方法。
class Adapter extends NeedClass implements PublicInterface{}
但是如果我们没有接口,但只有 2 个类怎么办?例如,我们有一些类(不是接口!),其方法使用客户端。现在我们需要通过创建适配器类来调用其他类的方法,但我们不能这样做,因为我们不能对适配器类进行多重继承。
class Adapter extends NeedClass, PublicInterface
上面的代码不起作用。 这种情况下我们能做什么呢?
How I understand, the Goal of the Adapter pattern is to call some class methods using some interface (which opened to clients). To make adapter pattern we need to implement some interface (which uses by client), and also we need to extend some class, which methods client need to call when calling interface methods.
class Adapter extends NeedClass implements PublicInterface{}
But what if we haven't interface, but have only 2 classes? For example we have some class(not interface!) which methods uses clients. Now we need to call methods of other class by making adapter class, but we cant to do this, because we cant make multiple Inheritance on the adapter class.
class Adapter extends NeedClass, PublicInterface
above code doesnt work.
What we can do in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在
Adapter
中拥有一个NeedClass
实例,并在需要时调用它。因此,您只能从PublicInterface
进行扩展。You can has an instance of
NeedClass
inAdapter
and call it, when you need. So you extend only fromPublicInterface
.您可以使用组合而不是继承。向 NeedClass 类型的 Adapter 类添加一个字段:
然后在 Adapter 方法内部将执行委托给 needClass 字段。
You can use a composition instead of inheritance. Add a field to Adapter class of type NeedClass:
Then inside Adapter methods delegate execution to needClass field.
根据我对适配器模式的理解。
它在处理第三方代码(例如 API)时很有帮助,API 随时可能发生更改,如果直接实现,我可能会破坏您的代码。
例如:在您的网站中使用 Paypal 进行在线支付。假设 Paypal 使用 payMoney() 方法进行支付。一段时间后,他们决定将该方法更改为其他方法,例如 sendMoney()。如果直接实现,这可能会破坏您的代码,使用适配器设计模式可以解决这个问题,如下
第三部分代码 =>因此
,
直接在代码中实现如下所示将破坏
使用适配器的代码,这将节省大量时间,并在每个已实现 API 脚本的地方将代码从 payMoney() 更新为 sendMoney() 的复杂工作。适配器在一处启用更新,仅此而已。
让我们看看吧。
;
就像这样,您可以直接在代码中使用 PaypalAdater,如下所示
因此,将来当供应商(Paypal)决定使用 sendMoney 而不是 payMoney 时,要做的就是打开 PaypalAdapter 类并在 pay($amount) 方法中执行以下操作:
在一个地方进行这一微小更改后,一切正常和以前一样。
From what i have understood the Adapter Pattern.
it is helpful when dealing with the third part codes such as API which is/ are subject to changes any time and my likely to break your code if implemented direct.
For example : Using Paypal in your site for payment online.let's assume the Paypal uses the method payMoney() for payment. and after sometime they decide to change the method to something else let's say sendMoney(). This is likely to break your code if implemented directly, with the use of Adapter Design pattern this can be solves as follow
the third part code => Paypal
}
so implement it directly in the code as below will break the code
using adapter will save numbers of hours and a complex work of updating the code from payMoney() to sendMoney() in every where that the API scripts has been implemented. Adapter enable update in one place and that's it.
Let see it.
}
so it is like that and there you can go and use the PaypalAdater directly into the code as follow;
So in future when the Vendor(Paypal) decide to use sendMoney instead of payMoney what to be done is to open the PaypalAdapter class and do the following in the pay($amount) method:
After this minor change in one place, everything works well as before.