PHP設計模式之工廠模式詳解
在開發(fā)大型系統(tǒng)時,往往會出現(xiàn)這樣一種情況:
我有一部分基礎數(shù)據(jù),是類classA是從數(shù)據(jù)庫A讀取出來的,其他很多的功能都是基于這個基礎數(shù)據(jù)來操作的。現(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)建對象??梢岳斫獬?,只負責生產(chǎn)同一等級結構中的任何一個產(chǎn)品,但是不能新增產(chǎn)品。
<?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)建對象的接口,讓子類去決定實例化哪個類??梢岳斫獬?,用來生產(chǎn)同一等級結構中的固定產(chǎn)品,但是支持增加產(chǎn)品。
<?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)建一系列相關或者相互依賴的對象的接口。可以理解成,用來生產(chǎn)不用類型的全部產(chǎn)品,但是不能增加新品,支持增加新的類型。
<?php
/**
* 抽象工廠模式
* */
interface userProperties {
function getUsername();
function getGender();
function getJob();
}
interface createUser { //將對象的創(chuàng)建抽象成一個接口
function createOpen($properties);//內(nèi)向創(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]);
?>
如有錯誤,請指正。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
PHP讀取XML文件的方法實例總結【DOMDocument及simplexml方法】
這篇文章主要介紹了PHP讀取XML文件的方法,結合實例形式總結分析了php基于DOMDocument及simplexml方法針對xml文件的載入、讀取等相關操作技巧,需要的朋友可以參考下2019-09-09
php模仿asp Application對象在線人數(shù)統(tǒng)計實現(xiàn)方法
這篇文章主要介紹了php模仿asp Application對象在線人數(shù)統(tǒng)計實現(xiàn)方法,通過一個比較簡單的自定義函數(shù)實現(xiàn)這一功能,具有一定參考借鑒價值,需要的朋友可以參考下2015-01-01
PHP排序算法之冒泡排序(Bubble Sort)實現(xiàn)方法詳解
這篇文章主要介紹了PHP排序算法之冒泡排序(Bubble Sort)實現(xiàn)方法,參照大話數(shù)據(jù)結構中的算法,結合實例形式較為詳細的分析了冒泡排序的原理與相關實現(xiàn)技巧,需要的朋友可以參考下2018-04-04
PHP實現(xiàn)的生成唯一RequestID類完整示例
這篇文章主要介紹了PHP實現(xiàn)的生成唯一RequestID類,結合完整實例形式分析了php唯一標識符生成、session操作等相關實現(xiàn)與使用技巧,需要的朋友可以參考下2018-07-07

