PHP design Pattern Simple complaint page instance, PHP design Pattern Example _php Tutorial

Source: Internet
Author: User
Tags learn php

PHP design Pattern Simple complaint page instance, PHP design pattern instance


This article introduces the PHP simple complaint page implementation code, share to everyone for reference, the specific content as follows

PHP Code:

<?php/* * Design mode exercise * 1. Database connection Class (singleton mode) * 2. Call the interface to implement the message function (Factory mode) * 3. Achieve hierarchical report processing function (Chain mode) * 4. Send different combinations of report information (bridging mode) * 5. Send report information in different formats (adapter mode) * 6. Automatically append time after the complaint content (adorner mode) * 7. Change the display style according to the member login information (viewer mode) * 8. Based on the length of the Post plus XP (policy mode) */interface DB {function conn ();}  /** * Singleton mode */class Mysqlsingle implements DB {protected static $_instance = NULL;    public static function getinstance () {if (!self::$_instance instanceof self) {self::$_instance = new self;  } return self::$_instance; } Final protected function __construct () {echo ' MySQL Singleton created successfully
'; } Final protected function __clone () {return false; } public Function Conn () {echo ' MySQL connection succeeded
'; }}/** * Factory mode */interface Factory {function createdb ();} Class Mysqlfactory implements Factory {public Function createdb () {echo ' MySQL factory created successfully
'; return Mysqlsingle::getinstance (); }}/** * Display different styles according to user name * Observer mode */class Observer implements Splsubject {protected $_observers = NULL; Public $_style = NULL; Public function __construct ($style) {$this->_style = $style; $this->_observers = new Splobjectstorage (); Public function Show () {$this->notify (); The Public function attach (Splobserver $observer) {$this->_observers->attach ($observer); The Public function detach (Splobserver $observer) {$this->_observers->detach ($observer); Public Function Notify () {$this->_observers->rewind (); while ($this->_observers->valid ()) {$observer = $this->_observers->current (); $observer->update ($this); $this->_observers->next (); }}}class Stylea implements Splobserver {public Function update (Splsubject $subject) {echo $subject->_style. ' Module A
'; }}class Styleb implements Splobserver {public Function update (Splsubject $subject) {echo $subject->_style. ' Module B
'; }}/** * Complaints by different means * Bridging mode */class Bridge {protected $_obj = NULL; Public function __construct ($obj) {$this->_obj = $obj; Public function msg ($type) {} public Function Show () {$this->msg (); $this->_obj->msg (); }}class Bridgeemail extends Bridge {public Function msg () {echo ' email>> '; }}class Bridgesms extends Bridge {public Function msg () {echo ' sms>> '; }}class Normal {public function msg () {echo ' normal
'; }}class Danger {public Function msg () {echo ' Danger
'; }}/** * Adapter Mode */class Serialize {public $content = NULL; Public function __construct ($content) {$this->content = serialize ($content); Public function Show () {return ' serialization format:
' . $this->content; }}class Jsonadapter extends Serialize {public function __construct ($content) {parent::__construct ($content); $tmp = unserialize ($this->content); $this->content = Json_encode ($tmp, TRUE); Public function Show () {return ' JSON format:
' . $this->content; }}/** * Adorner mode */class Base {protected $_content = NULL after complaint content) Public function __construct ($content) {$this->_content = $content; } public Function GetContent () {return $this->_content; }}class Decorator {Private $_base = NULL; Public function __construct (Base $base) {$this->_base = $base; Public function Show () {return $this->_base->getcontent (). ' >> system time: '. Date (' y-m-d h:i:s ', Time ()); }}/** * Hierarchical report processing function * Responsibility chain mode */class Level1 {protected $_level = 1; protected $_top = ' Level2 '; Public function Deal ($level) {if ($level <= $this->_level) {echo ' Processing level: 1
'; Return } $top = new $this->_top; $top->deal ($level); }}class Level2 {protected $_level = 2; protected $_top = ' Level3 '; Public function Deal ($level) {if ($level <= $this->_level) {echo ' Processing level: 2
'; Return } $top = new $this->_top; $top->deal ($level); }}class level3 {protected $_level = 3; protected $_top = ' Level2 '; Public function Deal ($level) {echo ' Processing level: 3
'; Return }}if (!empty ($_post)) {echo '

PHP design mode

'; Connect Database-factory + Singleton mode $mysqlFactory = new Mysqlfactory (); $single = $mysqlFactory->createdb (); $single->conn (); Echo '
'; Observer pattern $username = $_post[' username '); $ob = new Observer ($username); $a = new Stylea (); $ob->attach ($a); $b = new Styleb (); $ob->attach ($b); $ob->show (); Echo '
'; $ob->detach ($b); $ob->show (); Echo '
'; Bridging mode $typeM = $_post[' Typem '); $typeN = ' Bridge '. $_post[' Typen ']; $obj = new $typeN (new $typeM); $obj->show (); Echo '
'; Adapter mode $post = $_post; $obj = new Serialize ($post); echo $obj->show (); Echo '
'; $json = new Jsonadapter ($post); echo $json->show (); Echo '
'; Echo '
'; Adorner mode $content = $_post[' content '); $decorator = new Decorator (new Base ($content)); echo $decorator->show (); Echo '
'; Responsibility chain mode echo '
'; $level = $_post[' level '); $deal = new Level1 (); $deal->deal (Intval ($level)); return;} Require ("0.html");

HTML code:

 
        PHP design mode    
 
      
 
                

The above is the whole content of this article, I hope that everyone's study has helped.

Articles you may be interested in:

    • Simple factory model of PHP design mode
    • Explanation of "Simple Factory mode" instance code in PHP
    • Example of the Observer pattern for PHP design Patterns
    • The delegate mode of PHP design mode
    • design mode of design for PHP in common use
    • PHP Design Mode Series Specification Specification mode
    • Learn PHP Design Patterns PHP Implementation Memo mode (Memento)
    • Learn PHP Design Patterns PHP Implementation viewer mode (Observer)
    • Learn PHP Design Patterns PHP Implementation Template Method pattern
    • Examples of simple factory patterns in PHP design pattern programming

http://www.bkjia.com/PHPjc/1104342.html www.bkjia.com true http://www.bkjia.com/PHPjc/1104342.html techarticle PHP design mode Simple complaint page instance, PHP design Pattern Example This article introduces the PHP simple complaint page implementation code, share to everyone for reference, the specific content as follows p ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.