IImage--factory
<?php
/* 实例4 */
/* 使用工厂类解析图像工作 */
interface IImage {
function getWidth();
function getHeight();
function getData();
}
class Image_PNG implements IImage {
protected $_width, $height, $_data; public function __construct($file) {
$this->_file = $file;
$this->_parse();
} private function _parse() {
//完成PNG格式的解析工作
//并填充$_width, $_height, $_data
$this->_data = getimagesize($this->_file);
list($this->_width, $this->_height) = $this->_data;
} public function getWidth() {
return $this->_width;
} public function getHeight() {
return $this->_height;
} public function getData() {
return $this->_data;
}
}
class Image_JPGE implements IImage { protected $_width, $_height, $_data; public function __construct($file) {
$this->_file = $file;
$this->_parse();
} private function _parse() {
//完成JPGE格式的解析工作
//并填充$_width, $_height, $_data
//$this->_width = imagesx($this->_file);
//$this->_height = imagesx($this->_file);
$this->_data = getimagesize($this->_file);
list($this->_width, $this->_height) = $this->_data;
} public function getWidth(){
return $this->_width;
} public function getHeight(){
return $this->_height;
} public function getData() {
return $this->_data;
}
}
class ImageFactory {
public static function factory($file) {
$filename = pathinfo($file);
switch(strtolower($filename['extension'])){
case 'jpg':
$return = new Image_JPEG($file);
break;
case 'png':
$return = new Image_PNG($file);
break;
default:
echo '图片类型不正确';
break;
}
if($return instanceof IImage) {
return $return ;
} else {
echo '出错了';
exit();
}
} }
$image = ImageFactory::factory('path/my.png');
var_dump($image->getWidth());
echo '<br>';
print_r($image->getheight());
echo '<br>';
print_r($image->getData());
IImage--factory的更多相关文章
- PHP设计模式(三)抽象工厂模式(Abstract Factory For PHP)
一.什么是抽象工厂模式 抽象工厂模式的用意为:给客户端提供一个接口,可以创建多个产品族中的产品对象 ,而且使用抽象工厂模式还要满足以下条件: 系统中有多个产品族,而系统一次只可能消费其中一族产品. 同 ...
- PHP设计模式(一)简单工厂模式 (Simple Factory For PHP)
最近天气变化无常,身为程序猿的寡人!~终究难耐天气的挑战,病倒了,果然,程序猿还需多保养自己的身体,有句话这么说:一生只有两件事能报复你:不够努力的辜负和过度消耗身体的后患.话不多说,开始吧. 一.什 ...
- 菜鸟理解的工厂模式(Factory Pattern)是什么样子的?
直接开始说了,不浪费园友宝贵的时间! 什么是工厂模式? 在学习前,先问一下:"它是什么?". 工厂模式,它是项目里面常用的设计模式之一. 它是属于创建型模式,简单的理解创建型模式就 ...
- Angular Service和Factory应用的区别
Service可以用来将返回同类业务的多种返回值 Factory可以用来提供对同类业务的多个方法的调用 另外:Provider可以用来封装各独立职责
- 【解决方案】 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userHandler': Injection of resource dependencies failed;
一个错误会浪费好多青春绳命 鉴于此,为了不让大家也走弯路,分享解决方案. [错误代码提示] StandardWrapper.Throwableorg.springframework.beans.fac ...
- Design Patterns Simplified - Part 3 (Simple Factory)【设计模式简述--第三部分(简单工厂)】
原文链接:http://www.c-sharpcorner.com/UploadFile/19b1bd/design-patterns-simplified-part3-factory/ Design ...
- C#设计模式之简单工厂模式(Simple Factory)
1. 概述 简单工厂模式就是将一个类的实例化交给一个静态工厂来执行. 2. 使用频率 中 3. 模式结构 3.1 机构图 3.2 模式中的角色 Product:抽象类,把具体产品类公共的代码进行抽象和 ...
- Task.Factory.StartNew的用法
代码: private void button5_Click(object sender, EventArgs e) { ; Task.Factory.StartNew(() => { Mess ...
- 使用Setup Factory安装包制作工具制作安装包
在我们开发完软件后,除了极个别案例我们把整个目录复制给客户用外,我们一般都需要做成安装包,方便整个软件的部署操作,以安装包的部署操作可能简单的是复制文件,也可能包括一些注册表.数据库等额外的操作,不过 ...
- 设计模式 — 简单工厂模式(Simple Factory)
定义:定义一个工厂类,它可以根据参数的不同返回不同类型的实例,被创建的实例通常有公共的父类. 模式类型:创建型模型 Factory(工厂角色):即工厂类,负责实现创建所有产品实例的内部逻辑:工厂类可以 ...
随机推荐
- JavaScript Array pop(),shift()函数
pop() 删除数组的最后一个元素并返回删除的元素 shift() 删除并返回数组的第一个元素
- Oracle 隐式游标 存储过程
--隐式游标 注意变量赋值用(:=) 连接符用(||)而不是加号(+) DECLARE v_pk T_PLAT_KEYWORD.ID%TYPE; --主键 v_amount_message T_PLA ...
- Catch That Cow(广度优先搜索_bfs)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 48036 Accepted: 150 ...
- NSMutableAttributedString 的使用方法,设置格式
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:title]; NS ...
- hibernate之6.one2many单向
表结构: 实体类图: CRUD: Student: package com.demo.model; import java.io.UnsupportedEncodingException; impor ...
- 什么是 Service Mesh
作者|敖小剑 微服务方兴未艾如火如荼之际,在 spring cloud 等经典框架之外,Service Mesh 技术正在悄然兴起.到底什么是 Service Mesh,它的出现能带来什么,又能改变什 ...
- 如何简单的实现一个tab页title的动画效果
首先我们来看看实现的效果 tab上的title沉下去的效果 先来看看布局 <?xml version="1.0" encoding="utf-8"?> ...
- python获取shell输出(转)
From:http://www.cnblogs.com/snow-backup/p/5035792.html python中获取shell命令输出的方法: 1. import subproces ...
- windows上mysql安装
1. 下载MySQL Community Server 5.7.14 Index of /MySQL/Downloads/MySQL-Cluster-7.1 2. 解压MySQL压缩包 安装路径:E: ...
- 下载某资源文件并加载其中的所有Prefab到场景中
using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> / ...