PHP design mode-simple complaint page instance, php design mode instance-PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP design mode: simple complaint page instance, php design mode instance. PHP design mode: simple complaint page instance. php design mode instance This article introduces the PHP simple complaint page implementation code and shares it with you for your reference, the specific content is as follows: simple complaints page instance of the p PHP design mode, and php design mode instance

This article introduces the implementation code of the PHP simple complaint page for your reference. The details are as follows:

Php code:

<? Php/** design mode exercise * 1. database Connection class (Singleton mode) * 2. call the interface to implement the message book function (Factory mode) * 3. implement the hierarchical report processing function (responsibility chain mode) * 4. send different combinations of report information (bridging mode) * 5. send report information in different formats (Adapter mode) * 6. automatically append the time (in decorator mode) * 7. change the display style (Observer Mode) based on the member login information * 8. add experience value (policy mode) */interface DB {function conn ();} based on the posting length ();} /*** 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 successful
';}}/*** Factory mode */interface Factory {function createDB ();} class MysqlFactory implements Factory {public function createDB () {echo' Mysql Factory created successfully
'; Return MysqlSingle: getInstance () ;}/ *** display different styles by 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 ();} public function attach (SplObserver $ observer) {$ this-> _ observers-> attach ($ observer );} public function detach (SplObserver $ observer) {$ this-> _ observers-> detach ($ observer);} public function between Y () {$ 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
';}} Class StyleB implements SplObserver {public function update (SplSubject $ subject) {echo $ subject-> _ style.' Module B
';}}/*** Complain * bridging mode */class Bridge {protected $ _ obj = NULL; public function _ construct ($ obj) according to different methods) {$ 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 'serialized 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 ;}}/*** automatically append the * decorator mode */class Base {protected $ _ content = NULL after the 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 ());}} /*** classification 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 to the database -- Factory + Singleton mode $ mysqlFactory = new MysqlFactory (); $ single = $ mysqlFactory-> createDB (); $ single-> conn (); echo'
'; // Observer Mode $ 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'
'; // Decorator mode $ content =$ _ POST ['content']; $ Decorator = new decorator (new Base ($ content )); echo $ decorator-> show (); echo'
'; // Referer 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 all the content of this article, hoping to help you learn.

Articles you may be interested in:
  • Simple factory mode for php design patterns
  • Explanation of "simple factory mode" instance code in PHP
  • PHP design pattern-Observer pattern example
  • Delegated php design pattern
  • Delegated design pattern for common PHP design patterns
  • Specification mode of PHP design pattern series
  • Learning php design patterns php implementation memorandum patterns (Memento)
  • Learning php design pattern php implements Observer pattern (Observer)
  • Learning php design mode php implementation Template method mode
  • Example: simple factory mode in PHP design mode programming

This article introduces the implementation code of the PHP simple complaint page and shares it with you for your reference. the specific content is as follows...

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.