php设计模式课程---6、策略模式如何使用

一、总结

一句话总结:比如代码需求,做一饭店,有南北方不同菜系,不同分店有不同的饭菜汤的需求,代码怎么设计

从饭店有特色过渡到厨师有特色(南方厨师(南方饭,南方菜,南方汤),北方厨师(北方饭,北方菜,北方汤))
利用厨师的组合实现饭店有不同特色(满足不同分店的需求)

1、传入的参数是对象,执行对象的方法如何实现?

就是普通的->执行方法: return $this->fanCreateor->fan();
 83 class FD {
84 protected $fanCreateor = null;
85 protected $caiCreateor = null;
86 protected $tangCreateor = null;
87
88 public function __construct($f,$c,$t) {
89 $this->fanCreateor = $f;
90 $this->caiCreateor = $c;
91 $this->tangCreateor = $t;
92 }
93
94 public function createFan() {
95 return $this->fanCreateor->fan();
96 }
97
98 public function createCai() {
99 return $this->caiCreateor->cai();
100 }
101
102 public function createTang() {
103 return $this->tangCreateor->tang();
104 }
105 }
106
107
108 $fd = new FD(new NorthCook() , new NorthCook() , new SouthCook);

2、编程的灵活性原则?

有什么 比 是什么  更灵活
组合 比  继承更灵活

二、策略模式如何使用

1、代码

 <?php
/*
// 做一饭店
class FanDian {
public function fan() {
return '面条';
} public function cai() {
return '炒菜';
} public function tang() {
return '蛋花汤';
}
} class SouthDian {
public function fan() {
return '大米饭';
} public function cai() {
return '烧菜+奶油';
} public function tang() {
return '海鲜汤';
}
} class BjDian {
public function fan() {
return '大米饭';
} public function cai() {
return '炒菜';
} public function tang() {
return null;
}
} $fd = new FanDian();
echo $fd->tang(); */ class NorthCook {
public function fan() {
return '面条';
} public function cai() {
return '炒菜';
} public function tang() {
return '蛋花汤';
}
} class SouthCook {
public function fan() {
return '米饭';
} public function cai() {
return '烧菜+奶油';
} public function tang() {
return '海鲜汤';
}
} class FD {
protected $fanCreateor = null;
protected $caiCreateor = null;
protected $tangCreateor = null; public function __construct($f,$c,$t) {
$this->fanCreateor = $f;
$this->caiCreateor = $c;
$this->tangCreateor = $t;
} public function createFan() {
return $this->fanCreateor->fan();
} public function createCai() {
return $this->caiCreateor->cai();
} public function createTang() {
return $this->tangCreateor->tang();
}
} $fd = new FD(new NorthCook() , new NorthCook() , new SouthCook); echo $fd->createFan() , "<br>";
echo $fd->createTang() , "<br />"; ?>
 

