• 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');
  • 相关阅读:
    每日总结
    体温登记app(大年初一要收的作业)慢慢更,这个写完了
    2021/01/31周学习总结
    2021/01/24周学习总结
    从小工到专家
    构建之法阅读笔记
    2021/01/17周学习总结
    人月神话阅读笔记
    利用Word制作Kindle用的6寸PDF电纸书
    面试题-谈谈封装和抽象的区别(转)
  • 原文地址:https://www.cnblogs.com/zper/p/3175900.html
Copyright © 2020-2023  润新知