Created
June 11, 2013 01:35
-
-
Save sugarHoge/5753950 to your computer and use it in GitHub Desktop.
Templatemethod
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 | |
/** | |
* AbstractClassクラスに相当する | |
*/ | |
abstract class AbstractDisplay { | |
/** | |
* 表示するデータ | |
*/ | |
private $data; | |
/** | |
* コンストラクタ | |
* @param mixed 表示するデータ | |
*/ | |
public function __construct($data) { | |
if (!is_array($data)) { | |
$data = array($data); | |
} | |
$this->data = $data; | |
} | |
/** | |
* template methodに相当する | |
*/ | |
public function display() { | |
$this->displayHeader(); | |
$this->displayBody(); | |
$this->displayFooter(); | |
} | |
/** | |
* データを取得する | |
*/ | |
public function getData() { | |
return $this->data; | |
} | |
/** | |
* ヘッダを表示する | |
* サブクラスに実装を任せる抽象メソッド | |
*/ | |
protected abstract function displayHeader(); | |
/** | |
* ボディ(クライアントから渡された内容)を表示する | |
* サブクラスに実装を任せる抽象メソッド | |
*/ | |
protected abstract function displayBody(); | |
/** | |
* フッタを表示する | |
* サブクラスに実装を任せる抽象メソッド | |
*/ | |
protected abstract function displayFooter(); | |
} | |
?> |
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 | |
require_once 'AbstractDisplay.class.php'; | |
?> | |
<?php | |
/** | |
* ConcreteClassクラスに相当する | |
*/ | |
class ListDisplay extends AbstractDisplay { | |
/** | |
* ヘッダを表示する | |
*/ | |
protected function displayHeader() { | |
echo '<dl>'; | |
} | |
/** | |
* ボディ(クライアントから渡された内容)を表示する | |
*/ | |
protected function displayBody() { | |
foreach ($this->getData() as $key => $value) { | |
echo '<dt>Item ' . $key . '</dt>'; | |
echo '<dd>' . $value . '</dd>'; | |
} | |
} | |
/** | |
* フッタを表示する | |
*/ | |
protected function displayFooter() { | |
echo '</dl>'; | |
} | |
} | |
?> |
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 | |
require_once 'AbstractDisplay.class.php'; | |
?> | |
<?php | |
/** | |
* ConcreteClassクラスに相当する | |
*/ | |
class TableDisplay extends AbstractDisplay { | |
/** | |
* ヘッダを表示する | |
*/ | |
protected function displayHeader() { | |
echo '<table border="1" cellpadding="2" cellspacing="2">'; | |
} | |
/** | |
* ボディ(クライアントから渡された内容)を表示する | |
*/ | |
protected function displayBody() { | |
foreach ($this->getData() as $key => $value) { | |
echo '<tr>'; | |
echo '<th>' . $key . '</th>'; | |
echo '<td>' . $value . '</td>'; | |
echo '</tr>'; | |
} | |
} | |
/** | |
* フッタを表示する | |
*/ | |
protected function displayFooter() { | |
echo '</table>'; | |
} | |
} | |
?> |
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 | |
require_once '/../models/TemplateMethod/ListDisplay.class.php'; | |
require_once '/../models/TemplateMethod/TableDisplay.class.php'; | |
class TemplatemethodController extends Zend_Controller_Action | |
{ | |
public function init() | |
{ | |
/* Initialize action controller here */ | |
} | |
public function indexAction() | |
{ | |
$data = array('Design Pattern', | |
'Gang of Four', | |
'Template Method Sample1', | |
'Template Method Sample2'); | |
$display1 = new ListDisplay($data); | |
$display2 = new TableDisplay($data); | |
$this->view->display1 = $display1->display(); | |
$this->view->display2 = $display2->display(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment