zoukankan      html  css  js  c++  java
  • php接口、工厂函数

    //图像接口
    interface img{
        public function getWidth();
        public function getHeight();
        public function getData();
    }
    
    //png图像处理
    class Img_PNG implements img{
        private $width, $height, $data;
    
        public function __construct($file){
            $this->file = $file;
            $this->parse();
        }
        public function parse(){
            echo '这里对png格式图形进行处理<br>';
        }
        public function getWidth(){
            return $this->width;
        }
        public function getHeight(){
            return $this->height;
        }
        public function getData(){
            return $this->data;
        }
    }
    //jpeg图像处理
    class Img_JPEG implements img{
        private $width, $height, $data;
    
        public function __construct($file){
            $this->file = $file;
            $this->parse();
        }
        public function parse(){
            echo '这里对JPEG格式图形进行处理<br>';
        }
        public function getWidth(){
            return $this->width;
        }
        public function getHeight(){
            return $this->height;
        }
        public function getData(){
            return $this->data;
        }
    }
    //图像工厂
    class ImgFactory{
        public static function factory($file) {
            $pathParts = pathinfo($file);
            $ext = strtolower($pathParts['extension']);
            switch($ext) {
                case 'png':
                    $ret = new Img_PNG($file);
                    break;
                case 'jpeg':
                case 'jpg':
                    $ret = new Img_JPEG($file);
                    break;
                default:
                    return false;
            }
    
            if ($ret instanceof Img) {
                return $ret;
            } else {
                return false;
            }
        }
    }
    
    
    $img1 = ImgFactory::factory('ab/cc/aa.png');
    $img2 = ImgFactory::factory('ab/cc/aa.jpeg');
    $img3 = ImgFactory::factory('ab/cc/aa.jpg');
    $img3 = ImgFactory::factory('ab/cc/aa.aaa');
  • 相关阅读:
    [BAT]用BAT自作开机后自动启动截屏软件
    [TFS]如何彻底删除TFS上的团队项目
    [GIT]如何删除Git本地仓库
    [SQL] update select 查询的结果集
    [VS]反编译_DllToC#_REFLECTOR8.5
    Docker容器开机自动启动
    公告:开通csdn博客,敬请关注!
    1018 Public Bike Management
    微信红包算法
    LRU Cache
  • 原文地址:https://www.cnblogs.com/zper/p/3175900.html
Copyright © 2011-2022 走看看