Copy codeThe Code is as follows:
<? Php
/**
* Adapter Mode
*
* Convert an interface of a class to another interface that the customer wants. classes that are originally incompatible and cannot work together can work together.
*/
// This is the original type
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 );