Created
December 26, 2012 04:02
-
-
Save mia-0032/4377742 to your computer and use it in GitHub Desktop.
Adapterパターン
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
<?php | |
//既存のクラス | |
class Banner { | |
private $string; | |
public function __construct($text) { | |
if (is_string($text)) { | |
$this->string = $text; | |
} else { | |
die; | |
} | |
} | |
public function showWithParen() { | |
echo '(' . $this->string . ')'; | |
} | |
public function showWithAster() { | |
echo '*' . $this->string . '*'; | |
} | |
} | |
//必要とされているインターフェース | |
interface MyPrint { | |
public function printWeak(); | |
public function printStrong(); | |
} | |
//変換クラス | |
class PrintBanner extends Banner implements MyPrint { | |
public function __construct($text) { | |
parent::__construct($text); | |
} | |
public function printWeak() { | |
parent::showWithParen(); | |
} | |
public function printStrong() { | |
parent::showWithAster(); | |
} | |
} | |
//main | |
function writeWithPrint(MyPrint $print) { | |
$print->printStrong(); | |
$print->printWeak(); | |
} | |
$printer = new PrintBanner('hello world'); | |
writeWithPrint($printer); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment