安哥网络 发表于 2013-8-1 15:39:36

php设计模式 FlyWeight (享元模式)

<?php
/**
* 享元模式
*
* 运用享元技术有效的支持大量细粒度的对象
*/
class CD
{
private$_title=null;
private$_artist=null;

publicfunction setTitle($title)
    {
$this->_title =$title;
    }

publicfunction getTitle()
    {
return$this->_title;
    }

publicfunction setArtist($artist)
    {
$this->_artist =$artist;
    }

publicfunction getArtist($artist)
    {
return$this->_artist;
    }
}

class Artist
{
private$_name;

publicfunction __construct($name)
    {
echo"construct ".$name."
";
$this->_name =$name;
    }

publicfunction getName()
    {
return$this->_name;
    }
}

class ArtistFactory
{
private$_artists=array();

publicfunction 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'));代码包下载:
(传统的23种模式(没有区分简单工厂与抽象工厂)
http://it.agoit.com/thread-419151-1-1.htmlphp设计模式 Interpreter(解释器模式)
http://it.agoit.com/thread-419152-1-1.htmlphp设计模式 Factory(工厂模式)
http://it.agoit.com/thread-419153-1-1.htmlphp设计模式 Facade(外观模式)
http://it.agoit.com/thread-419154-1-1.htmlphp设计模式 Decorator(装饰模式)
http://it.agoit.com/thread-419155-1-1.htmlphp设计模式 Builder(建造者模式)
http://it.agoit.com/thread-419156-1-1.htmlphp设计模式 Adapter(适配器模式)
http://it.agoit.com/thread-419157-1-1.htmlphp设计模式 Template (模板模式)
http://it.agoit.com/thread-419158-1-1.htmlphp设计模式 Command(命令模式)
http://it.agoit.com/thread-419159-1-1.htmlphp设计模式 Singleton(单例模式)
http://it.agoit.com/thread-419160-1-1.htmlphp设计模式 Observer(观察者模式)
http://it.agoit.com/thread-419161-1-1.htmlphp设计模式 Strategy(策略模式)
http://it.agoit.com/thread-419162-1-1.htmlphp设计模式 Visitor (访问者模式)
http://it.agoit.com/thread-419163-1-1.htmlphp设计模式 Memento (备忘录模式)
http://it.agoit.com/thread-419164-1-1.html php设计模式 Prototype (原型模式)
http://it.agoit.com/thread-419165-1-1.html php设计模式 Mediator (中介者模式)
http://it.agoit.com/thread-419166-1-1.html php设计模式 FlyWeight (享元模式)
http://it.agoit.com/thread-419167-1-1.html php设计模式 Chain Of Responsibility (职责链模式)
http://it.agoit.com/thread-419168-1-1.html php设计模式 Bridge (桥接模式)
http://it.agoit.com/thread-419169-1-1.html php设计模式 Proxy (代理模式)
http://it.agoit.com/thread-419170-1-1.html php设计模式 State (状态模式)
http://it.agoit.com/thread-419171-1-1.html php设计模式 Composite (组合模式)
http://it.agoit.com/thread-419172-1-1.html php设计模式 Interator (迭代器模式)
下面来自<<php设计模式>>
http://it.agoit.com/thread-419173-1-1.html php设计模式 DAO(数据访问对象模式)
http://it.agoit.com/thread-419174-1-1.html php设计模式 Delegation(委托模式)




页: [1]
查看完整版本: php设计模式 FlyWeight (享元模式)