PHP设计模式

TinaXTU

简单工厂模式

// 共同接口
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();
阅读 2.2k

打酱油

267 声望
135 粉丝
0 条评论

打酱油

267 声望
135 粉丝
文章目录
宣传栏