-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
931bc61
commit c13587e
Showing
4 changed files
with
71 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,9 @@ | ||
#Adapter - Design Patterns# | ||
#Adapter - Design Patterns# | ||
|
||
O padrão "Adapter" permite que classes que possuem interfaces "incompatíveis" consigam trabalhar juntas. Para isto, | ||
cria-se um "Adaptador" que utiliza dessas classes oferecendo uma nova interface que atenda a nova necessidade. | ||
|
||
Adapter é uma abstração intermediária que traduz ou mapeia um comportamento (classes, métodos ou funções) antigo | ||
do sistema para uma nova interface aceita pela nova arquitetura do sistema. | ||
|
||
Em outras palavras podemos dizer que o padrão Adapter converte uma classe em uma interface que outra classe espera. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
namespace GSoares\DesignPatterns\Structural\Adapter; | ||
|
||
class LegacyPhoneNumber | ||
{ | ||
private $number; | ||
|
||
public function __construct($number) | ||
{ | ||
$this->number = $number; | ||
} | ||
|
||
public function getNumber() | ||
{ | ||
return $this->number; | ||
} | ||
} | ||
|
||
class PhoneNumberAdapter | ||
{ | ||
private $legacyNumber; | ||
|
||
public function __construct(LegacyPhoneNumber $legacyPhoneNumber) | ||
{ | ||
$this->legacyNumber = preg_replace('/[^0-9]/', '', $legacyPhoneNumber->getNumber()); | ||
} | ||
|
||
public function getAreaCode() | ||
{ | ||
return substr($this->legacyNumber, 2, 2); | ||
} | ||
|
||
public function getCountryCode() | ||
{ | ||
return substr($this->legacyNumber, 0, 2); | ||
} | ||
|
||
public function getNumber() | ||
{ | ||
return substr($this->legacyNumber, 4); | ||
} | ||
} | ||
|
||
function runAdapter() | ||
{ | ||
$legacyNumber = new LegacyPhoneNumber('+55 (48) 3222-2222'); | ||
|
||
echo "Set legacy phone number " . $legacyNumber->getNumber() . "\n"; | ||
|
||
$newNumber = new PhoneNumberAdapter($legacyNumber); | ||
|
||
echo "After adapter we can get: \n"; | ||
echo "Country code: " . $newNumber->getCountryCode() . "\n"; | ||
echo "Area code: " . $newNumber->getAreaCode() . "\n"; | ||
echo "Number: " . $newNumber->getNumber(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters