本文章來(lái)給大家介紹php入門教程之工廠模式哦,工廠模式概念:實(shí)例化對(duì)象,用工廠方法替代new操作,有需要了解學(xué)習(xí)的朋友可參考.
工廠模式實(shí)例代碼:
<?php //首先定義什么是人類 interface Human { //人是愉快的,會(huì)笑的 public function laugh(); //人類還會(huì)哭,代表痛苦 public function cry(); //人類會(huì)說(shuō)話 public function talk(); } //然后定義具體的人種 class YellowHuman implements Human {//黃色人種,這個(gè)翻譯的不準(zhǔn)確,將就點(diǎn)吧 function cry() { var_dump( "黃色人種會(huì)哭");} function laugh() {var_dump( "黃色人種會(huì)大笑,幸福呀!");} function talk() {var_dump( "黃色人種會(huì)說(shuō)話,一般說(shuō)的都是雙字節(jié)"); } } class WhiteHuman implements Human { //白色人種 public function cry() {var_dump("白色人種會(huì)哭");} public function laugh() {var_dump("白色人種會(huì)大笑,侵略的笑聲"); } public function talk() {var_dump("白色人種會(huì)說(shuō)話,一般都是但是單字節(jié)!"); } } class BlackHuman implements Human { //黑色人種 public function cry() {var_dump("黑色人種會(huì)哭");} public function laugh() {var_dump("黑色人種會(huì)大笑,侵略的笑聲"); } public function talk() {var_dump("黑色人種會(huì)說(shuō)話,一般都是但是單字節(jié)!"); } } //人種也定義完畢了,那我們把八卦爐定義出來(lái): class HumanFactory { //定一個(gè)烤箱,泥巴塞進(jìn)去,人就出來(lái),這個(gè)太先進(jìn)了 public static function createHuman( $c ){ if ($c instanceof Human ) {//判斷是否繼承自Human return $c; }else { var_dump("必須跟HuMan有一腿"); } } } var_dump("------------造出的第一批人是這樣的:白人-----------------"); $whiteHuman = HumanFactory::createHuman( new WhiteHuman()); $whiteHuman->cry(); $whiteHuman->laugh(); $whiteHuman->talk(); //女?huà)z第二次造人,火候加足點(diǎn),然后又出了個(gè)次品,黑人 var_dump("nn------------造出的第二批人是這樣的:黑人-----------------"); $blackHuman = HumanFactory::createHuman(new BlackHuman()); $blackHuman->cry(); $blackHuman->laugh(); $blackHuman->talk(); //第三批人了,這次火候掌握的正好,黃色人種(不寫黃人,免得引起歧義),備注:RB人不屬于此列 var_dump("nn------------造出的第三批人是這樣的:黃色人種-----------------"); $yellowHuman = HumanFactory::createHuman(new YellowHuman()); $yellowHuman->cry(); $yellowHuman->laugh(); $yellowHuman->talk(); ?>
相關(guān)文章:
Statement of this Website
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

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article
How to fix KB5060533 fails to install in Windows 10?
3 weeks ago
By DDD
Dune: Awakening - Where To Get Insulated Fabric
3 weeks ago
By Jack chen
Gmail Login: How to Sign Up, Sign In, or Sign Out of Gmail - MiniTool
1 months ago
By Jack chen
How to fix KB5060999 fails to install in Windows 11?
3 weeks ago
By DDD
Guild Guide In Tainted Grail: The Fall Of Avalon
4 weeks ago
By Jack chen

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
