php设计模式 Adapter(适配器模式)
2019/6/30 15:23:43
本文主要是介绍php设计模式 Adapter(适配器模式),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
复制代码 代码如下:
<?php
/**
* 适配器模式
*
* 将一个类的接口转换成客户希望的另外一个接口,使用原本不兼容的而不能在一起工作的那些类可以在一起工作
*/
// 这个是原有的类型
class OldCache
{
public function __construct()
{
echo "OldCache construct<br/>";
}
public function store($key,$value)
{
echo "OldCache store<br/>";
}
public function remove($key)
{
echo "OldCache remove<br/>";
}
public function fetch($key)
{
echo "OldCache fetch<br/>";
}
}
interface Cacheable
{
public function set($key,$value);
public function get($key);
public function del($key);
}
class OldCacheAdapter implements Cacheable
{
private $_cache = null;
public function __construct()
{
$this->_cache = new OldCache();
}
public function set($key,$value)
{
return $this->_cache->store($key,$value);
}
public function get($key)
{
return $this->_cache->fetch($key);
}
public function del($key)
{
return $this->_cache->remove($key);
}
}
$objCache = new OldCacheAdapter();
$objCache->set("test",1);
$objCache->get("test");
$objCache->del("test",1);
这篇关于php设计模式 Adapter(适配器模式)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-30百度空间安装易优,如何去除URL中的index.php-icode9专业技术文章分享
- 2024-06-30宝塔Linux服务器隐藏index.php-icode9专业技术文章分享
- 2024-03-29env: php: no such file or directory
- 2024-03-01php foreach break
- 2024-02-26Exception参数 php-icode9专业技术文章分享
- 2023-12-30PHP文件批量上传-icode9专业技术文章分享
- 2023-12-30thinkphp6 withJoin-icode9专业技术文章分享
- 2023-12-27MagicArray:像php一样,让Go业务代码不再卷!
- 2023-11-18centos7编译安装PHP教程。
- 2023-11-18centos7编译安装phpMyAdmin教程。