コード例 #1
0
ファイル: Flyweight.php プロジェクト: rick00young/StudyNote
        $this->_name = $name;
    }
    public function getName()
    {
        return $this->_name;
    }
}
class ArtistFactory
{
    private $_artists = array();
    public function getArtist($name)
    {
        if (isset($this->_artists[$name])) {
            return $this->_artists[$name];
        } else {
            $this->_artists[$name] = new Artist($name);
            return $this->_artists[$name];
        }
    }
}
$objArtistFactory = new ArtistFactory();
$objCD1 = new CD();
$objCD1->setTitle("title1");
$objCD1->setArtist($objArtistFactory->getArtist("artist1"));
$objCD2 = new CD();
$objCD2->setTitle("title2");
$objCD2->setArtist($objArtistFactory->getArtist("artist2"));
$objCD3 = new CD();
$objCD3->setTitle("title3");
$objCD3->setArtist($objArtistFactory->getArtist("artist1"));
コード例 #2
0
    }
    public function getName()
    {
        return $this->_name;
    }
}
class ArtistFactory
{
    private $_artists = array();
    public function getArtist($name)
    {
        if (isset($this->_artists[$name])) {
            return $this->_artists[$name];
        } else {
            $objArtist = new Artist($name);
            $this->_artists[$name] = $objArtist;
            return $objArtist;
        }
    }
}
$objArtistFactory = new ArtistFactory();
$objCD1 = new CD();
$objCD1->setTitle("title1");
$objCD1->setArtist($objArtistFactory->getArtist('artist1'));
$objCD2 = new CD();
$objCD2->setTitle("title2");
$objCD2->setArtist($objArtistFactory->getArtist('artist2'));
$objCD3 = new CD();
$objCD3->setTitle("title3");
$objCD3->setArtist($objArtistFactory->getArtist('artist1'));