PHP之设计模式—建造者模式(通过选择mysql,mongo数据库链接类型做说明)

来源:互联网 发布:hp1010打印机端口 编辑:程序博客网 时间:2024/05/13 11:19

1、什么是建造者模式?

      建造者设计模式定义了处理其他对象的复杂构建的对象设计。

2、用一个简单的mysql,mongo链接类说明:

<?php /** * 数据库链接类 - 此类只是一个简单的说明实例,如需使用,请加以简单完善修改后在使用 * ------------------------ * @author liuxinming  * @Dtime:2012-07-02 */ class connect{ private $localhost;//主机 sql服务器 private $dbuser;//数据库用户名 private $password;//数据库密码 private $database;//数据库名 private $charset;//编码 private $pconnect;//是否持久链接  private $port; //数据库端口 private $mongo;//mongo 对象 private $db;//db mongodb对象数据库  public function __construct($config){$this->localhost=$config['localhost']; $this->dbuser=$config['dbuser']; $this->password=$config['password']; $this->database=$config['database']; $config['charset']?$this->charset=$config['charset']:'utf8';//默认为utf8字符集 $config['pconnect']?$this->pconnect=$config['pconnect']:0;//默认为utf8字符集 $config['port']?$this->port=$config['port']:3360;//端口  //MongoDB if (!$config['option']) $config['option'] = array('connect' => true);} /** * MYSQL连接 * @return obj */ public function db_connect(){ $conn = ($config['pconnect'] == 0) ? mysql_connect($this->localhost, $this->dbuser, $this->password, true) : mysql_pconnect($this->localhost, $this->dbuser, $this->password);    if(!$conn){         die('Could not connect: ' . mysql_error());      }    mysql_query('SET NAMES ' . $this->database, $conn);  //设置数据库字符集    $dbname=@mysql_select_db($this->database, $conn);      if(!$dbname){          die ("Can\'t use test_db : " . mysql_error());      }      return $conn;   } /**  * NoSql mongoDB链接  */ public function mongodb(){ $server = 'mongodb://' . $this->localhost . ':' . $this->port; //链接mongodb 数据库 $this->mongo = new Mongo($server, $this->options); //选择数据库$this->db = $this->mongo->selectDB($this->database);//用户名 密码$this->db->authenticate($this->dbuser, $this->password);//$mongo->connect();  //调用connect方法,来保持连接。  //$mongo->close();  //调用close方法,关闭数据库连接。  //$alldb= $this->mongo->listDBs(); //调用listDBs方法,返回$alldb这个多维数组。显示所有的数据库。  } } /**  * 建造者模式类  目的是处理数据库链接对象的复杂构建的对象设计  * 这只是一个简单说明类,如需分装一个完整的正式环境下用的数据库多种类型链接类,请稍微加以修改即可用。  * 当然此类你也可以直接拿去用,只是感觉不完美。  */  class MysqlDb{  protected $obj;  protected $sqltype;  public function __construct($config){  $this->obj=new connect($config);  $this->sqltype=$config['sqltype'];  }  public function buildDb(){  if($this->sqltype=='mysql'){  $this->obj->db_connect();  }else{  $this->obj->mongodb();  }  }  }/* 创建过程被封装了,方便切换使用mysql 或 mongo数据库链接  */  $config = array( 'sqltype'=>'mysql',     'localhost' => '127.0.0.1',      'dbuser' => 'root',      'password' => '123456' ,    'database'=>'weike',    );   $sql=new MysqlDb($config); $sql->buildDb();  // 用sql查询测试  $sql='select * from pre_brand'; $result =mysql_query($sql); $row=mysql_fetch_array($result);  print_r($row);  mysql_free_result($result);//释放与之关联的资源 (脚本执行完毕后会自动释放内存)?> 



测试输出结果:

Array ( [0] => 1 [bid] => 1 [1] => 9 [uid] => 9 [2] => 0 [like] => 0 [3] => [type] => [4] => 0 [local] => 0 [5] => 0 [date] => 0 [6] => 0 [extra1] => 0 [7] => 0 [extra2] => 0 [8] => 0 [extra3] => 0 [9] => 0 [extra4] => 0 [10] => 0.00 [extra5] => 0.00 [11] => 0.00 [extra6] => 0.00 [12] => 0.00 [extra7] => 0.00 [13] => 0.00 [extra8] => 0.00 )


PS:由于我的ubuntu服务器没有装MongoDB,所有没有测试mongodb。有安装的同志可以测试下。

原创粉丝点击