工厂类就是一个专门用来创建其它对象的类,工厂类在多态性编程实践中是非常重要的。它允许动态替换类,修改配置,会使应用程序更加灵活。掌握工厂模式对Web开发是必不可少的。
工厂模式通常用来返回类似接口的不同的类,工厂的一种常见用法就是创建多态的提供者。
通常工厂模式有一个关键的构造,即一般被命名为factory的静态方法。这个静态方法可以接受任意数量的参数,并且必须返回一个对象。
Program List:基本的工厂类
<?php class Fruit { // 对象从工厂类返回 } Class FruitFactory { public static function factory() { // 返回对象的一个新实例 return new Fruit(); } } // 调用工厂 $instance = FruitFactory::factory(); ?>
Program List:利用工厂类生产对象
<?php class Example { // The parameterized factory method public static function factory($type) { if (include_once 'Drivers/' . $type . '.php') { $classname = 'Driver_' . $type; return new $classname; } else { throw new Exception('Driver not found'); } } } // Load a MySQL Driver $mysql = Example::factory('MySQL'); // Load an SQLite Driver $sqlite = Example::factory('SQLite'); ?>
Program List:一个完整的工厂类
下面的程序定义了一个通用的工厂类,它生产能够保存你所有操作的空对象,你可以获得一个实例,这些操作都在那个实例中了。
<?php /** * Generic Factory class * * This Magic Factory will remember all operations you perform on it, * and apply them to the object it instantiates. * */ class FruitFactory { private $history, $class, $constructor_args; /** * Create a factory of given class. Accepts extra arguments to be passed to * class constructor. */ function __construct( $class ) { $args = func_get_args(); $this->class = $class; $this->constructor_args = array_slice( $args, 1 ); } function __call( $method, $args ) { $this->history[] = array( 'action' => 'call', 'method' => $method, 'args' => $args ); } function __set( $property, $value ) { $this->history[] = array( 'action' => 'set', 'property' => $property, 'value' => $value ); } /** * Creates an instance and performs all operations that were done on this MagicFactory */ function instance() { # use Reflection to create a new instance, using the $args $reflection_object = new ReflectionClass( $this->class ); $object = $reflection_object->newInstanceArgs( $this->constructor_args ); # Alternative method that doesn't use ReflectionClass, but doesn't support variable # number of constructor parameters. //$object = new $this->class(); # Repeat all remembered operations, apply to new object. foreach( $this->history as $item ) { if( $item['action'] == 'call' ) { call_user_func_array( array( $object, $item['method'] ), $item['args'] ); } if( $item['action'] == 'set' ) { $object->{$item['property']} = $item['value']; } } # Done return $object; } } class Fruit { private $name, $color; public $price; function __construct( $name, $color ) { $this->name = $name; $this->color = $color; } function setName( $name ) { $this->name = $name; } function introduce() { print "Hello, this is an {$this->name} {$this->sirname}, its price is {$this->price} RMB."; } } # Setup a factory $fruit_factory = new FruitFactory('Fruit', 'Apple', 'Gonn'); $fruit_factory->setName('Apple'); $fruit_factory->price = 2; # Get an instance $apple = $fruit_factory->instance(); $apple->introduce(); ?>
程序运行结果:
Hello, this is an Apple , its price is 2 RMB.
工厂模式主要是为创建对象提供过渡接口,以便将创建对象的具体过程屏蔽隔离起来,达到提高灵活性的目的。
工厂模式可以分为三类:
- 简单工厂模式(Simple Factory)
- 工厂方法模式(Factory Method)
- 抽象工厂模式(Abstract Factory)
这三种模式从上到下逐步抽象,并且更具一般性。
简单工厂模式又称静态工厂方法模式。重命名上就可以看出这个模式一定很简单。它存在的目的很简单:定义一个用于创建对象的接口。工厂方法模式去掉了简单工厂模式中工厂方法的静态属性,使得它可以被子类继承。这样在简单工厂模式里集中在工厂方法上的压力可以由工厂方法模式里不同的工厂子类来分担。
工厂方法模式仿佛已经很完美的对对象的创建进行了包装,使得客户程序中仅仅处理抽象产品角色提供的接口。那我们是否一定要在代码中遍布工厂呢?大可不必。也许在下面情况下你可以考虑使用工厂方法模式:
- 当客户程序不需要知道要使用对象的创建过程。
- 客户程序使用的对象存在变动的可能,或者根本就不知道使用哪一个具体的对象。

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

WebStorm Mac版
好用的JavaScript开发工具

Dreamweaver Mac版
视觉化网页开发工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

记事本++7.3.1
好用且免费的代码编辑器