安哥网络 发表于 2013-8-1 15:20:36

php设计模式 Strategy(策略模式)

<?php
/**
* 策略模式(Strategy.php)
*
* 定义一系列算法,把它们一个个封装起来,并且使它们可相互替换,使用得算法的变化可独立于使用它的客户
*
*/

//---以下是一系列算法的封闭----
interface CacheTable
{
publicfunction get($key);
publicfunction set($key,$value);
publicfunction del($key);
}

// 不使用缓存
class NoCache implements CacheTable
{
publicfunction __construct(){
echo"Use NoCache
";
    }

publicfunction get($key)
    {
returnfalse;
    }

publicfunction set($key,$value)
    {
returntrue;
    }

publicfunction del($key)
    {
returnfalse;
    }
}

// 文件缓存
class FileCache implements CacheTable
{
publicfunction __construct()
    {
echo"Use FileCache
";
// 文件缓存构造函数
    }

publicfunction get($key)
    {
// 文件缓存的get方法实现
    }

publicfunction set($key,$value)
    {
// 文件缓存的set方法实现
    }

publicfunction del($key)
    {
// 文件缓存的del方法实现
    }
}

// TTServer
class TTCache implements CacheTable
{
publicfunction __construct()
    {
echo"Use TTCache
";
// TTServer缓存构造函数
    }

publicfunction get($key)
    {
// TTServer缓存的get方法实现
    }

publicfunction set($key,$value)
    {
// TTServer缓存的set方法实现
    }

publicfunction del($key)
    {
// TTServer缓存的del方法实现
    }
}

// -- 以下是使用不用缓存的策略 ------
class Model
{
private$_cache;
publicfunction __construct()
    {
$this->_cache =new NoCache();
    }

publicfunction setCache($cache)
    {
$this->_cache =$cache;
    }
}

class UserModel extends Model
{
}

class PorductModel extends Model
{
publicfunction __construct()
    {
$this->_cache =new TTCache();
    }
}

// -- 实例一下 ---
$mdlUser=new UserModel();
$mdlProduct=new PorductModel();
$mdlProduct->setCache(new FileCache()); // 改变缓存策略
?>代码包下载:
(传统的23种模式(没有区分简单工厂与抽象工厂)
http://it.agoit.com/thread-419151-1-1.htmlphp设计模式 Interpreter(解释器模式)
http://it.agoit.com/thread-419152-1-1.htmlphp设计模式 Factory(工厂模式)
http://it.agoit.com/thread-419153-1-1.htmlphp设计模式 Facade(外观模式)
http://it.agoit.com/thread-419154-1-1.htmlphp设计模式 Decorator(装饰模式)
http://it.agoit.com/thread-419155-1-1.htmlphp设计模式 Builder(建造者模式)
http://it.agoit.com/thread-419156-1-1.htmlphp设计模式 Adapter(适配器模式)
http://it.agoit.com/thread-419157-1-1.htmlphp设计模式 Template (模板模式)
http://it.agoit.com/thread-419158-1-1.htmlphp设计模式 Command(命令模式)
http://it.agoit.com/thread-419159-1-1.htmlphp设计模式 Singleton(单例模式)
http://it.agoit.com/thread-419160-1-1.htmlphp设计模式 Observer(观察者模式)
http://it.agoit.com/thread-419161-1-1.htmlphp设计模式 Strategy(策略模式)
http://it.agoit.com/thread-419162-1-1.htmlphp设计模式 Visitor (访问者模式)
http://it.agoit.com/thread-419163-1-1.htmlphp设计模式 Memento (备忘录模式)
http://it.agoit.com/thread-419164-1-1.html php设计模式 Prototype (原型模式)
http://it.agoit.com/thread-419165-1-1.html php设计模式 Mediator (中介者模式)
http://it.agoit.com/thread-419166-1-1.html php设计模式 FlyWeight (享元模式)
http://it.agoit.com/thread-419167-1-1.html php设计模式 Chain Of Responsibility (职责链模式)
http://it.agoit.com/thread-419168-1-1.html php设计模式 Bridge (桥接模式)
http://it.agoit.com/thread-419169-1-1.html php设计模式 Proxy (代理模式)
http://it.agoit.com/thread-419170-1-1.html php设计模式 State (状态模式)
http://it.agoit.com/thread-419171-1-1.html php设计模式 Composite (组合模式)
http://it.agoit.com/thread-419172-1-1.html php设计模式 Interator (迭代器模式)
下面来自<<php设计模式>>
http://it.agoit.com/thread-419173-1-1.html php设计模式 DAO(数据访问对象模式)
http://it.agoit.com/thread-419174-1-1.html php设计模式 Delegation(委托模式)





页: [1]
查看完整版本: php设计模式 Strategy(策略模式)