From c13587e94ac6f36322438e7d0039e5e510735991 Mon Sep 17 00:00:00 2001 From: Gabriel Felipe Soares Date: Sun, 3 Aug 2014 21:51:54 -0300 Subject: [PATCH] Adapter sample --- public/index.php | 7 ++- .../Structural/Adapter/README.md | 10 +++- .../Structural/Adapter/Sample.php | 56 +++++++++++++++++++ .../Structural/Bridge/Sample.php | 5 -- 4 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 src/GSoares/DesignPatterns/Structural/Adapter/Sample.php diff --git a/public/index.php b/public/index.php index 4baeca1..e9b4b27 100644 --- a/public/index.php +++ b/public/index.php @@ -53,7 +53,7 @@ background: #EAEAEA; padding: 20px; background: #000; - font-size: 40px; + font-size: 15px; } @@ -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... :(

'; echo 'Implemente a função "' . $function . '()" em:

"' . $sampleFile . '"'; diff --git a/src/GSoares/DesignPatterns/Structural/Adapter/README.md b/src/GSoares/DesignPatterns/Structural/Adapter/README.md index 2dfefb0..18edb35 100644 --- a/src/GSoares/DesignPatterns/Structural/Adapter/README.md +++ b/src/GSoares/DesignPatterns/Structural/Adapter/README.md @@ -1 +1,9 @@ -#Adapter - Design Patterns# \ No newline at end of file +#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. \ No newline at end of file diff --git a/src/GSoares/DesignPatterns/Structural/Adapter/Sample.php b/src/GSoares/DesignPatterns/Structural/Adapter/Sample.php new file mode 100644 index 0000000..79b5235 --- /dev/null +++ b/src/GSoares/DesignPatterns/Structural/Adapter/Sample.php @@ -0,0 +1,56 @@ +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(); +} \ No newline at end of file diff --git a/src/GSoares/DesignPatterns/Structural/Bridge/Sample.php b/src/GSoares/DesignPatterns/Structural/Bridge/Sample.php index 8d2e29e..14a3ff3 100644 --- a/src/GSoares/DesignPatterns/Structural/Bridge/Sample.php +++ b/src/GSoares/DesignPatterns/Structural/Bridge/Sample.php @@ -1,11 +1,6 @@