PHP 设计模式
1.单例模式
01 02 03 04 05 06 07 08 09 10 11 | class Db { protected $db ; provate function __construct(){} public static function getInstence(){ if (self:: $db ){ return self:: $db ; } self:: $db = new self(); return self:: $db ; } } |
2.工厂模式: (将DB类放到工厂里面,需要调用数据库类的地方,直接调用工厂就可以,无需到处配置DB)
1 2 3 4 5 | class Factory { public static function createDB(){ $db = Db::getInstence(); } } |
3.注册模式
1 2 3 4 5 6 7 8 9 | class Register { protected staitc $objects ; public static function set( $key , $object ){ self:: $objects [ $key ] = $object ; } public static function get( $key ){ return self:: $objects [ $key ]; } } |
4. 适配器模式(例如:整合 mysql、mysqli、PDO)
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | // 定义DB接口 interface IDb{ public function connect( $host , $user , $password , $dbname ); public function query( $sql ); public function close(); } //mysql函数,实现接口 class mysql implements IDb { protected $link ; public function connect( $host , $user , $password , $dbname ){ $this ->link = mysql_connect( $host , $user , $password ); mysql_select_db( $dbname ); } public function query( $sql ){ return mysql_query( $sql , $this ->link); } public function close(){ mysql_close( $this ->link); } } // mysqli 函数,实现接口 class mysqli implements IDb { protected $link ; public function connect( $host , $user , $password , $dbname ){ $this ->link = mysqli_connect( $host , $user , $password , $dbname ); } public function query( $sql ){ return mysqli_query( $this ->link, $sql ); } public function close(){ mysqli_close( $this ->link); } } // PDO 函数,实现接口 class PDO implements IDb { protected $dbh ; public function connect( $host , $user , $password , $dbname ){ $dsn = "mysql:dbname={$dbname}:host={$host}" ; $this ->dbh = new PDO( $dsn , $user , $password ); } public function query( $sql ){ return $thid ->dbh->query( $sql ); } public function close(){ unset( $this ->dbh); } } $db = new mysql(); $db = new mysqli(); $db = new PDO(); $db ->connect( '127.0.0.1' , 'root' , 'root' , 'test' ); $db ->query( 'select * from user' ); $db ->close(); |
5.策略模式(适应某种场景上下文环境)
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <?php interface UserStrategy { public function showAd(); public function showCategory(); } class FamaleStrategy implements UserStrategy { public function showAD(){ return '新款女装' ; } public function showCategory(){ return '女装' ; } } class MaleStrategy implements UserStrategy{ public function showAD(){ return 'pro' ; } public function showCategory(){ return '电子产品' ; } } class page { private $strategy ; public function show(){ echo $this ->strategy->showAD(); echo "<br>" ; echo $this ->strategy->showCategory(); } public function setStrategy(UserStrategy $strategy ){ $this ->strategy = $strategy ; } } $user = isset( $_GET [ 'user' ]) ? $_GET [ 'user' ] : '' ; $page = new page(); if ( $user == 'famale' ){ $strategy = new FamaleStrategy(); } else { $strategy = new MaleStrategy(); } $page ->setStrategy( $strategy ); $page ->show(); |
6.数据对象映射模式(将类封装与数据对应)
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | // 用户表 create table user ( user_id int primary key auto_incremnet, username char(20) not null ) insert user values(null, 'user' ); class user { public $user_id ; public $username ; private $link ; public function __construct( $user_id ){ $this ->link = mysqli_connect( 'localhost' , 'root' , 'root' , 'userdb' ); $res = mysqli_query( $this ->link, 'select * from user where id = $user_id "); $user = $res ->fetch_assoc(); $this ->user_id = $user [ 'user_id' ]; $this ->username = $user [ 'username' ]; } public function __destruct(){ mysqli_query( $this ->link, "update user set username='{$this->username}' where userid=$this->user_id" ); } } $user = new user(1); $user ->username = 'user123' ; |
7.观察者模式
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | interface Observer { public function update(); } class EventGenerator { private static $observers = array (); public static function addObserver(Observer $observer ){ self:: $observers [] = $observer ; } public function notify(){ foreach (self:: $observers as $observer ) { $observer ->update(); } } } class Event extends EventGenerator { public function trigger() { $this ->notify(); } } class Observer1 implements Observer { public function update(){ echo 'observer1' ; } } class Observer2 implements Observer { public function update(){ echo 'observer2' ; } } $event = new Event(); EventGenerator::addObserver( new observer2()); $event ->trigger(); |
8.原型模式 (通过 close 原型对象创建新对象, 避免了没有new 对象的消耗,原型仅需要内存拷贝即可)
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 | class Prototype { public function __construct() { } public function test() { echo 'test' ; } } $prototype = new Prototype(); $prototype1 = close $prototype ; $prototype1 ->test(); $prototype2 = close $prototype ; $prototype2 ->test(); |
9. 装饰器模式 (可以动态添加修改类的功能)
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | interface IDecorater { public function before(); public function after(); } class TestDecorator { protected $idecorater ; public function __construct(){ } public function addObject(IDecorater $idecorater ){ $this ->idecorater[] = $idecorater ; } private function before(){ foreach ( $this ->idecorater as $object ) { $object ->before(); } } private function after(){ $this ->idecorater = array_reverse ( $this ->idecorater); foreach ( $this ->idecorater as $object ) { $object ->after(); } } public function show(){ $this ->before(); echo 'This is show' ; $this ->after(); } } class ColorDecorator implements IDecorater { protected $color ; public function __construct( $color = 'red' ){ $this ->color = $color ; } public function before(){ echo "<div style='color:" . $this ->color. "'>" ; } public function after(){ echo "</div>" ; } } class SizeDecorator implements IDecorater{ protected $size ; public function __construct( $size = 12){ $this ->size = $size ; } public function before(){ echo "<div style='font-size:" . $this ->size. "'>" ; } public function after(){ echo "</div>" ; } } $testDecorator = new TestDecorator(); $testDecorator ->addObject( new ColorDecorator()); $testDecorator ->addObject( new SizeDecorator(16)); $testDecorator ->show(); |
10.迭代器模式(在不需要了解内部实现,遍历一个聚合对象的内部元素。使用集成 Iterator(迭代器)接口)
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | class Myiterator implements Iterator { private $position = 0; private $array = array ( "firstelement" , "secondelement" , "lastelement" , ); public function __construct() { $this ->position = 0; } public function rewind () { // 返回到迭代器的第一个元素 $this ->position = 0; } public function current() { return $this -> array [ $this ->position]; } public function key() { return $this ->position; } public function next() { $this ->position++; } public function valid() { // 检查当前位置是否有效 return isset( $this -> array [ $this ->position]); } } $Myiterator = new Myiterator(); echo "<pre>" ; foreach ( $Myiterator as $key => $value ) { var_dump( $key , $value ); } |
11.代理模式 (客户端与实体之间建立一个代理对象[proxy], 客户端进行操作全部委托给代理对象,隐藏实体的具体实现细节)
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 | /* * 代理模式,实现数据库读写分类 */ interface IUser { public function getUser( $id ); public function setUser( $id = '' ); } class Proxy implements Iuser { public function getUser( $id ){ $user = Fectory::getDatabase( 'slave' ); $user ->query( "select * from user where id = {$id}" ); } public function setUser( $id , $username ){ $user = Fectory::getDatabase( 'master' ); $user ->query( "update user set username = '{$username}' where id={$id}" ); } } $proxy = new Proxy(); $proxy ->getUser(1); $proxy ->set(1, '123' ); |