A-A+

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

2021年07月11日 我爱编程 暂无评论

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

php代码:

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

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

给我留言

Copyright © 四季博客 保留所有权利.   Theme  Ality

用户登录