@blog.justoneplanet.info

日々勉強

Builder Pattern

■コード

News.php

単一のnewsのデータ構造を示す。

class News {
    private $title;
    private $url;
    private $targetDate;
    public function __construct($title, $url, $targetDate) {
        $this->title = $title;
        $this->url   = $title;
        $this->targetDate = $targetDate;
    }
}

NewsDirector.php

class NewsDirector {
    private $builder;
    private $url;
    public function __construct(NewsBuilder $builder, $url) {
        $this->builder = $builder;
        $this->url = $url;
    }
    public function getNews() {
        return $this->builder->parse($this->url);
    }
}

NewsBuilder.php

interface NewsBuilder {
    public function parse($date);
}

RssNewsBuilder.php

class RssNewsBuilder implements NewsBuilder {
    public function parse($url) {
        // RSSを取得する => Newsオブジェクトのリストを返す
    }
}

■クライアントコード

$builder = new RssNewsBuilder();
$director = new NewsDirector($builder, $url);
$news = $director->getNews();
var_dump($news);

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

No comments yet.

RSS feed for comments on this post.TrackBack URL

Leave a comment