六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 685|回复: 0

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

[复制链接]
 楼主| 发表于 2013-8-1 15:39:36 | 显示全部楼层 |阅读模式
  1. <?php
  2. /**
  3. * 享元模式
  4. *
  5. * 运用享元技术有效的支持大量细粒度的对象
  6. */
  7. class CD
  8. {
  9. private$_title=null;
  10. private$_artist=null;

  11. publicfunction setTitle($title)
  12.     {
  13. $this->_title =$title;
  14.     }

  15. publicfunction getTitle()
  16.     {
  17. return$this->_title;
  18.     }

  19. publicfunction setArtist($artist)
  20.     {
  21. $this->_artist =$artist;
  22.     }

  23. publicfunction getArtist($artist)
  24.     {
  25. return$this->_artist;
  26.     }
  27. }

  28. class Artist
  29. {
  30. private$_name;

  31. publicfunction __construct($name)
  32.     {
  33. echo"construct ".$name."
  34. ";
  35. $this->_name =$name;
  36.     }

  37. publicfunction getName()
  38.     {
  39. return$this->_name;
  40.     }
  41. }

  42. class ArtistFactory
  43. {
  44. private$_artists=array();

  45. publicfunction getArtist($name)
  46.     {
  47. if(isset($this->_artists[$name]))
  48.         {
  49. return$this->_artists[$name];
  50.         } else {
  51. $objArtist=new Artist($name);
  52. $this->_artists[$name] =$objArtist;
  53. return$objArtist;
  54.         }
  55.     }
  56. }

  57. $objArtistFactory=new ArtistFactory();

  58. $objCD1=new CD();
  59. $objCD1->setTitle("title1");
  60. $objCD1->setArtist($objArtistFactory->getArtist('artist1'));

  61. $objCD2=new CD();
  62. $objCD2->setTitle("title2");
  63. $objCD2->setArtist($objArtistFactory->getArtist('artist2'));

  64. $objCD3=new CD();
  65. $objCD3->setTitle("title3");
  66. $objCD3->setArtist($objArtistFactory->getArtist('artist1'));
复制代码
代码包下载:
(传统的23种模式(没有区分简单工厂与抽象工厂)
http://it.agoit.com/thread-419151-1-1.html  php设计模式 Interpreter(解释器模式)
http://it.agoit.com/thread-419152-1-1.html  php设计模式 Factory(工厂模式)
http://it.agoit.com/thread-419153-1-1.html  php设计模式 Facade(外观模式)
http://it.agoit.com/thread-419154-1-1.html  php设计模式 Decorator(装饰模式)
http://it.agoit.com/thread-419155-1-1.html  php设计模式 Builder(建造者模式)
http://it.agoit.com/thread-419156-1-1.html  php设计模式 Adapter(适配器模式)
http://it.agoit.com/thread-419157-1-1.html  php设计模式 Template (模板模式)
http://it.agoit.com/thread-419158-1-1.html  php设计模式 Command(命令模式)
http://it.agoit.com/thread-419159-1-1.html  php设计模式 Singleton(单例模式)
http://it.agoit.com/thread-419160-1-1.html  php设计模式 Observer(观察者模式)
http://it.agoit.com/thread-419161-1-1.html  php设计模式 Strategy(策略模式)
http://it.agoit.com/thread-419162-1-1.html  php设计模式 Visitor (访问者模式)
http://it.agoit.com/thread-419163-1-1.html  php设计模式 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(委托模式)




该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表