PHP设计模式---工厂模式
2012-07-02 11:51
543 查看
工厂模式的作用在与,可以根据输入参数或者应用程序配置的不同类创建一种专门用来实例化并返回其他类的实例的类。工厂模式包含一个名为factory()的方法,这个方法必须返回一个对象
interface IImage{ function getHeight(); function getWidth(); function getData(); } //png图片 class Image_PNG implements IImage{ private $_width,$_height,$_data; public function __construct($file){ $this->_file=$file; $this->_parse(); } private function _parse(){ // echo 'You are a png img!'; } function getHeight(){ return $this->_height; } function getWidth(){ return $this->_width; } function getData(){ return $this->_data; } } //jpeg class Image_JPEG implements IImage{ private $_width,$_height,$_data,$_file; public function __construct($file){ $this->_file=$file; $this->_parse(); } private function _parse(){ //************* $img=Image::getImageInfo($this->_file); $this->_width=$img[0]; echo 'You are a jpeg image o!'; } function getHeight(){ return $this->_height; } function getWidth(){ return $this->_width; } function getData(){ return $this->_data; } } //图片格式工厂 class ImageFactory{ public static function factory($file){ $pathParts=pathinfo($file); switch (strtolower($pathParts['extension'])){ case 'jpg': $ret=new Image_JPEG($file); break; case 'png': $ret=new Image_PNG($file); break; default:// 有问题 } if ($ret instanceof IImage){ return $ret; }else { //有问题 } } } //调用 $image=ImageFactory::factory("D:\\My Documents\\My Pictures\\aa.jpg"); echo $image->getWidth();
相关文章推荐
- PHP设计模式系列 - 工厂模式
- php设计模式(1)-工厂模式
- 浅谈php设计模式(1)---工厂模式
- PHP设计模式之:工厂模式
- PHP设计模式之 单例模式 工厂模式 实例讲解
- php设计模式之工厂模式(工厂模式实现计算器功能)
- php设计模式-工厂模式
- php设计模式(工厂模式)
- php模式设计之 工厂模式
- 魔兽争霸之PHP设计模式-工厂模式[Factory]
- PHP面向对象深入理解之四(设计模式-工厂模式)
- php常用设计模式(策略模式/单例模式/工厂模式)
- php部分---面向对象,设计模式(单例模式、工厂模式)、oop六大原则;
- 设计模式php实例:工厂模式
- php 设计模式(注册器模式+工厂模式+数据库映射模式,单例模式+工厂模式+注册器模式混合使用)
- php设计模式-工厂模式
- PHP设计模式笔记:使用PHP实现工厂模式
- php设计模式 — 工厂方法模式(多态性工厂模式) (虚拟构造子模式)
- php设计模式 工厂模式
- PHP五种常用的设计模式——工厂模式