本文章来给大家介绍php5中的一个新东西,就是工厂模式了,这里是我常用工厂模式的一些学习笔记,下面与大家一同分享有需要了解的朋友可参考.
在大型系统中,许多代码依赖于少数几个关键类.需要更改这些类时,可能会出现困难.例如,假设您有一个从文件读取的 User 类.您希望将其更改为从数据库读取的其他类,但是,所有的代码都引用从文件读取的原始类.这时候,使用工厂模式会很方便.工厂模式是一种类,它具有为您创建对象的某些方法.您可以使用工厂类创建对象,而不直接使用 new.这样,如果您想要更改所创建的对象类型,只需更改该工厂即可.使用该工厂的所有代码会自动更改.
示例1:显示工厂类的一个示列.
等式的服务器端包括两个部分:数据库和一组 PHP 页面,这些页面允许您添加反馈、请求反馈列表并获取与特定反馈相关的文章
实例代码如下:
- <?php
- interface IUser
- {
- function getName();
- }
- class User implements IUser
- {
- public function __construct( $id ) { }
- public function getName()
- {
- return "Jack";
- }
- }
- class UserFactory
- {
- public static function Create( $id )
- {
- return new User( $id );
- }
- }
- $uo = UserFactory::Create( 1 );
- echo( $uo->getName()."n" );
- ?>
IUser接口定义用户对象应执行什么操作.IUser 的实现称为 User,UserFactory 工厂类则创建 IUser 对象.此关系可以用图1中的UML表示.
图 1. 工厂类及其相关 IUser 接口和用户类如果您使用 php 解释器在命令行上运行此代码,将得到如下结果:
测试代码会向工厂请求 User 对象,并输出 getName 方法的结果.有一种工厂模式的变体使用工厂方法.类中的这些公共静态方法构造该类型的对象.如果创建此类型的对象非常重要,此方法非常有用.例如,假设您需要先创建对 象,然后设置许多属性.此版本的工厂模式会将该进程封装在单个位置中,这样,不用复制复杂的初始化代码,也不必将复制好的代码在在代码库中到处粘贴.
示例2 显示使用工厂方法的一个示例.
实例代码如下:
- <?php
- interface IUser
- {
- function getName();
- }
- class User implements IUser
- {
- public static function Load( $id )
- {
- return new User( $id );
- }
- public static function Create( )
- {
- return new User( null );
- }
- public function __construct( $id ) { }
- public function getName()
- {
- return "Jack";
- }
- }
- $uo = User::Load( 1 );
- echo( $uo->getName()."n" );
- ?>
好了上面讲了很多了, 下面我来来看个实例
我们建立以下四个文件
index.php实例代码如下:
- <?php
- include_once("f.inc.php");
- $f=new factory;
- $t1=&$f->create('T1');
- echo $t1->getName();
- echo $config;
- ?>
f.inc.php实例代码如下:
- <?php
- class factory
- {
- function factory()
- {
- $this->mClasses=array
- ('T1'=>'t1.inc.php','T2'=>'t2.inc.php');
- }
- function &create($class)
- {
- if (!class_exists($class))
- {
- require_once($this->mClasses[$class]);
- }
- return new $class;
- }
- }
- ?>
t1.inc.php实例代码如下:
- <?php
- global $config;
- $config='surfchen';
- class T1
- {
- var $mName='name::T1';
- function getName()
- {
- return $this->mName;
- }
- }
- ?>
t2.inc.php实例代码如下:
- <?php
- class T2
- {
- function T2()
- {
- echo 't2 is ok';
- }
- }
- ?>
在index.php里,我们通过一个factory类来创建其他的类实例.在factory里,保存有一个数组$this->mClasses,格式为array(“类名”=>”类文件路径”).我们通过factory::create()来创建一个类实例的时候,在create()里,会首先检测类是否存在,如果不存在,就根据$this->mClasses把类对应的类文件包含进来.然后创建并返回一个该类的实例.这样,我们只需要把factory类文件包含进执行的脚本(如index.php)中就可以了.大家可能还注意到了在t1.inc.php中的这两行代码.
实例代码如下:
- global $config;
- $config='surfchen';
为什么需要global呢?因为t1.inc.php是在factory::create中被包含的,t1文件中的变量将会默认为create的函数级变量.所以我们需要对其中的变量(如$config)进行global以便index.php能访问到.运行index.php,将会输出
实例代码如下:
- name::T1surfchen
波比源码 » PHP设计模式之工厂模式学习笔记
generic levaquin 250mg levofloxacin 500mg oral
purchase imdur sale telmisartan 20mg pills telmisartan 80mg price
buy salbutamol 100mcg pill sildenafil 200 mg viagra for sale
order viagra without prescription lisinopril 5mg ca lisinopril 5mg price
order levitra online cheap order vardenafil 20mg pills methylprednisolone 16 mg without a doctor prescription
oral tadalafil 40mg order tadalafil sale order viagra 100mg pill
order imitrex 25mg for sale imitrex 50mg for sale order avodart pills
sildenafil 50 mg order sildenafil 100mg pill tadalafil cialis
order generic deltasone order isotretinoin 40mg generic order amoxicillin 250mg pills
lasix for sale online lasix cheap hydroxychloroquine 400mg cheap
sildenafil online buy fildena 100mg price budesonide for sale
naprosyn pills order naproxen without prescription order lansoprazole 15mg pill
clarithromycin price clonidine 0.1mg us meclizine 25mg drug
buy spiriva 9 mcg sale hytrin 1mg cheap buy terazosin 5mg generic
pioglitazone 15mg pills sildenafil 50mg brand pfizer viagra 50mg
cialis in usa no deposit bonus codes slot games online free
nifedipine 10mg cost purchase allegra online order allegra 180mg generic
blackjack online real money gambling site empire city casino online
ramipril 5mg sale buy altace 5mg generic etoricoxib 60mg tablet
oral doxycycline 200mg albuterol 2mg pills purchase cleocin generic
purchase diamox pill order imdur 40mg pill imuran 25mg generic
order generic lanoxin cheap lanoxin 250 mg oral molnupiravir 200 mg
carvedilol pills order ditropan 5mg generic buy elavil sale
famotidine 40mg over the counter buy remeron 30mg for sale remeron 15mg generic
buy requip 1mg online rocaltrol 0.25mg generic order labetalol generic
buy tricor 200mg pills overnight delivery viagra cheap sildenafil pills
esomeprazole 20mg pill clarithromycin generic furosemide 100mg brand
tadalafil for women order generic sildenafil 100mg real viagra pharmacy prescription
buy modafinil 100mg generic buy stromectol generic phenergan for sale online
buy deltasone 5mg order amoxicillin generic order amoxil 250mg
fildena 50mg generic red ed pill finasteride 1mg sale
ivermectin goodrx buy stromectol 3mg for sale cheap prednisone
zofran sale brand amoxicillin 250mg buy generic bactrim 960mg
modafinil 100mg brand purchase lisinopril online cheap purchase metoprolol generic
avodart for sale purchase xenical pill orlistat generic
azathioprine medication telmisartan 80mg drug naproxen 500mg canada
buy generic cefdinir 300 mg protonix 20mg uk protonix for sale
purchase dapsone online cheap buy mesalamine without prescription atenolol 50mg ca
viagra 100 mg buy cialis in usa cialis 20mg price
order alfuzosin 10mg without prescription desyrel sale purchase diltiazem pill
levofloxacin 250mg over the counter buy bupropion online order zyban 150 mg generic
buy generic medex for sale reglan over the counter zyloprim 100mg cost
order generic cenforce 50mg buy generic glycomet over the counter glycomet cost
cialis 5mg tablet viagra pills 25mg buy ed pills fda
buy generic neurontin 600mg monodox uk order monodox
buy prednisolone generic buy prednisolone 10mg lasix sale
vibra-tabs order buy augmentin medication buy generic clavulanate
buy albenza tablets generic provera 10mg provera without prescription
glycomet 1000mg drug order generic glucophage 500mg order amlodipine 5mg generic
purchase praziquantel sale buy hydrochlorothiazide 25mg pills buy periactin 4mg
buy pregabalin without a prescription priligy 90mg sale priligy online order
rosuvastatin medication rosuvastatin 10mg cost buy domperidone medication
oral imitrex 25mg levaquin ca order avodart for sale
purchase sumycin cyclobenzaprine 15mg brand purchase baclofen online cheap
clopidogrel 150mg cost buy clopidogrel generic nizoral 200mg over the counter
tamsulosin usa flomax 0.4mg pill aldactone 100mg canada
buy cymbalta 40mg generic cymbalta 40mg pill buy nootropil no prescription
order ipratropium 100mcg sale oral dexamethasone 0,5 mg zyvox 600 mg us
progesterone 100mg cheap purchase olanzapine pills buy zyprexa 10mg pill
buy starlix 120 mg online cheap nateglinide 120 mg pills order candesartan sale
buy zocor 20mg sale valtrex 500mg ca buy viagra 100mg for sale
buy carbamazepine 400mg buy lincomycin for sale buy lincomycin 500mg without prescription
duricef 500mg cost propecia 1mg canada propecia uk
estrace order online buy generic prazosin online buy prazosin 1mg pills
diflucan pills buy cipro online cheap ciprofloxacin sale
order mebendazole 100mg for sale order tadalafil 20mg without prescription buy tadalafil 10mg sale
buy flagyl 400mg generic buy trimethoprim generic buy cephalexin medication
how to get cleocin without a prescription buy cleocin 300mg sale brand fildena 50mg
indomethacin 50mg price cefixime 100mg price cefixime 100mg cheap
order tamoxifen 20mg pill order nolvadex 10mg generic order ceftin 250mg pill
bimatoprost pills buy robaxin for sale how to get trazodone without a prescription
minocycline pills minocin buy online actos 30mg pill
order cialis cialis 5mg cost of cialis
azipro canada purchase azithromycin generic buy gabapentin online
ramipril 10mg brand glimepiride 4mg over the counter cheap etoricoxib 120mg
purchase asacol buy asacol 400mg online cheap irbesartan 300mg us
coreg us coreg cost chloroquine sale
oral digoxin 250 mg molnupiravir without prescription buy generic molnunat 200mg
proventil 100mcg us cheap albuterol purchase phenazopyridine for sale
montelukast over the counter buy symmetrel without prescription avlosulfon for sale
buy nifedipine 10mg generic perindopril 8mg cheap buy generic fexofenadine 120mg
dapoxetine uk order misoprostol generic buy xenical tablets
oral diltiazem 180mg buy acyclovir cheap zyloprim 100mg drug
buy tetracycline 250mg cheap ozobax buy generic lioresal for sale
sulfamethoxazole cost purchase septra sale cleocin 300mg ca
buy generic budesonide for sale buy careprost online cheap buy bimatoprost generic
order generic reglan 20mg buy reglan generic esomeprazole 40mg price
buy aurogra no prescription estradiol 2mg sale estradiol over the counter
buy tretinoin cream online cheap tadalafil pills avanafil 100mg price
finasteride medication sildenafil online buy sildenafil pharmacy
order tadalafil 10mg pills guaranteed cialis overnight delivery usa purchase viagra
terbinafine where to buy trimox 250mg oral buy amoxicillin 500mg generic
buy anastrozole arimidex 1 mg for sale buy catapres 0.1mg online
buy generic imuran 50mg order imuran online cheap purchase micardis for sale
best ed medications sildenafil oral brand viagra 50mg
phenazopyridine 200mg cost amantadine order oral amantadine
natural ed pills tadalafil 5mg pills order tadalafil 5mg for sale
buy avlosulfon 100mg online buy generic nifedipine online cheap aceon 8mg
buy allegra generic buy ramipril paypal order glimepiride 1mg online cheap
order arcoxia online cheap buy cheap asacol cost azelastine
avapro price buspar brand buspirone 5mg generic
where can i buy albenza provera for sale online buy medroxyprogesterone 10mg online
cheap biltricide 600 mg buy hydrochlorothiazide tablets buy cyproheptadine online cheap
nitrofurantoin oral order ibuprofen 400mg pills buy pamelor 25mg online
buy glucotrol 10mg pills buy betnovate 20 gm online cheap betamethasone 20 gm usa
calcitriol online fenofibrate pill buy tricor 200mg without prescription
captopril 25mg us candesartan 8mg oral tegretol ca
combivir oral cost accupril purchase quinapril for sale
frumil 5mg price differin for sale online buy zovirax without prescription
buy valaciclovir 500mg generic ofloxacin 400mg us buy floxin pills