PHP設計模式

簡單工廠模式

// 共同接口
interface db{
    function conn();
}

// 服務器端開發(不知道將會被誰調用)
class dbsqlite implements db{
    public function conn(){
        echo '鏈接上了sqlite';
    }
}

class dbmysql implements db{
    public function conn(){
        echo '鏈接上了mysql';
    }
}


class Factory{
    public static function creatDB($type){
        if($type == 'mysql'){
            return new dbmysql();
        }elseif($type == 'sqlite'){
            return new dbsqlite();
        }else{
            throw new Exception("Error DB type", 1);
        }
    }
}
// 客戶端調用時,不知道工廠類中實例化的幾種類,只須要傳遞$type參數就能夠
$db = Factory::creatDB('mysql');
$db->conn();

工廠模式

// 共同接口
interface db{
    function conn();
}

interface Factory{
    function createDB();
}

// 服務器端開發(不知道將會被誰調用)
class dbsqlite implements db{
    public function conn(){
        echo '鏈接上了sqlite';
    }
}

class dbmysql implements db{
    public function conn(){
        echo '鏈接上了mysql';
    }
}

class mysqlFactory implements Factory{
    public function createDB(){
        return new dbmysql();
    }
}

class sqliteFactory implements Factory{
    public function createDB(){
        return new dbsqlite();
    }
}


// =====服務器端添加了Oracle類
// 前面的代碼不用修改
class dboracle implements db{
    public function conn(){
        echo '鏈接上了oracle';
    }
}

class oracleFactory implements Factory{
    public function createDB(){
        return new dboracle();
    }
}

// =====客戶端開始====
$fact = new mysqlFactory();
$db = $fact->createDB();
$db->conn();

$fact = new sqliteFactory();
$db = $fact->createDB();
$db->conn();

$fact = new oracleFactory();
$db = $fact->createDB();
$db->conn();

// 在面向對象設計法則中,重要的開閉原則--對於修改是封閉的,對於擴展是開放的

單例模式

// 第二步 封鎖new操做
class sigle{
    protected static $ins = null;
    // 方法前加final,則方法不能被覆蓋,在類前加final,則不能被繼承
    final protected function __contruct(){
        
    }

    public static function getIns(){
        if(self::$ins === null){
            self::$ins = new self();
        }
        return self::$ins;
    }

    // 防止被克隆
    final protected function __clone(){}
}

$s1 = sigle::getIns();
// $s2 = clone $s1;
$s2 = sigle::getIns();
if($s1 === $s2){
    echo '同一個對象';
}else{
    echo '不是同一個對象';
}

觀察者模式

class User implements SplSubject{
    public $lognum;
    public $hobby;

    protected $observers = null;

    public function __construct($hobby){
        $this->lognum = rand(1,10);
        $this->hobby  = $hobby;
        $this->observers = new SplObjectStorage();
    }

    public function login(){
        $this->notify();
    }

    public function attach(SplObserver $observer){
        $this->observers->attach($observer);
    }

    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 security implements SplObserver{
    public function update(SplSubject $subject){
        if($subject->lognum<3){
            echo '這是第'.$subject->lognum.'次安全登陸';
        }else{
            echo '這是第'.$subject->lognum.'次登陸,異常';
        }
    }
}

class ad implements SplObserver{
    public function update(SplSubject $subject){
        if($subject->hobby == 'sports'){
            echo '籃球';
        }else{
            echo '好好學習';
        }
    }
}

// 實施觀察
$user = new User('sports');
$user->attach(new security());
$user->attach(new ad());
$user->login();

裝飾器模式

// 裝飾器模式作文章修飾功能
class baseArt{
    protected $content;
    protected $art = null;

    public function __construct($content){
        $this->content = $content;
    }

    public function decorator(){
        return $this->content;
    }
}

// 編輯文章摘要
class editorArt extends baseArt{
    public function __construct(baseArt $art){
        $this->art = $art;
        $this->decorator();
    }

    public function decorator(){
        //return $this->art->content .= '小編摘要';
        return $this->content = $this->art->decorator() . '小編摘要';
    }
}

// SEO添加內容
class SEOart extends baseArt{
    public function __construct(baseArt $art){
        $this->art = $art;
        $this->decorator();
    }
    public function decorator(){
        return $this->content = $this->art->decorator() . 'SEO關鍵詞';
    }
}

// 不管是哪一個先編輯,順序均可以進行交換
$x = new SEOart(new editorArt(new baseArt('每天向上')));
$y = new editorArt(new SEOart(new baseArt('每天向上')));
echo $x->decorator();
echo '<br>';
echo $y->decorator();
相關文章
相關標籤/搜索