<?php
abstract class Strategy
{
    public abstract function AlgorithmInterface();
}

class ConcreteStrategyA extends Strategy
{
    public function AlgorithmInterface()
    {
        // 实现细节
    }
}

class ConcreteStrategyB extends Strategy
{
    public function AlgorithmInterface()
    {
        // 实现细节
    }
}

class ConcreteStrategyC extends Strategy
{
    public function AlgorithmInterface()
    {
        // 实现细节
    }
}

/* *************************************** */
// 配置
class Context
{
    public $strategy;

public function __construct($strategy)
    {
        $this->strategy = $strategy;
    }

public function ContextInterface()
    {
        $this->strategy->AlgorithmInterface();
    }
}

// 实现过程
$context = new Context(new ConcreteStrategyA());
$context.ContextInterface();

$context = new Context(new ConcreteStrategyB());
$context.ContextInterface();

$context = new Context(new ConcreteStrategyC());
$context.ContextInterface();

/* ********************************* */
// 策略模式具体实现

abstract class CashSuper
{
    public abstract function acceptCash($money);
}

// 正常收费
class CashNormal extends CashSuper
{
    public function acceptCash($money)
    {
        return $money;
    }
}

// 打折收费
class CashRebate extends CashSuper
{
    private $moneyReturn;

public function __construct($moneyReturn)
    {
        $this->moneyReturn = $moneyReturn;
    }

public function acceptCash($money)
    {
        return $money * $this->moneyReturn;
    }
}

// 返利收费
class CashReturn extends CashSuper
{
    private $moneyCondtion;
    private $moneyReturn;
    
    function __construct($moneyCondition, $moneyReturn)
    {
        $this->moneyCondtion = $moneyCondition;
        $this->moneyReturn = $moneyReturn;
    }

public function acceptCash($money)
    {
        $result = $money;
        if($money >= $this->moneyCondtion)
            $result = $money - Math.Float($money/$this->moneyCondition) * $this->moneyReturn;
        return $result;
    }    
}

// 实现

class CashContext
{
    // 为一个构造实现一子类
    private cs;
    public function __construct($cashSuper)
    {
        $this->cs = $cashSuper;
    }

public function GetResult($money)
    {
        return cs.acceptCash($money);
    }
}
/**
    @param $flag 检测收费方式
    @param $old 正常价格
    @param $back 返回利率
    @param $condtion 正常价格
    @param $conditionback 应返回的利率
    @param $money 实现的金额
 */
$flag;
$old, $back;
$condtion, $conditionback;
$money;
switch($flag)
{
    case "normal":
        $res = new CashContext(new CashNormal());
        break;
  case "打折":
        $res = new CashContext(new CashRebate($old, $back));
        break;
    case "返利":
        $res = new CashContext(new CashReturn($conditon, $conditionback));
        break;
}
$res.acceptCash($money);

/** ********************************** */
// 策略模式与工厂模式结合,只需修改 CashContext即可

/**
    @param $flag 检测收费方式
    @param $old 正常价格
    @param $back 返回利率
    @param $condtion 正常价格
    @param $conditionback 应返回的利率
    @param $money 实现的金额
 */
// 用到时把参数修改为实际数字即可
class CashContext
{
    // 为一个构造实现一子类
    private cs;
    private $flag;
    public function __construct($flag)
    {
        $this->flag = $flag;
    }

public function GetResult($money)
    {
        switch($this->flag)
        {
            case "normal":
                $res = new CashContext(new CashNormal());
                break;
            case "打折":
                $res = new CashContext(new CashRebate($old, $back));
                break;
            case "返利":
                $res = new CashContext(new CashReturn($conditon, $conditionback));
                break;
        }
        return $res.acceptCash($money);
    }
}

$flag;
$res = new CashContext($flag);
$res.acceptCash($money);

