当前位置:首页 > PHP教程 > php面向对象 > 列表

PHP设计模式之简单投诉页面实例

发布:smiling 来源: PHP粉丝网  添加日期:2021-07-11 12:56:02 浏览: 评论:0 

这篇文章主要为大家详细介绍了PHP设计模式下简单投诉页面实例,感兴趣的小伙伴们可以参考一下,本文实例介绍了PHP简单投诉页面的实现代码,分享给大家供大家参考,具体内容如下

php代码:

  1. <?php 
  2.  
  3. /* 
  4.  * 设计模式练习 
  5.  * 1.数据库连接类(单例模式) 
  6.  * 2.调用接口实现留言本功能(工厂模式) 
  7.  * 3.实现分级举报处理功能(责任链模式) 
  8.  * 4.发送不同组合的举报信息(桥接模式) 
  9.  * 5.发送不同格式的举报信息(适配器模式) 
  10.  * 6.在投诉内容后自动追加时间(装饰器模式) 
  11.  * 7.根据会员登录信息变换显示风格(观察者模式) 
  12.  * 8.根据发帖长度加经验值(策略模式) 
  13.  */ 
  14.  
  15. interface DB { 
  16.  
  17.   function conn(); 
  18. } 
  19.  
  20. /** 
  21.  * 单例模式 
  22.  */ 
  23. class MysqlSingle implements DB { 
  24.  
  25.   protected static $_instance = NULL; 
  26.  
  27.   public static function getInstance() { 
  28.     if (!self::$_instance instanceof self) { 
  29.       self::$_instance = new self; 
  30.     } 
  31.     return self::$_instance; 
  32.   } 
  33.  
  34.   final protected function __construct() { 
  35.     echo 'Mysql单例创建成功<br>'; 
  36.   } 
  37.  
  38.   final protected function __clone() { 
  39.     return false; 
  40.   } 
  41.  
  42.   public function conn() { 
  43.     echo 'Mysql连接成功<br>'; 
  44.   } 
  45.  
  46. } 
  47.  
  48. /** 
  49.  * 工厂模式 
  50.  */ 
  51. interface Factory { 
  52.  
  53.   function createDB(); 
  54. } 
  55.  
  56. class MysqlFactory implements Factory { 
  57.  
  58.   public function createDB() { 
  59.     echo 'Mysql工厂创建成功<br>'; 
  60.     return MysqlSingle::getInstance(); 
  61.   } 
  62.  
  63. } 
  64.  
  65. /** 
  66.  * 根据用户名显示不同风格 
  67.  * 观察者模式 
  68.  */ 
  69. class Observer implements SplSubject { 
  70.  
  71.   protected $_observers = NULL; 
  72.   public $_style = NULL; 
  73.  
  74.   public function __construct($style) { 
  75.     $this->_style = $style; 
  76.     $this->_observers = new SplObjectStorage(); 
  77.   } 
  78.  
  79.   public function show() { 
  80.     $this->notify(); 
  81.   } 
  82.  
  83.   public function attach(SplObserver $observer) { 
  84.     $this->_observers->attach($observer); 
  85.   } 
  86.  
  87.   public function detach(SplObserver $observer) { 
  88.     $this->_observers->detach($observer); 
  89.   } 
  90.  
  91.   public function notify() { 
  92.     $this->_observers->rewind(); 
  93.     while ($this->_observers->valid()) { 
  94.       $observer = $this->_observers->current(); 
  95.       $observer->update($this); 
  96.       $this->_observers->next(); 
  97.     } 
  98.   } 
  99.  
  100. } 
  101.  
  102. class StyleA implements SplObserver { 
  103.  
  104.   public function update(SplSubject $subject) { 
  105.     echo $subject->_style . ' 模块A<br>'; 
  106.   } 
  107.  
  108. } 
  109.  
  110. class StyleB implements SplObserver { 
  111.  
  112.   public function update(SplSubject $subject) { 
  113.     echo $subject->_style . ' 模块B<br>'; 
  114.   } 
  115.  
  116. } 
  117.  
  118. /** 
  119.  * 根据不同方式进行投诉 
  120.  * 桥接模式 
  121.  */ 
  122. class Bridge { 
  123.  
  124.   protected $_obj = NULL; 
  125.  
  126.   public function __construct($obj) { 
  127.     $this->_obj = $obj; 
  128.   } 
  129.  
  130.   public function msg($type) { 
  131.       
  132.   } 
  133.  
  134.   public function show() { 
  135.     $this->msg(); 
  136.     $this->_obj->msg(); 
  137.   } 
  138.  
  139. } 
  140.  
  141. class BridgeEmail extends Bridge { 
  142.  
  143.   public function msg() { 
  144.     echo 'Email>>'; 
  145.   } 
  146.  
  147. } 
  148.  
  149. class BridgeSms extends Bridge { 
  150.  
  151.   public function msg() { 
  152.     echo 'Sms>>'; 
  153.   } 
  154.  
  155. } 
  156.  
  157. class Normal { 
  158.  
  159.   public function msg() { 
  160.     echo 'Normal<br>'; 
  161.   } 
  162.  
  163. } 
  164.  
  165. class Danger { 
  166.  
  167.   public function msg() { 
  168.     echo 'Danger<br>'; 
  169.   } 
  170.  
  171. } 
  172.  
  173. /** 
  174.  * 适配器模式 
  175.  */ 
  176. class Serialize { 
  177.  
  178.   public $content = NULL; 
  179.  
  180.   public function __construct($content) { 
  181.     $this->content = serialize($content); 
  182.   } 
  183.  
  184.   public function show() { 
  185.     return '序列化格式:<br>' . $this->content; 
  186.   } 
  187.  
  188. } 
  189.  
  190. class JsonAdapter extends Serialize { 
  191.  
  192.   public function __construct($content) { 
  193.     parent::__construct($content); 
  194.     $tmp = unserialize($this->content); 
  195.     $this->content = json_encode($tmp, TRUE); 
  196.   } 
  197.  
  198.   public function show() { 
  199.     return 'Json格式:<br>' . $this->content; 
  200.   } 
  201.  
  202. } 
  203.  
  204. /** 
  205.  * 在投诉内容后自动追加 
  206.  * 装饰器模式 
  207.  */ 
  208. class Base { 
  209.  
  210.   protected $_content = NULL; 
  211.  
  212.   public function __construct($content) { 
  213.     $this->_content = $content; 
  214.   } 
  215.  
  216.   public function getContent() { 
  217.     return $this->_content; 
  218.   } 
  219.  
  220. } 
  221.  
  222. class Decorator { 
  223.  
  224.   private $_base = NULL; 
  225.  
  226.   public function __construct(Base $base) { 
  227.     $this->_base = $base; 
  228.   } 
  229.  
  230.   public function show() { 
  231.     return $this->_base->getContent() . '>>系统时间:' . date('Y-m-d H:i:s', time()); 
  232.   } 
  233.  
  234. } 
  235.  
  236. /** 
  237.  * 分级举报处理功能 
  238.  * 责任链模式 
  239.  */ 
  240. class level1 { 
  241.  
  242.   protected $_level = 1; 
  243.   protected $_top = 'Level2'; 
  244.  
  245.   public function deal($level) { 
  246.     if ($level <= $this->_level) { 
  247.       echo '处理级别:1<br>'; 
  248.       return; 
  249.     } 
  250.     $top = new $this->_top; 
  251.     $top->deal($level); 
  252.   } 
  253.  
  254. } 
  255.  
  256. class level2 { 
  257.  
  258.   protected $_level = 2; 
  259.   protected $_top = 'Level3'; 
  260.  
  261.   public function deal($level) { 
  262.     if ($level <= $this->_level) { 
  263.       echo '处理级别:2<br>'; 
  264.       return; 
  265.     } 
  266.     $top = new $this->_top; 
  267.     $top->deal($level); 
  268.   } 
  269.  
  270. } 
  271.  
  272. class level3 { 
  273.  
  274.   protected $_level = 3; 
  275.   protected $_top = 'Level2'; 
  276.  
  277.   public function deal($level) { 
  278.     echo '处理级别:3<br>'; 
  279.     return; 
  280.   } 
  281.  
  282. } 
  283.  
  284. if (!emptyempty($_POST)) { 
  285.   echo '<h1>PHP设计模式</h1>'; 
  286.   //连接数据库——工厂+单例模式 
  287.   $mysqlFactory = new MysqlFactory(); 
  288.   $single = $mysqlFactory->createDB(); 
  289.   $single->conn(); 
  290.   echo '<br>'; 
  291.   //观察者模式 
  292.   $username = $_POST['username']; 
  293.   $ob = new Observer($username); 
  294.   $a = new StyleA(); 
  295.   $ob->attach($a); 
  296.   $b = new StyleB(); 
  297.   $ob->attach($b); 
  298.   $ob->show(); 
  299.   echo '<br>'; 
  300.   $ob->detach($b); 
  301.   $ob->show(); 
  302.   echo '<br>'; 
  303.   //桥接模式 
  304.   $typeM = $_POST['typeM']; 
  305.   $typeN = 'Bridge' . $_POST['typeN']; 
  306.   $obj = new $typeN(new $typeM); 
  307.   $obj->show(); 
  308.   echo '<br>'; 
  309.   //适配器模式 
  310.   $post = $_POST; 
  311.   $obj = new Serialize($post); 
  312.   echo $obj->show(); 
  313.   echo '<br>'; 
  314.   $json = new JsonAdapter($post); 
  315.   echo $json->show(); 
  316.   echo '<br>'; 
  317.   echo '<br>'; 
  318.   //装饰器模式 
  319.   $content = $_POST['content']; 
  320.   $decorator = new Decorator(new Base($content)); 
  321.   echo $decorator->show(); 
  322.   echo '<br>'; 
  323.   //责任链模式 
  324.   echo '<br>'; 
  325.   $level = $_POST['level']; 
  326.   $deal = new Level1(); 
  327.   $deal->deal(intval($level)); 
  328.   return; 
  329. } 
  330. require("0.html"); 

