PHP中的实现AbstractFactory模式

 

<? php
/* *
 * 数据库连接抽象工厂
 * filename: IAbstractFactory.php
 
*/
interface  IAbstractFactory{
    
function  getMysqlConnection();
    
function  getOracleConnection();
}
?>

 

<? php
/* *
 * filename: ConnectionFactory.php
 
*/
require_once   ' IAbstractFactory.php ' ;
require_once   ' MysqlConnection.php ' ;
require_once   ' OracleConnection.php ' ;
/* *
 * 数据库连接工厂
 
*/
class  ConnectionFactory  implements  IAbstractFactory {
    
/* *
     * uri scheme 一个uri的模式部分,例如 JDBC数据库连接 jdbc:mysql://localhost:3306/test
     * 这里的scheme部分为jdbc,还有其他的比如 http,ftp,https 等等 
     * @access private
     
*/
    
protected   $scheme   =   null ;
    
public   function  getMysqlConnection() {
        
return   new  MysqlConnection();
    }
    
public   function  getOracleConnection() {
        
return   new  OracleConnection();
    }
}
?>

 

<? php
/* *
 * 数据库连接接口
 * filename: IConnection.php
 
*/
interface  IConnection {
}
?>

数据库链接实现

<? php
/* *
 * filename: MysqlConnection.php
 
*/
require_once   ' IConnection.php ' ;
class  MysqlConnection  implements  IConnection {
    
function  __construct() {
    }
}
?>

 

<? php
/* *
 * filename: OracleConnection.php
 
*/
require_once   ' IConnection.php ' ;
class  OracleConnection  implements  IConnection {
    
function  __construct() {
    }
}
?>

UnitTestCase

<? php
require_once   ' simpletest/unit_tester.php ' ;
require_once   ' simpletest/reporter.php ' ;
require_once   ' ConnectionFactory.php ' ;
require_once   ' MysqlConnection.php ' ;
/* *
 * Database Factory Test Case 
 * 
 
*/
class  FactoryTestCase  extends  UnitTestCase {
    
function  testFactoryAndConnection() {
        
//  factory test
         $factory   =   new  ConnectionFactory();
        
$this -> assertNotNull( $factory );
        
$this -> assertIsA( $factory ,   ' ConnectionFactory ' );
        
//  mysql connection test
         $connection   =   $factory -> getMysqlConnection();
        
$this -> assertNotNull( $connection );
        
$this -> assertIsA( $connection ,   ' MysqlConnection ' );
        
//  oracle connection test
         $connection   =   $factory -> getOracleConnection();
        
$this -> assertNotNull( $connection );
        
$this -> assertIsA( $connection ,   ' OracleConnection ' );
    }
}
//  start test
$test   =   new  FactoryTestCase();
$test -> run( new  HtmlReporter());
?>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值