使用“适配器”图案

发布于 2024-12-26 18:49:10 字数 405 浏览 3 评论 0原文

据我了解,适配器模式的目标是使用某些接口(向客户端开放)调用某些类方法。为了实现适配器模式,我们需要实现一些接口(供客户端使用),还需要扩展一些类,客户端在调用接口方法时需要调用哪些方法。

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 技术交流群。

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

发布评论

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

评论(3

回忆追雨的时光 2025-01-02 18:49:10

您可以在 Adapter 中拥有一个 NeedClass 实例,并在需要时调用它。因此,您只能从 PublicInterface 进行扩展。

public class Adapter extends PublicInterface {

    private NeedClass needClass;

    @Override
    public void doSomething() {
        needClass.doSomethingElse("someParameter");
    }
}

You can has an instance of NeedClass in Adapter and call it, when you need. So you extend only from PublicInterface.

public class Adapter extends PublicInterface {

    private NeedClass needClass;

    @Override
    public void doSomething() {
        needClass.doSomethingElse("someParameter");
    }
}
乄_柒ぐ汐 2025-01-02 18:49:10

您可以使用组合而不是继承。向 NeedClass 类型的 Adapter 类添加一个字段:

public class Adapter extends PublicInterface {
    private NeedClass needClass;
}

然后在 Adapter 方法内部将执行委托给 needClass 字段。

You can use a composition instead of inheritance. Add a field to Adapter class of type NeedClass:

public class Adapter extends PublicInterface {
    private NeedClass needClass;
}

Then inside Adapter methods delegate execution to needClass field.

把时间冻结 2025-01-02 18:49:10

根据我对适配器模式的理解。
它在处理第三方代码(例如 API)时很有帮助,API 随时可能发生更改,如果直接实现,我可能会破坏您的代码。
例如:在您的网站中使用 Paypal 进行在线支付。假设 Paypal 使用 payMoney() 方法进行支付。一段时间后,他们决定将该方法更改为其他方法,例如 sendMoney()。如果直接实现,这可能会破坏您的代码,使用适配器设计模式可以解决这个问题,如下

第三部分代码 =>因此

   class Paypal {
     public function __construct(){
          // their codes 
     }
   public function payMoney($amount){
      // the logic of validating
      // the $amount variables and do the payment
   }   

直接在代码中实现如下所示将破坏

$pay = new Paypal();
$pay->payMoney(200);

使用适配器的代码,这将节省大量时间,并在每个已实现 API 脚本的地方将代码从 payMoney() 更新为 sendMoney() 的复杂工作。适配器在一处启用更新,仅此而已。
让我们看看吧。

  class paypalAdapter {

     private $paypal;
     // Paypal object into construct and check if it's pa
     // Paypal object via type hint
     public function __construct(PayPal $paypal) {
        $this->paypal = $paypal;
     }
     // call the Paypal method in your own 
     //custom method that is to be  
     // implemented directly into your code
     public function pay($amount) {
        $this->paypal->payMoney($amount);
}

就像这样,您可以直接在代码中使用 PaypalAdater,如下所示

  $pay = new PaypalAdapter(new Paypal);
  $pay->pay(200);

因此,将来当供应商(Paypal)决定使用 sendMoney 而不是 payMoney 时,要做的就是打开 PaypalAdapter 类并在 pay($amount) 方法中执行以下操作:

     // SEE THIS METHOD ABOVE TO OBSERVE CHANGES
     // FROM $this->paypal->payMoney($amount);
     //  TO $this->paypal->senMoney($amount);
     public function pay($amount) {
        $this->paypal->sendMoney($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

   class Paypal {
     public function __construct(){
          // their codes 
     }
   public function payMoney($amount){
      // the logic of validating
      // the $amount variables and do the payment
   }   

}

so implement it directly in the code as below will break the code

$pay = new Paypal();
$pay->payMoney(200);

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.

  class paypalAdapter {

     private $paypal;
     // Paypal object into construct and check if it's pa
     // Paypal object via type hint
     public function __construct(PayPal $paypal) {
        $this->paypal = $paypal;
     }
     // call the Paypal method in your own 
     //custom method that is to be  
     // implemented directly into your code
     public function pay($amount) {
        $this->paypal->payMoney($amount);
}

}

so it is like that and there you can go and use the PaypalAdater directly into the code as follow;

  $pay = new PaypalAdapter(new Paypal);
  $pay->pay(200);

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:

     // SEE THIS METHOD ABOVE TO OBSERVE CHANGES
     // FROM $this->paypal->payMoney($amount);
     //  TO $this->paypal->senMoney($amount);
     public function pay($amount) {
        $this->paypal->sendMoney($amount);
}   

After this minor change in one place, everything works well as before.

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