PHP设计模式之:策略模式的更多相关文章

  1. 设计模式:策略模式(Strategy)

    定   义:它定义了算法家族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化, 不会影响到使用算法的客户. 示例:商场收银系统,实现正常收费.满300返100.打8折.......等不同收费 ...

  2. PHP设计模式之策略模式

    前提: 在软件开发中也常常遇到类似的情况,实现某一个功能有多种算法或者策略,我们可以根据环境或者条件的不同选择不同的算法或者策略来完成该功能.如查 找.排序等,一种常用的方法是硬编码(Hard Cod ...

  3. JavaScript设计模式之策略模式(学习笔记)

    在网上搜索“为什么MVC不是一种设计模式呢?”其中有解答:MVC其实是三个经典设计模式的演变:观察者模式(Observer).策略模式(Strategy).组合模式(Composite).所以我今天选 ...

  4. 乐在其中设计模式(C#) - 策略模式(Strategy Pattern)

    原文:乐在其中设计模式(C#) - 策略模式(Strategy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 策略模式(Strategy Pattern) 作者:webabc ...

  5. JavaScript设计模式之策略模式

    所谓"条条道路通罗马",在现实中,为达到某种目的往往不是只有一种方法.比如挣钱养家:可以做点小生意,可以打分工,甚至还可以是偷.抢.赌等等各种手段.在程序语言设计中,也会遇到这种类 ...

  6. 【设计模式】【应用】使用模板方法设计模式、策略模式 处理DAO中的增删改查

    原文:使用模板方法设计模式.策略模式 处理DAO中的增删改查 关于模板模式和策略模式参考前面的文章. 分析 在dao中,我们经常要做增删改查操作,如果每个对每个业务对象的操作都写一遍,代码量非常庞大. ...

  7. [design-patterns]设计模式之一策略模式

    设计模式 从今天开始开启设计模式专栏,我会系统的分析和总结每一个设计模式以及应用场景.那么首先,什么是设计模式呢,作为一个软件开发人员,程序人人都会写,但是写出一款逻辑清晰,扩展性强,可维护的程序就不 ...

  8. 设计模式入门,策略模式,c++代码实现

    // test01.cpp : Defines the entry point for the console application.////第一章,设计模式入门,策略模式#include &quo ...

  9. 设计模式之策略模式和状态模式(strategy pattern & state pattern)

    本文来讲解一下两个结构比较相似的行为设计模式:策略模式和状态模式.两者单独的理解和学习都是比较直观简单的,但是实际使用的时候却并不好实践,算是易学难用的设计模式吧.这也是把两者放在一起介绍的原因,经过 ...

  10. python设计模式之策略模式

    每次看到项目中存在大量的if else代码时,都会心生一丝不安全感. 特别是产品给的需求需要添加或者更改一种if条件时,生怕会因为自己的疏忽而使代码天崩地裂,哈哈,本文的目的就是来解决这种不安全感的, ...

随机推荐

  1. java spring一个类型split的方法

    /** * Take a String which is a delimited list and convert it to a String array. * <p>A single ...

  2. nginx配置文件的说明

    设置连接超时时间 http { ## # Basic Settings ## sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout ...

  3. 高性能MySql进化论(十一):常见查询语句的优化

    总结一下常见查询语句的优化方式 1        COUNT 1.       COUNT的作用 ·        COUNT(table.filed)统计的该字段非空值的记录行数 ·         ...

  4. SQL GROUP BY GROUPING SETS,ROLLUP,CUBE(需求举例)

    实现按照不同级别分组统计 关于GROUP BY 中的GROUPING SETS,ROLLUP,CUBE 从需求的角度理解会更加容易些. 需求举例: 假如一所学校只有两个系, 每个系有两个专业, 每个专 ...

  5. linux系统编程:进程间通信-mmap

    进程间通信-mmap #include <sys/mman.h> void *mmap(void *addr, size_t length, int prot, int flags, in ...

  6. LINUX 文件系统JBD ----深入理解Fsync

    http://www.cnblogs.com/hustcat/p/3283955.html http://www.cnblogs.com/zengkefu/p/5639200.html http:// ...

  7. unix进程的环境--unix环境高级编程读书笔记

    http://blog.csdn.net/xiaocainiaoshangxiao/article/category/1800937

  8. 注释驱动的 Spring cache 缓存介绍--转载

    概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...

  9. spring beans源码解读之--bean definiton解析器

    spring提供了有两种方式的bean definition解析器:PropertiesBeanDefinitionReader和XmLBeanDefinitionReader即属性文件格式的bean ...

  10. Android(java)学习笔记190:Eclipse中的控制台不停报错Can't bind to local 8700 for debugger

    [DDMS] Can't bind to local 8600 for debugger 改成 Under Window -> Preferences -> Android -> D ...