php设计模式课程---6、策略模式如何使用的更多相关文章

  1. 【转】设计模式 ( 十八 ) 策略模式Strategy(对象行为型)

    设计模式 ( 十八 ) 策略模式Strategy(对象行为型) 1.概述 在软件开发中也常常遇到类似的情况,实现某一个功能有多种算法或者策略,我们可以根据环境或者条件的不同选择不同的算法或者策略来完成 ...

  2. 设计模式 ( 十八 ) 策略模式Strategy(对象行为型)

    设计模式 ( 十八 ) 策略模式Strategy(对象行为型) 1.概述 在软件开发中也经常遇到类似的情况,实现某一个功能有多种算法或者策略,我们能够依据环境或者条件的不同选择不同的算法或者策略来完毕 ...

  3. 《Head First 设计模式》[01] 策略模式

    <Head First 设计模式>(点击查看详情) 1.写在前面的话 之前在列书单的时候,看网友对于设计模式的推荐里说,设计模式的书类别都大同小异,于是自己就选择了Head First系列 ...

  4. javascript设计模式--策略模式

    javascript策略模式总结 1.什么是策略模式? 策略模式的定义是:定义一系列的算法,把他们独立封装起来,并且可以相互替换. 例如我们需要写一段代码来计算员工的奖金.当绩效为a时,奖金为工资的5 ...

  5. [head first 设计模式] 第一章 策略模式

    [head first 设计模式] 第一章 策略模式 让我们先从一个简单的鸭子模拟器开始讲起. 假设有个简单的鸭子模拟器,游戏中会出现各种鸭子,此系统的原始设计如下,设计了一个鸭子超类,并让各种鸭子继 ...

  6. Python设计模式: 最佳的"策略"模式实践代码

    Python设计模式: 最佳的"策略"模式实践代码 今天抽空看了下流畅的python,发现里面介绍了不少python自带的库的使用实例,用起来非常的优雅. 平时用Python来写爬 ...

  7. [设计模式] javascript 之 策略模式

    策略模式说明 定义: 封装一系列的算法,使得他们之间可以相互替换,本模式使用算法独立于使用它的客户的变化. 说明:策略模式,是一种组织算法的模式,核心不在于算法,而在于组织一系列的算法,并且如何去使用 ...

  8. 设计模式:Strategy 策略模式 -- 行为型

    设计模式 策略模式Strategy(对象行为型) 这是几年前写的文字(转载做的笔记更准确些),发觉还是废话多了点. 其实,核心就是5.结构中的UML图 5.1 和 5.2(新增).现在看这张图就觉得一 ...

  9. 设计模式浅谈----策略模式(c#及java实现)

    一.何为策略模式 策略模式是行为型模式的一种,主要用于需要使用不同的算法来处理不同的数据对象时使用,是一种可以在运行时选择算法的设计模式.也称为政策模式. 主要解决:在有多种算法相似的情况下,使用 i ...

  10. JavaScript设计模式 Item 7 --策略模式Strategy

    1.策略模式的定义 何为策略?比如我们要去某个地方旅游,可以根据具体的实际情况来选择出行的线路. 如果没有时间但是不在乎钱,可以选择坐飞机. 如果没有钱,可以选择坐大巴或者火车. 如果再穷一点,可以选 ...

随机推荐

  1. java.lang.NoClassDefFoundError: ch/qos/logback/core/joran/spi/JoranException

    问题描述:启动tomcat服务器的时候,报找不到JoranException类的异常 原因:tomcat中没有logback-core-1.1.2.jar包 解决方法:在tomcat中的lib目录添加 ...

  2. Error in as.POSIXlt.character(x, tz, ...) :

    > sqlFetch(channel,"user")Error in as.POSIXlt.character(x, tz, ...) :   character strin ...

  3. BZOJ 2176 Strange string 最小表示法

    题目大意:给定一个串S,求最小表示法 n<=1000W,实在不敢写后缀自己主动机,就去学了最小表示法= = 记得用unsigned char不然WA= = 数据真是逗- - #include & ...

  4. AR9331出现connect-debounce failed,port 1 disabled解决方法备忘

    基于AR9331的路由器,自己画的pcb板子,居然出现这个错误,百度下,貌似有不少人遇见过这个错误,可是在改动板子前我的固件用的是没问题的.USB完美使用 改动过板子后出现这个问题! hub 1-0: ...

  5. sql生成器(含凝视)问题修复版

    接上篇http://blog.csdn.net/panliuwen/article/details/47406455 sql生成器--生成含凝视的sql语句 今天我使用自己写的sql生成器了.自我感觉 ...

  6. Configure the modules to be find by modprobe

    sudo ln -s /path/to/module.ko /lib/modules/`uname -r` sudo depmod -a #depmod will output a dependenc ...

  7. Java图形界面实战案例——实现打字母游戏

    实现打字母的游戏 这次这个案例能够说是头几次所讲的内容的一个技术汇总,主要是 运用了几大块的知识.我们先来定义一下案例的背景:在一个300*400的窗口上.有10个随机产生的字母下落,在键盘上敲击字母 ...

  8. Map集合按value的大小排序

    public static void main(String[] args) { Map<String, Integer> map = new HashMap<String, Int ...

  9. hdu 4414 Finding crosses【简单模拟】

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4414 CSUST:点击打开链接 Finding crosses Time Limit: 2000/1000 ...

  10. CSS图片居中,多余隐藏

    /*外层DIV*/ div {position: relative;overflow:hidden;width: 显示宽度px;} /*left=50%刚好在中间,margin-left=往前移动图片 ...