一. 单例模式(Singleton)
如果应用程序每次包含且仅包含一个对象,那么这个对象就是一单例. 用来替代全局变量.
复制代码 代码如下:
require_once("DB.php");
class DatabaseConnection{
public static function get(){
static $db = null;
if ( $db == null )
$db = new DatabaseConnection();
return $db;
}
private $_handle = null;
private function __construct() {
$dsn = 'mysql://root:password@localhost/photos';
$this->_handle =& DB::Connect( $dsn, array() );
}
public function handle()
{
return $this->_handle;
}
}
print( "Handle = ".DatabaseConnection::get()->handle()."n" );
print( "Handle = ".DatabaseConnection::get()->handle()."n" );
?>
复制代码 代码如下:
interface Hello{
function say_hello();
}
class English implements Hello{
public function say_hello(){
echo "Hello!";
}
}
class Chinese implements Hello{
public function say_hello(){
echo "你好";
}
}
class speak{
public static function factory($type){
if($type == 1) $temp = new English();
else if($type == 2) $temp = new Chinese();
else{
die("Not supported!");
}
return $temp;
}
}
$test = Speak::factory(1);
$test->say_hello();
复制代码 代码如下:
//抽象工厂
interface Speaker{
function assignSpeaker();
}
//具体工厂1
class EnglishSpeaker implements Speaker{
public function assignSpeaker(){
return new English();
}
}
//具体工厂2
class ChineseSpeaker implements Speaker{
public function assignSpeaker(){
return new Chinese();
}
}
//抽象产品
interface Hello{
function say_hello();
}
//具体产品1
class English implements Hello{
public function say_hello(){
echo "Hello!";
}
}
//具体产品2
class Chinese implements Hello{
public function say_hello(){
echo "你好";
}
}
复制代码 代码如下:
if(!empty($_GET['t'])){
switch($_GET['t']){
case 1: $temp=new EnglishSpeaker();
break;
case 2: $temp=new ChineseSpeaker();
break;
}
$man=$temp->assignSpeaker();
$man->say_hello();
}
复制代码 代码如下:
//抽象工厂
abstract class Speaker{
const NORMAL =1;
const SING =2;
abstract function assignSpeaker($flag_int);
}
//具体工厂1
class EnglishSpeaker extends Speaker {
public function assignSpeaker($flag_int){
switch($flag_int){
case self::NORMAL:
return new NormalEnglish();
break;
case self::SING:
return new SingEnglish();
break;
}
}
}
//具体工厂2
class ChineseSpeaker extends Speaker{
public function assignSpeaker($flag_int){
switch($flag_int){
case self::NORMAL:
return new NormalChinese();
break;
case self::SING:
return new SingChinese();
break;
}
}
}
//抽象产品
interface Hello{
function say_hello();
}
//具体产品A1
class NormalEnglish implements Hello{
public function say_hello(){
echo "Hello!";
}
}
//具体产品B1
class NormalChinese implements Hello{
public function say_hello(){
echo "你好!";
}
}
//具体产品A2
class SingEnglish implements Hello{
public function say_hello(){
echo "Oh, jingle bells, jingle bells, Hello! Hello! Hello!";
}
}
//具体产品B2
class SingChinese implements Hello{
public function say_hello(){
echo "叮叮当,叮叮当, 你好!你好!你好!";
}
}
复制代码 代码如下:
//根据程序的业务逻辑确定具体工厂
switch($_GET['language']){
case 1: $temp=new EnglishSpeaker();
break;
case 2: $temp=new ChineseSpeaker();
break;
}
//根据程序的业务逻辑确定具体产品,无需关心是哪个具体工厂了,维护性提高
$man=$temp->assignSpeaker( $_GET['style']);
//使用产品,无需关心是哪个具体产品
$man->say_hello();