search
Homephp教程php手册PHP高级对象构建 工厂模式的使用

工厂模式包含普通工厂模式和抽象工厂模式,但是,不管是什么工厂模式,它们都是有一个作用,那就是生成对象

PHP设计模式工厂模式的使用方法
代码如下:
/*
* 每日练习 PHP设计模式工厂模式的使用方法
* PHP工厂模式不难理解,顾名思义,就是一个加工厂,然后工厂是制造产品的,只要制造产品
* 就必须有几个要素:"方法","模型","工厂车间"。
*/
/*第一种示例 普通工厂模式
* */
abstract class model {//产品模型
abstract function getNames();
}
class zhangsan extends model {//产品实例
function getNames(){
return "my name is zhengsan";
}
}
class lisi extends model{//产品实例
function getNames(){
return "my name is lisi";
}
}
abstract class gongchangModel {//工厂模型
abstract function getZhangsan();
abstract function getLisi();
}
class gongchang extends gongchangModel{//工厂实例
function getZhangsan(){
return new zhangsan();
}
function getLisi(){
return new lisi();
}
}
$gongchang=new gongchang();//实例化工厂
$zhangsan=$gongchang->getZhangsan();//制造产品
echo $zhangsan->getNames();//产品输出功能
?>

之前我写去关于工厂设计模式的文章,实际上,工厂模式包含普通工厂模式和抽象工厂模式,但是,不管是什么工厂模式,它们都是有一个作用,那就是生成对象。
好了,那我们用下面最最简单的例子,再把PHP设计模式中的厂模式再演示一下。
我自己总结了一下,工厂模式的三个要素:
一、产品模型
二、产品实例
三、工厂车间
代码如下:
abstract class prModel {//产品模型
abstract function link();
}
class webLink extends prModel{//实例一个产品
public function link(){
echo "www.jb51.net";
}
}
class gongchang {//工厂
static public function createLink (){
return new webLink();
}
}
$weblink=gongchang::createLink();//通过工厂制造一个对象
$weblink->link();//输出 www.jb51.net
?>

以上方法,就简单的说明了工厂类的使用方法。关注面向对象

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft