@blog.justoneplanet.info

日々勉強

Template Methodパターン

abstract class AbstractDisplay {
    private $data;
    public function __construct($data) {
        if(!is_array($data)) {
            $data = array($data);
        }
        $this->data = $data;
    }
    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();
}
class ListDisplay extends AbstractDisplay {
    protected function displayHeader(){
        echo '<dl>';
    }
    protected function displayBody() {
        foreach($this->getData() as $key => $value){
            echo "<dt>{$key}</dt>¥n<dd>{$value}</dd>";
        }
    }
    protected function displayFooter() {
        echo '</dl>'
    }
}
class TableDisplay extends AbstractDisplay {
    protected function displayHeader(){
        echo '<table>';
    }
    protected function displayBody() {
        foreach($this->getData() as $key => $value){
            echo "<tr><th>{$key}</th><td>{$value}</td></tr>";
        }
    }
    protected function displayFooter() {
        echo '</table>'
    }
}

コメントはまだありません»

No comments yet.

RSS feed for comments on this post.TrackBack URL

Leave a comment