PHP design mode: simple complaint page instance, 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 <br> ';} final protected function _ clone () {return false;} public function conn () {echo 'mysql connection successful <br> ';}/*** Factory mode */interface Factory {function createDB ();} class MysqlFactory implements Factory {public function createDB () {echo 'mysql factory Creation Successful <br> '; 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-> Policy ();} public function attach (SplObserver $ observer) {$ this-> _ observers-> attach ($ observe R);} 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 A <Br> ';}} class StyleB implements SplObserver {public function update (SplSubject $ subject) {echo $ subject-> _ style. 'module B <br> ';}}/*** complaints based on different methods * 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 <br> ';}} class Danger {public function msg () {echo 'danger <br> ';}} /*** adapter mode */class Serialize {public $ content = NULL; public function _ construct ($ content) {$ this-> content = serialize ($ content );} public function show () {return 'serialization format: <br> '. $ t His-> 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: <br> '. $ this-> content ;}}/*** automatically append the * decorator mode */class Base {protected $ _ content = NULL after the complaint content; public function _ construct ($ content) {$ this-> _ content = $ co Ntent;} 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) {I F ($ level <= $ this-> _ level) {echo 'processing level: 1 <br> '; 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 <br> '; return ;}$ top = new $ this-> _ top; $ top-> deal ($ level) ;}} class level3 {protected $ _ level = 3; protected $ _ top = 'level2'; public function deal ($ leve L) {echo 'processing level: 3 <br> '; return ;}} if (! Empty ($ _ POST) {echo '
Html code:
<! DOCTYPE html> <! -- To change this license header, choose License Headers in Project Properties. to change this template file, choose Tools | Templatesand open the template in the editor. -->
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