Adapter muster

Allikas: Teadmusbaas

Tarkvaraarendues, adapteri struktuuris on tarkvara disain muster (tuntud ka kui Wrapper alternatiivne nimede jagatud Decorator muster), mis võimaldab pesaga olemasoleva klassi, mida kasutatakse teise pesaga. Sageli kasutatakse olemasolevaid klasse töötada koos teistega muutmata selle lähtekoodi.

Näiteks on adapter, mis muudab liidest Document Object Model XML dokumendi puu struktuuri, mida saab kuvada.

Definitsioon

Adapter aitab kahte vastuolus liidesed koos töötama. See on reaalses maailmas määratlus adapteriga. Liidesed võib olla vastuolus, kuid sisemine funktsionaalsus peaks sobima vajadus. Adapter disain muster lubab muidu vastuolus klasse koostööd tegema, muutes liides ühe klassi liides oodatakse kliente.

Näited

PHP

<source lang=PHP> // Adapter Pattern example

interface IFormatIPhone {

   public function recharge();
   public function useLightning();

}

interface IFormatAndroid {

   public function recharge();
   public function useMicroUsb();

}

// Adaptee class IPhone implements IFormatIPhone {

   private $connectorOk = FALSE;
   public function useLightning()
   {
       $this->connectorOk = TRUE;
       echo "Lightning connected -$\n";
   }
   public function recharge()
   {
       if($this->connectorOk)
       {
           echo "Recharge Started\n";
           echo "Recharge 20%\n";
           echo "Recharge 50%\n";
           echo "Recharge 70%\n";
           echo "Recharge Finished\n";
       }
       else
       {
           echo "Connect Lightning first\n";
       }
   }

}

// Adapter class IPhoneAdapter implements IFormatAndroid {

   private $mobile;
   public function __construct(IFormatIPhone $mobile)
   {
       $this->mobile = $mobile;
   }
   public function recharge()
   {
       $this->mobile->recharge();
   }
   public function useMicroUsb()
   {
       echo "MicroUsb connected -> ";
       $this->mobile->useLightning();
   }

}

class Android implements IFormatAndroid {

   private $connectorOk = FALSE;
   public function useMicroUsb()
   {
       $this->connectorOk = TRUE;
       echo "MicroUsb connected ->\n";
   }
   public function recharge()
   {
       if($this->connectorOk)
       {
           echo "Recharge Started\n";
           echo "Recharge 20%\n";
           echo "Recharge 50%\n";
           echo "Recharge 70%\n";
           echo "Recharge Finished\n";
       }
       else
       {
           echo "Connect MicroUsb first\n";
       }
   }

}

// client class MicroUsbRecharger {

   private $phone;
   private $phoneAdapter;
   public function __construct()
   {
       echo "---Recharging iPhone with Generic Recharger---\n";
       $this->phone = new IPhone();
       $this->phoneAdapter = new IPhoneAdapter($this->phone);
       $this->phoneAdapter->useMicroUsb();
       $this->phoneAdapter->recharge();
       echo "---iPhone Ready for use---\n\n";
   }

}

$microUsbRecharger = new MicroUsbRecharger();

class IPhoneRecharger {

   private $phone;
   public function __construct()
   {
       echo "---Recharging iPhone with iPhone Recharger---\n";
       $this->phone = new IPhone();
       $this->phone->useLightning();
       $this->phone->recharge();
       echo "---iPhone Ready for use---\n\n";
   }

}

$iPhoneRecharger = new IPhoneRecharger();

class AndroidRecharger {

   public function __construct()
   {
       echo "---Recharging Android Phone with Generic Recharger---\n";
       $this->phone = new Android();
       $this->phone->useMicroUsb();
       $this->phone->recharge();
       echo "---Phone Ready for use---\n\n";
   }

}

$androidRecharger = new AndroidRecharger();

// Result: #quanton81

//---Recharging iPhone with Generic Recharger--- //MicroUsb connected -> Lightning connected -$ //Recharge Started //Recharge 20% //Recharge 50% //Recharge 70% //Recharge Finished //---iPhone Ready for use--- // //---Recharging iPhone with iPhone Recharger--- //Lightning connected -$ //Recharge Started //Recharge 20% //Recharge 50% //Recharge 70% //Recharge Finished //---iPhone Ready for use--- // //---Recharging Android Phone with Generic Recharger--- //MicroUsb connected -> //Recharge Started //Recharge 20% //Recharge 50% //Recharge 70% //Recharge Finished //---Phone Ready for use--- </source>

Java

<source lang=Java> public class AdapteeToClientAdapter implements Adapter {

   private final Adaptee instance;
   public AdapteeToClientAdapter(final Adaptee instance) {
        this.instance = instance;
   }
   @Override
   public void clientMethod() {
      // call Adaptee's method(s) to implement Client's clientMethod
   }

} </source>

Villem Ausmees (arutelu) 7. märts 2017, kell 01:52 (EET)