<?php
interface Ia {
public function set($k,$v);
public function get($k);
public function delete($k);
}
/**
*
*/
class Memcache implements Ia
{
public function set($k,$v)
{
echo "Memcache set function";
}
public function get($k)
{
echo "Memcache set function";
}
public function delete($k)
{
echo "Memcache set function";
}
}
/**
* 假如系统使用的是 Memcache 缓存,实现的是 一个接口类。
如果现在换成Redis,只需要在 factory 函数中替换 要执行的函数类即可。
*/
class Redis implements Ia
{
public function set($k,$v)
{
echo "redis set function";
}
public function get($k)
{
echo "redis get function";
}
public function delete($k)
{
echo "redis delete function";
}
}
/**
*
*/
class Cache
{
public static function factory()
{
return new Memcache();
return new Redis();
}
}
$cache = Cache::factory();
$cache->get('123');