PHP常用设计模式

2019-12-05  本文已影响0人  牍中玉的小木屋

工厂模式

<?php

class Factory
{
    public static function createDB()
    {
        return new DB();
    }
}

class DB
{
    public function __construct()
    {
        echo '工厂模式';
    }
}

Factory::createDB();

单例模式

<?php

class Database
{
    private static $instance;

    private function __construct()
    {
    }

    private function __clone()
    {
    }

    public static function getInstance()
    {
        if (self::$instance) {
            return self::$instance;
        }

        echo '单例模式';
        self::$instance = new self();
        return self::$instance;
    }
}

Database::getInstance();
Database::getInstance();
Database::getInstance();

注册树模式

<?php

class Register
{
    private static $objects;

    public static function _set($name, $object)
    {
        self::$objects[$name] = $object;
    }

    public static function _get($name)
    {
        return self::$objects[$name];
    }

    public static function _unset($name)
    {
        unset(self::$objects[$name]);
    }
}


/****************  测试  ****************/
Register::_set('test1', '这是一个测试');
echo Register::_get('test1');
Register::_unset('test1');
echo Register::_get('test1');

适配器模式

策略模式

<?php

interface  UserStrategy
{
    function showAD();

    function showCategory();
}

class FemaleStrategy implements UserStrategy
{
    function showAD()
    {
        echo '新款女装';
    }

    function showCategory()
    {
        echo '女装';
    }
}

class MaleStrategy implements UserStrategy
{
    function showAD()
    {
        echo 'IPone11';
    }

    function showCategory()
    {
        echo '电子产品';
    }
}

class Page
{
    protected $strategy;

    public function index()
    {
        echo 'AD:';
        $this->strategy->showAD();
        echo '<br>';

        echo 'category:';
        $this->strategy->showCategory();
        echo '<br>';
    }

    public function setStrategy(UserStrategy $strategy)
    {
        $this->strategy = $strategy;
    }
}


/****************  测试  ****************/
$page = new Page();

if (isset($_GET['female'])) {
    $strategy = new FemaleStrategy();
} else {
    $strategy = new MaleStrategy();
}

// 传入一个策略对象
$page->setStrategy($strategy);
$page->index();

总结

<?php

/**
 * 工厂模式
 * Class Factory
 */
class Factory
{
    static public function createDB()
    {
        echo '工厂模式' . '<br>';
        $db = Database::getInstance();
        Register::_set('db', $db);
        return $db;
    }
}

/**
 * 单例模式
 * Class Database
 */
class Database
{
    private static $db;

    private function __construct()
    {
    }

    private function __clone()
    {
    }

    public static function getInstance()
    {
        if (self::$db) {
            return self::$db;
        }

        echo '单例模式' . '<br>';
        self::$db = new self();
        return self::$db;
    }
}

/**
 * 注册树模式(注册器模式)
 * Class Register
 */
class Register
{
    private static $objects;

    public static function _set($name, $object)
    {
        self::$objects[$name] = $object;
    }

    public static function _get($name)
    {
        return self::$objects[$name];
    }

    public static function _unset($name)
    {
        unest(self::$objects[$name]);
    }
}

/**
 * 适配器模式
 * Interface IDatabase
 */
interface IDatabase
{
    function connect($host, $user, $passwd, $dbname);

    function query($sql);

    function close();
}

class MySQL implements IDatabase
{
    protected $conn;

    function connect($host, $user, $passwd, $dbname)
    {
        $conn = mysql_connect($host, $user, $passwd);
        mysql_select_db($dbname);
        $this->conn = $conn;

    }

    function query($sql)
    {
        $res = mysql_query($sql, $this->conn);
        return $res;
    }

    function close()
    {
        mysql_close($this->conn);
    }
}

class MySQLii implements IDatabase
{
    protected $conn;

    function connect($host, $user, $passwd, $dbname)
    {
        $conn = mysqli_connect($host, $user, $passwd, $dbname);
        $this->conn = $conn;
    }

    function query($sql)
    {
        $res = mysqli_query($this->conn, $sql);
        return $res;
    }

    function close()
    {
        mysqli_close($this->conn);
    }
}

class MyPDO implements IDatabase
{
    protected $conn;

    function connect($host, $user, $passwd, $dbname)
    {
        $conn = new \PDO("mysql:host=$host;dbname=$dbname", $user, $passwd);
        $this->conn = $conn;
    }

    function query($sql)
    {
        return $this->conn->query($sql);
    }

    function close()
    {
        unset($this->conn);
    }
}

/**
 * 策略模式
 * Interface UserStrategy
 */
interface UserStrategy
{
    function showAd();

    function showCategory();
}

class FemaleUserStrategy implements UserStrategy
{
    function showAd()
    {
        echo '新款女装';
    }

    function showCategory()
    {
        echo '女装';
    }
}

class MaleStrategy implements UserStrategy
{
    function showAd()
    {
        echo 'IPhone11';
    }

    function showCategory()
    {
        echo '电子产品';
    }
}

class Page
{
    protected $strategy;

    // 假设为页面展示的内容
    public function index()
    {
        echo 'AD:';
        $this->strategy->showAd();
        echo '<br>';

        echo 'category:';
        $this->strategy->showCategory();
        echo '<br>';
    }

    // 约定下接口类型,传入的是一个策略的对象
    public function setStrategy(UserStrategy $strategy)
    {
        $this->strategy = $strategy;
    }
}


/**************  测试部分  ***************/

$test = Factory::createDB();

var_dump($test);

$page = new Page;

if (isset($_GET['female'])) {
    $strategy = new FemaleUserStrategy();
} else {
    $strategy = new MaleStrategy();
}

//传入一个策略对象
$page->setStrategy($strategy);
$page->index();
上一篇下一篇

猜你喜欢

热点阅读