php设计模式 Adapter php设计模式 Adapter(适配器模式)

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

php设计模式 Adapter php设计模式 Adapter(适配器模式)

  2021-03-18 我要评论
想了解php设计模式 Adapter(适配器模式)的相关内容吗,在本文为您仔细讲解php设计模式 Adapter的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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);

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们