Skip to content

Instantly share code, notes, and snippets.

@mia-0032
Created December 26, 2012 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mia-0032/4377742 to your computer and use it in GitHub Desktop.
Save mia-0032/4377742 to your computer and use it in GitHub Desktop.
Adapterパターン
<?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