欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP設計模式之工廠模式詳解

 更新時間:2017年10月24日 10:18:09   作者:kevinler  
這篇文章主要為大家詳細介紹了PHP設計模式之工廠模式,具有一定的參考價值,感興趣的小伙伴們可以參考一下

在開發(fā)大型系統(tǒng)時,往往會出現(xiàn)這樣一種情況:

我有一部分基礎數(shù)據(jù),是類classA是從數(shù)據(jù)庫A讀取出來的,其他很多的功能都是基于這個基礎數(shù)據(jù)來操作的?,F(xiàn)在呢,我想把數(shù)據(jù)從數(shù)據(jù)庫A變成從另外的數(shù)據(jù)源去獲取,這時候,要修改起來就比較麻煩,要修改其他很多類的代碼。這種設計顯然是不夠靈活的,換句話說,就是緊耦合的,那么什么是緊耦合呢?緊耦合就是指系統(tǒng)中某個部分的函數(shù)或類嚴重依賴于系統(tǒng)的其他部分中的函數(shù)或類的行為和結構。

這時,工廠模式的作用性就體現(xiàn)出來了。

工廠模式    

就是解決這樣的一些情況的設計方法。

工廠模式是一種類,建立了一個工廠來根據(jù)所需來創(chuàng)建對象,這種方式在多態(tài)性編程中是很重要的,允許動態(tài)替換類,修改配置等。

/*基本工廠模式代碼*/

<?php 
/** 
 * 基本工廠模式 
 * */ 
class User { 
 private $username; 
 public function __construct($username) { 
  $this->username = $username; 
 } 
  
 public function getUser() { 
  return $this->username; 
 } 
} 
 
class userFactory { 
 static public function createUser() { 
  return new User('Jack'); 
 } 
} 
 
$user = userFactory::createUser();echo $user->getUser(); 

?>

工廠模式分為:簡單工廠模式、工廠方法模式、抽象工廠模式。

簡單工廠模式,通過靜態(tài)方法創(chuàng)建對象??梢岳斫獬?,只負責生產同一等級結構中的任何一個產品,但是不能新增產品。

<?php

/** 
 *簡單工廠模式 
 * */ 
interface userProperties { 
 function getUsername(); 
 function getGender(); 
 function getJob(); 
} 
class User implements userProperties{ 
 private $username; 
 private $gender; 
 private $job; 
 public function __construct($username, $gender, $job) { 
  $this->username = $username; 
  $this->gender = $gender; 
  $this->job = $job; 
 } 
 
 public function getUsername() { 
  return $this->username; 
 } 
 
 public function getGender() { 
  return $this->gender; 
 } 
 
 public function getJob() { 
  return $this->job; 
 } 
} 
 
class userFactory { 
 static public function createUser($properties = []) { 
  return new User($properties['username'], $properties['gender'], $properties['job']); 
 } 
} 
 
$employers = [ 
 ['username' => 'Jack', 'gender' => 'male', 'job' => 'coder'], 
 ['username' => 'Marry', 'gender' => 'female', 'job' => 'designer'], 
 ]; 
$user = userFactory::createUser($employers[0]); 
echo $user->getUsername(); 
 
?>

工廠方法模式,去掉了簡單工廠模式中方法的靜態(tài)屬性,使其可以被子類集成,定義一個創(chuàng)建對象的接口,讓子類去決定實例化哪個類??梢岳斫獬?,用來生產同一等級結構中的固定產品,但是支持增加產品。

<?php
/** 
 * 工廠方法模式 
 **/ 
interface userProperties { 
 function getUsername(); 
 function getGender(); 
 function getJob(); 
} 
 
interface createUser { 
 function create($properties); 
} 
 
class User implements userProperties{ 
 private $username; 
 private $gender; 
 private $job; 
 public function __construct($username, $gender, $job) { 
  $this->username = $username; 
  $this->gender = $gender; 
  $this->job = $job; 
 } 
 
 public function getUsername() { 
  return $this->username; 
 } 
 
 public function getGender() { 
  return $this->gender; 
 } 
 
 public function getJob() { 
  return $this->job; 
 } 
} 
 
class userFactory { 
 private $user; 
 public function __construct($properties = []) { 
  $this->user = new User($properties['username'], $properties['gender'], $properties['job']); 
 } 
 
 public function getUser() { 
  return $this->user; 
 } 
} 
 
class FactoryMan implements createUser { 
 function create($properties) { 
  return new userFactory($properties); 
 } 
} 
 
class FactoryWoman implements createUser { 
 function create($properties) { 
  return new userFactory($properties); 
 } 
} 
 
class clientUser { 
 static public function getClient($properties) { 
  $fac = new FactoryMan; 
  $man = $fac->create($properties); 
  echo $man->getUser()->getUsername(); 
 } 
} 
 
$employers = [ 
 ['username' => 'Jack', 'gender' => 'male', 'job' => 'coder'], 
 ['username' => 'Marry', 'gender' => 'female', 'job' => 'designer'], 
 ]; 
$user = clientUser::getClient($employers[0]); 
 
?>

抽象工廠模式,提供一個創(chuàng)建一系列相關或者相互依賴的對象的接口??梢岳斫獬桑脕砩a不用類型的全部產品,但是不能增加新品,支持增加新的類型。

<?php

/** 
 * 抽象工廠模式 
 * */ 
 
interface userProperties { 
 function getUsername(); 
 function getGender(); 
 function getJob(); 
} 
 
interface createUser { //將對象的創(chuàng)建抽象成一個接口 
 function createOpen($properties);//內向創(chuàng)建 
 function createIntro($properties);//外向創(chuàng)建 
} 
 
class User implements userProperties{ 
 private $username; 
 private $gender; 
 private $job; 
 public function __construct($username, $gender, $job) { 
  $this->username = $username; 
  $this->gender = $gender; 
  $this->job = $job; 
 } 
 
 public function getUsername() { 
  return $this->username; 
 } 
 
 public function getGender() { 
  return $this->gender; 
 } 
 
 public function getJob() { 
  return $this->job; 
 } 
} 
 
class userFactory { 
 private $user; 
 public function __construct($properties = []) { 
  $this->user = new User($properties['username'], $properties['gender'], $properties['job']); 
 } 
 
 public function getUser() { 
  return $this->user; 
 } 
} 
 
class FactoryMan implements createUser { 
 function createOpen($properties) { 
  return new userFactory($properties); 
 } 
 
 function createIntro($properties) { 
  return new userFactory($properties); 
 } 
} 
 
class FactoryWoman implements createUser { 
 function createOpen($properties) { 
  return new userFactory($properties); 
 } 
 
 function createIntro($properties) { 
  return new userFactory($properties); 
 } 
} 
 
class clientUser { 
 static public function getClient($properties) { 
  $fac = new FactoryMan; 
  $man = $fac->createOpen($properties); 
  echo $man->getUser()->getUsername(); 
 } 
} 
 
$employers = [ 
 ['username' => 'Jack', 'gender' => 'male', 'job' => 'coder'], 
 ['username' => 'Marry', 'gender' => 'female', 'job' => 'designer'], 
 ]; 
$user = clientUser::getClient($employers[0]); 
?>

如有錯誤,請指正。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論