English 中文(简体)
采用“Adapter”模式
原标题:Using "Adapter" pattern

我理解的是,适应模式的目标是使用某些接口(向客户开放)采用某些类别方法。 为了调整适应模式,我们需要实施一些接口(用户使用),我们也需要扩大某些类别,即用户在使用接口方法时需要采用哪一种方法。

class Adapter extends NeedClass implements PublicInterface{}

但是,如果我们有线搭桥,但只有两门? 例如,我们有几类(没有接口!)方法使用客户。 现在,我们需要通过调整适应人员类别来寻找其他类别的方法,但我们必须这样做,因为我们不能对适应者类别造成多重的疏忽。

class Adapter extends NeedClass, PublicInterface

above code doesnt work. What we can do in this case?

最佳回答

如有必要,可在<条码>上查阅,并注明。 因此,你只从<条码>公开传承<>。

public class Adapter extends PublicInterface {

    private NeedClass needClass;

    @Override
    public void doSomething() {
        needClass.doSomethingElse("someParameter");
    }
}
问题回答

你可以使用一种组成而不是继承。 添加一门适应器类需要器:

public class Adapter extends PublicInterface {
    private NeedClass needClass;
}

然后,在校方方法中,将执行权下放到需要的地方。

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

第三编:

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

iii

如下文所述,《刑法》将直接执行《刑法》

$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;
     iii
     // 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);
iii

iii

因此,你可以走到并直接把佩帕尔·阿达特带进法典。

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

因此,今后当供应商(Paypal)决定使用汇款而不是支付金钱时,必须做的是开设PaypalAdapter阶层,并在薪金(美元)方法中做以下工作:

     // 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);
iii   

在一个地方发生这种微小变化之后,所有东西都很好地发挥作用。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签