六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 806|回复: 0

php设计模式 Visitor (访问者模式)

[复制链接]
 楼主| 发表于 2013-8-1 15:25:08 | 显示全部楼层 |阅读模式
  1. <?php
  2. /**
  3. * 访问者模式
  4. *
  5. * 表示一个作用于某对象结构中的各元素的操作,可以在不改变各元素的类的前提下定义作用于这些元素的新操作
  6. *
  7. */
  8. abstractclass Visitor
  9. {
  10. abstractpublicfunction visitCroncreteElementA($element);
  11. abstractpublicfunction visitCroncreteElementB($element);
  12. }

  13. class ConcreteVisitor1 extends Visitor
  14. {
  15. publicfunction visitCroncreteElementA($element)
  16.     {
  17. echoget_class($element)." visit 1A
  18. ";
  19.     }

  20. publicfunction visitCroncreteElementB($element)
  21.     {
  22. echoget_class($element)." visit 1B
  23. ";
  24.     }
  25. }

  26. class ConcreteVisitor2 extends Visitor
  27. {
  28. publicfunction visitCroncreteElementA($element)
  29.     {
  30. echoget_class($element)." visit 2A
  31. ";
  32.     }

  33. publicfunction visitCroncreteElementB($element)
  34.     {
  35. echoget_class($element)." visit 2B
  36. ";
  37.     }
  38. }

  39. abstractclass Element
  40. {
  41. abstractpublicfunction accept($visitor);
  42. }

  43. class ConcreteElementA extends Element
  44. {
  45. publicfunction accept($visitor)
  46.     {
  47. $visitor->visitCroncreteElementA($this);
  48.     }
  49. }

  50. class ConcreteElementB extends Element
  51. {
  52. publicfunction accept($visitor)
  53.     {
  54. $visitor->visitCroncreteElementB($this);
  55.     }
  56. }

  57. class ObjectStructure
  58. {
  59. private$_elements=array();

  60. publicfunction attach($element)
  61.     {
  62. $this->_elements[] =$element;
  63.     }

  64. publicfunction detach($element)
  65.     {
  66. if($key=array_search($element,$this->_elements) !==false) unset($this->_elements[$key]);
  67.     }

  68. publicfunction accept($visitor)
  69.     {
  70. foreach($this->_elements as$element)
  71.         {
  72. $element->accept($visitor);
  73.         }
  74.     }
  75. }

  76. //
  77. $objOS=new ObjectStructure();
  78. $objOS->attach(new ConcreteElementA());
  79. $objOS->attach(new ConcreteElementB());

  80. $objCV1=new ConcreteVisitor1();
  81. $objCV2=new ConcreteVisitor2();

  82. $objOS->accept($objCV1);
  83. $objOS->accept($objCV2);
复制代码
本文摘 自:http://www.cnblogs.com/bluefrog/archive/2011/06/27/2091681.html

代码包下载:
(传统的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(委托模式)




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

本版积分规则

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