4.1 客户可以根据自己的需求选择sqlserver数据库或者access数据库
4.2 类图.
4.3 php源码
<?php /** * 抽象工厂模式 * copyright (c) http://blog.csdn.net/CleverCode */ class User {/*{{{*/ public $id; public $name; public function __construct($id,$name) { $this->id = $id; $this->name = $name; } public function setId($id) { $this->id = $id; } public function getId() { return $this->id; } public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } }/*}}}*/ //操作User表 class SqlServerUser {/*{{{*/ public function insert(User $user) { echo "SqlServer 向用户表插入一天记录。id:{$user->id},name:{$user->name}\r\n"; } public function getUser($id) { echo "SqlServer 获取用户id:{$id}记录\r\n"; } }/*}}}*/ class AccessUser {/*{{{*/ public function insert(User $user) { echo " Access 向数据库用户表插入一天记录。id:{$user->id},name:{$user->name}\r\n"; } public function getUser($id) { echo " Access 获取用户id:{$id}记录\r\n"; } }/*}}}*/ //部门表 class Dept {/*{{{*/ public $id; public $name; public function __construct($id,$name) { $this->id = $id; $this->name = $name; } public function setId($id) { $this->id = $id; } public function getId() { return $this->id; } public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } }/*}}}*/ class SqlServerDept {/*{{{*/ public function insert(Dept $dept) { echo "SqlServer 部门插入一天记录。id:{$dept->id},name:{$user->dept}\r\n"; } public function getUser($id) { echo "SqlServer 获取部门id:{$id}记录\r\n"; } }/*}}}*/ class AccessDept {/*{{{*/ public function insert(Dept $dept) { echo " Access 部门表表插入一天记录。id:{$user->id},name:{$user->name}\r\n"; } public function getUser($id) { echo " Access 获取部门id:{$id}记录\r\n"; } }/*}}}*/ //抽象工厂 abstract class DataOperate {/*{{{*/ abstract public function createUser(); abstract public function createDept(); }/*}}}*/ //具体工厂access class DataAccess extends DataOperate {/*{{{*/ public function createUser() { return new AccessUser(); } public function createDept() { return new AccessDept(); } }/*}}}*/ //具体工厂SqlServer class DataSqlServer extends DataOperate {/*{{{*/ public function createUser() { return new SqlServerUser(); } public function createDept() { return new SqlServerDept(); } }/*}}}*/ class Client {/*{{{*/ //客户选择的db public static function getDataOperate($db) { $dataOperate = null; switch($db) { case 'Access': $dataOperate = new DataAccess(); break; case 'SqlServer': $dataOperate = new DataSqlServer(); break; } return $dataOperate; } public static function main($argv) { //客户选择db $dataOperate = self::getDataOperate('Access'); //插入一个新的用户 $user = new User(1,'张三'); $userDb = $dataOperate->createUser(); $userDb->insert($user); } }/*}}}*/ Client::main($argv); ?>
<?php /** * 抽象工厂模式 * copyright (c) http://blog.csdn.net/CleverCode */ class User {/*{{{*/ public $id; public $name; public function __construct($id,$name) { $this->id = $id; $this->name = $name; } public function setId($id) { $this->id = $id; } public function getId() { return $this->id; } public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } }/*}}}*/ //操作User表 class SqlServerUser {/*{{{*/ public function insert(User $user) { echo "SqlServer 向用户表插入一天记录。id:{$user->id},name:{$user->name}\r\n"; } public function getUser($id) { echo "SqlServer 获取用户id:{$id}记录\r\n"; } }/*}}}*/ class AccessUser {/*{{{*/ public function insert(User $user) { echo " Access 向数据库用户表插入一天记录。id:{$user->id},name:{$user->name}\r\n"; } public function getUser($id) { echo " Access 获取用户id:{$id}记录\r\n"; } }/*}}}*/ //部门表 class Dept {/*{{{*/ public $id; public $name; public function __construct($id,$name) { $this->id = $id; $this->name = $name; } public function setId($id) { $this->id = $id; } public function getId() { return $this->id; } public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } }/*}}}*/ class SqlServerDept {/*{{{*/ public function insert(Dept $dept) { echo "SqlServer 部门插入一天记录。id:{$dept->id},name:{$user->dept}\r\n"; } public function getUser($id) { echo "SqlServer 获取部门id:{$id}记录\r\n"; } }/*}}}*/ class AccessDept {/*{{{*/ public function insert(Dept $dept) { echo " Access 部门表表插入一天记录。id:{$user->id},name:{$user->name}\r\n"; } public function getUser($id) { echo " Access 获取部门id:{$id}记录\r\n"; } }/*}}}*/ //抽象工厂(简单工厂+反射进行改进) class DataOperate {/*{{{*/ public $db; public function __construct() { $this->setDB($this->getDbConfig()); } public function setDB($db) { $this->db = $db; } //读取配置文件(使用配置文件配合反射) public function getDbConfig() { //读取配置 //..... return 'Access'; } function createTable($table) { //反射[db.table] $class = $this->db.$table; if(class_exists($class)) { return new $class(); } throw new Exception($class."类没有找到!"); } }/*}}}*/ class Client {/*{{{*/ public static function main($argv) { //插入一个新的用户 $user = new User(1,'张三'); $dataOperate = new DataOperate(); $userDb = $dataOperate->createTable('User'); $userDb->insert($user); } }/*}}}*/ Client::main($argv); ?>