html代码:

  1. <!DOCTYPE html> 
  2. <!-- 
  3. To change this license header, choose License Headers in Project Properties. 
  4. To change this template file, choose Tools | Templates 
  5. and open the template in the editor. 
  6. --> 
  7. <html> 
  8.   <head> 
  9.     <title>PHP设计模式</title> 
  10.     <meta charset="UTF-8"> 
  11.     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
  12.     <style> 
  13.       div{border:solid gray 1px;margin-top:10px;height: 100px;width: 200px;} 
  14.     </style> 
  15.   </head> 
  16.   <body> 
  17.     <form action="0.php" method="post"> 
  18.       <h1>用户名</h1> 
  19.       <select id="username" name="username"> 
  20.         <option value="Tom">Tom</option> 
  21.         <option value="Lily">Lily</option> 
  22.       </select> 
  23.       <h1>投诉方式</h1> 
  24.       <select id="type" name="typeM"> 
  25.         <option value="Normal">Normal</option> 
  26.         <option value="Danger">Danger</option> 
  27.       </select> 
  28.       <select id="type" name="typeN"> 
  29.         <option value="Email">Email</option> 
  30.         <option value="Sms">Sms</option> 
  31.       </select> 
  32.       <h1>处理级别</h1> 
  33.       <select id="level" name="level"> 
  34.         <option value="1">1</option> 
  35.         <option value="2">2</option> 
  36.         <option value="3">3</option> 
  37.       </select> 
  38.       <h1>投诉内容</h1> 
  39.       <textarea id="content" name="content" rows="3"></textarea> 
  40.       <button type="submit">提交</button> 
  41.     </form> 
  42.   </body> 
  43. </html> 

以上就是本文的全部内容,希望对大家的学习有所帮助。

Tags: PHP设计模式 PHP投诉页面

分享到: