[{"data":1,"prerenderedAt":66},["ShallowReactive",2],{"article-27":3},{"code":4,"msg":5,"data":6,"count":14},200,"查询成功",{"id":7,"title":8,"keywords":9,"description":10,"category_id":11,"content":12,"body_html":13,"thumb_up":14,"clicks":15,"sort":14,"remark":16,"status":17,"is_open":17,"is_deleted":14,"is_top":14,"is_recommend":14,"create_time":18,"update_time":19,"image_id":20,"url":13,"member_id":14,"cate_name":21,"prev":22,"next":25,"tags":28,"words":37,"read_time":38,"comments":14,"cover":39,"relevant":40},27,"PHP设计模式之适配、策略以及数据对象映射模式","php,设计模式,适配,策略,数据对象映射","本文章讲述了php设计模式中的适配器模式、策略模式以及数据对象映射模式；以及每种模式应用实例",2,"##### 一、适配器模式\n\n1、可以将截然不同的函数接口封装成统一的API\n\n2、实际应用举例：PHP的数据库操作有mysql\u002Fmysqli\u002Fpdo三种，可以用适配器模式统一成一致。\n\n类似的场景还有cache适配器，可以将memcache\u002Fredis\u002Ffile\u002Fapc等不同的缓存函数统一成一致的接口。\n\n实例代码：\n\n```php\ninterface IDataBase{\u002F\u002F接口 统一api的方式\n   public function connect($host, $user, $pwd, $dbname);\n   public function query($sql);\n   public function close();\n}\nclass MySQL implements IDataBase{\n   protected $conn;\n   public function connect($host, $user, $pwd, $dbname){\n\t$this->conn = mysql_connect($host, $user, $pwd);\n\tmysql_select_db($dbname, $this->conn);\n   }\n   public function query($sql){\n\t$res =mysql_query($sql, $this->conn);\n\treturn $res;\n   }\n   public function close(){\n\tmysql_close();\n   }\n}\nclass MySQLis implements IDataBase{\n   protected $conn;\n   public function connect($host, $user, $pwd, $dbname){\n\t$this->conn = mysqli_connect($host, $user, $pwd, $dbname);\n   }\n   public function query($sql){\n\t$res =mysqli_query($this->conn, $sql);\n\treturn $res;\n   }\n   public function close(){\n\tmysqli_close($this->conn);\n   }\n}\nclass PDOs implements IDataBase{\n   protected $conn;\n   public function connect($host, $user, $pwd, $dbname){\n\t$this->conn = new \\PDO(\"mysql:\u002F\u002Fhost=$host; dbname=$dbname\", $user, $pwd);\n   }\n   public function query($sql){\n\t$res = $this->conn->query($sql);\n\treturn $res;\n   }\n   public function close(){\n\tunset($this->conn);\n   }\n}\n$db = new MySQLis();\n$db->connect('localhost', 'root', 'root', 'test');\n$res = $db->query('show tables');\n$db->close();\nvar_dump($res);\n```\n##### 二、策略模式：\n\n分支逻辑处理\n\n1、策略模式，将一组特定的行为和算法封装成类，以适应某些特定的上下文环境， 这种模式就是策略模式\n\n2、实际应用举例，假如一个电商网站系统，\n\n针对男性女性用户要各自跳转到不同的商品类名，并且所有广告位展示不同的广告，\n\n传统的做法是加入if...else... 判断。如果新增加一种用户类型，只需要新增加一种策略即可\n\n实例代码：\n\n```php\ninterface Strategy{\n   public function showAd();\n   public function showCategory();\n}\nclass GrilStrategy implements Strategy{\n   public function showAd(){\n\techo \"新款女装\";\n   }\n   public function showCategory(){\n\techo \"女装\";\n   }\n}\nclass BoyStrategy implements Strategy{\n   public function showAd(){\n\techo \"新款男装\";\n   }\n   public function showCategory(){\n\techo \"男装\";\n   }\n}\nclass Page{\n   protected $strategy;\n   public function index(){\n\techo \"AD:\";\n\t$this->strategy->showAd();\n\techo \"categray:\";\n\t$this->strategy->showCategory();\n   }\n   public function setStrategy($strategy){\n\t$this->strategy = $strategy;\n   }\n}\n$page = new Page();\nif (isset($_GET['gril'])) {\n   $strategy = new GrilStrategy();\n}else{\n   $strategy = new BoyStrategy();\n}\n$page->setStrategy($strategy);\n$page->index();\n```\n##### 三、数据对象映射模式\n\n1、数据对象映射模式，是将对象和数据存储映射起来，对一个对象的操作会映射为对数据存储的操作，比我们在代码中new一个对象，那么使用该模式就可以将对对象的一些操作，\n\n比如说我们设置的一些属性，它就会自动保存到数据库，跟数据库中表的一条记录对应起来。\n\n实例，在代码中实现数据对象映射模式，我们将写一个ORM类，将复杂的SQL语句映射成对象属性的操作结合使用数据对象映射模式，工厂模式，注册模式。\n\n2、对象关系映射（英语：Object Relation Mapping，简称ORM，或O\u002FRM，或O\u002FR mapping），是一种程序技术，用于实现面向对象编程语言里不同类型系统的数据之间的转换  。\n\n从效果上说，它其实是创建了一个可在编程语言里使用的--“虚拟对象数据库”。\n\n面向对象是从软件工程基本原则（如耦合、聚合、封装）的基础上发展起来的，而关系数据库则是从数学理论发展而来的，两套理论存在显著的区别。为了解决这个不匹配的现象，\n\n对象关系映射技术应运而生。\n\n简单的说：ORM相当于中继数据。\n\n示例代码：\n\n```php\nclass MySQLis{\n   protected $conn;\n   public function connect($host, $user, $pwd, $dbname){\n\t$this->conn = mysqli_connect($host, $user, $pwd, $dbname);\n   }\n   public function query($sql){\n\t$res =mysqli_query($this->conn, $sql);\n\treturn $res;\n   }\n   public function close(){\n\tmysqli_close($this->conn);\n   }\n}\nclass User{\u002F\u002F数据对象模式\n   public $id;\n   public $name;\n   public $tell;\n   protected $db;\n   public function __construct($id){\n\t$this->db = new MySQLis();\n\t$this->db->connect('localhost', 'func', 'passwd', 'test');\n\t$res = $this->db->query(\"select * from user where id = {$id} limit 1\");\n\t$data = $res->fetch_assoc();\n\t$this->id = $data['id'];\n\t\u002F*$this->id   = $data['id'];\n\t$this->name = $data['name'];\n\t$this->tell = $data['tell'];*\u002F\n   }\n   public function __destruct(){\n        $this->db->query(\"update user set name = '{$this->name}', tell = '{$this->tell}' where id= {$this->id}\");\n   }\n}\nclass Factory{\u002F\u002F工厂模式\n   public static function CreateAnimal($name){\n   }\n   public static function getUser($id){\n\t$key = \"user_{$id}\";\n\t$user = Register::get($key);\n\tif (!$user) {\n\t\t$user = new User($id);\n\t\tRegister::set($key, $user);\n\t}\n\treturn $user;\n   }\n}\nclass Register {\u002F\u002F注册器模式\n   protected static $object;\n   public static function set($alias, $object){\n\tself::$object[$alias] = $object;\n   }\n   public static function get($alias){\n\treturn self::$object[$alias];\n   }\n   public static function _unset($alias){\n \tunset(self::$object[$alias]);\n   }\n}\nclass Page{\n   public function index(){\n\t$user = Factory::getUser(1);\n\t$user->name = 'admin110';\n\t$this->test();\n\techo \"ok\";\n   }\n   public function test(){\n\t$user =  Factory::getUser(1);\n\t$user->tell = '10010';\n   }\n}\n$page = new Page();\n$page->index();\n```",null,0,610,"",1,"2019-01-17 23:01:26","2026-04-19 15:15:04",124,"PHP",{"id":23,"title":24},26,"PHP设计模式之基础设计模式",{"id":26,"title":27},28,"PHP设计模式之观察者、原型以及装饰模式",[29,31,34],{"id":30,"name":21},25,{"id":32,"name":33},47,"设计模式",{"id":35,"name":36},48,"后端",3057,7,"https:\u002F\u002Ftp.myong.top\u002Fstorage\u002Farticle\u002F69\u002F38924b4eb5e960ecdb0961eb143096.jpg",[41,46,51,56,61],{"id":42,"title":43,"create_time":44,"description":45},93,"PHP执行Python脚本解压，压缩文件夹操作","2020-08-06 14:59:05","PHP是有解压缩类文件的，但在实际项目中，用户方使用Python脚本压缩，很容易出现中文解压乱码，导致文件内容丢失的情况，本文主要介绍PHP执行Python脚本解压缩出现的问题以及解决方法",{"id":47,"title":48,"create_time":49,"description":50},92,"PHP对图片的处理","2020-07-30 15:53:38","图片处理函数功能：缩放、剪切、相框、水印、锐化、旋转、翻转、透明度、反色处理并保存历史记录的思路：当有图片有改动时自动生成一张新图片",{"id":52,"title":53,"create_time":54,"description":55},96,"PHP中字符串跟0做比较永远是true","2021-02-04 16:16:27","最近在项目中做数据对比的功能，发现一个特殊的情况，0跟任何字符串做比较永远返回true，特意了解了一下。",{"id":57,"title":58,"create_time":59,"description":60},13,"Win系统下配置PHP链接Oracle","2019-01-17 22:23:36","该文章内容主要介绍window系统下，php连接oracle的配置。",{"id":62,"title":63,"create_time":64,"description":65},103,"面向对象三大特性五大原则 + 低耦合高内聚[转载]","2021-10-31 23:18:58","面向对象的三大特性是\"封装、\"多态\"、\"继承\"，五大原则是\"单一职责原则\"、\"开放封闭原则\"、\"里氏替换原则\"、\"依赖倒置原则\"、\"接口分离原则\"。",1783431669208]