Skip to content

Commit

Permalink
Adapter sample
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielfs7 committed Aug 4, 2014
1 parent 931bc61 commit c13587e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7 deletions.
7 changes: 6 additions & 1 deletion public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
background: #EAEAEA;
padding: 20px;
background: #000;
font-size: 40px;
font-size: 15px;
}
</style>
</head>
Expand Down Expand Up @@ -103,7 +103,12 @@ function handleDir($path)
}

if (function_exists($function)) {
ob_start();
$function();

$content = ob_get_clean();

echo nl2br($content);
} else {
echo 'Exemplo não implementado ainda... :( <br/><br/>';
echo 'Implemente a função <em>"' . $function . '()"</em> em:<br/><br/><em>"' . $sampleFile . '"</em>';
Expand Down
10 changes: 9 additions & 1 deletion src/GSoares/DesignPatterns/Structural/Adapter/README.md
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.
56 changes: 56 additions & 0 deletions src/GSoares/DesignPatterns/Structural/Adapter/Sample.php
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();
}
5 changes: 0 additions & 5 deletions src/GSoares/DesignPatterns/Structural/Bridge/Sample.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
<?php
namespace GSoares\DesignPatterns\Structural\Bridge;

abstract class sdasdsadasdsdsa
{

}

function runBridge()
{
echo "Bridge Sample...";
Expand Down

0 comments on commit c13587e

Please sign in to comment.