8.2.2 使用组合 《深入PHP:面向对象、模式与实践》
2017-05-27 22:07
483 查看
<?php
abstract class Lesson {
private $duration;//
private $coststrategy;//
public function __construct($duration,CostStrategy $coststrategy){
$this->duration=$duration;
$this->coststrategy=$coststrategy;
}
public function cost(){
return $this->coststrategy->cost($this);
}
public function chargeType(){
return $this->coststrategy->chargeType();
}
public function getDuration(){
return $this->duration;
}
}
//Lesson类需要一个作为属性的CostStrategy对象。Lesson::cost()方法只调用CostStrategy::cost()--这种现实调用另一个对象的方法来执行一个请求的方式便是所谓的委托:CostStrategy 对象是Lesson对象的委托方。
class Lecture extends Lesson {}
class Senimar extends Lesson {}
abstract class CostStrategy {
abstract public function cost(Lesson $lesson);
abstract public function chargeType();
}
class TimedCostStrategy extends CostStrategy {
public function cost(Lesson $lesson){
return 5*($lesson->getDuration());
}
public function chargeType(){
return "Timed rate";
}
}
class FixedCostStrategy extends CostStrategy {
public function cost(Lesson $lesson){
return 30;
}
public function chargeType(){
return "Fixed rate";
}
}
//通过传递不同的CostStrategy对象,可以在代码运行时改变Lesson对象计算费用的方式。具有高度灵活性的代码。动态的组合及重组对象,
$lesson[]=new Lecture(4,new TimedCostStrategy());
$lesson[]=new Senimar(4,new FixedCostStrategy());
foreach($lesson as $v){
echo "{$v->cost()} {$v->chargeType()}";
echo "<br>";
}
不过多种方式动态的处理任务,导致代码的可读性降低。因为组合需要更多的对象类型,而这些类型的关系并不像在继承关系中那般固定的 可预见性,所以理解系统中类和对象的关系会有些困难。
下一节解耦:
abstract class Lesson {
private $duration;//
private $coststrategy;//
public function __construct($duration,CostStrategy $coststrategy){
$this->duration=$duration;
$this->coststrategy=$coststrategy;
}
public function cost(){
return $this->coststrategy->cost($this);
}
public function chargeType(){
return $this->coststrategy->chargeType();
}
public function getDuration(){
return $this->duration;
}
}
//Lesson类需要一个作为属性的CostStrategy对象。Lesson::cost()方法只调用CostStrategy::cost()--这种现实调用另一个对象的方法来执行一个请求的方式便是所谓的委托:CostStrategy 对象是Lesson对象的委托方。
class Lecture extends Lesson {}
class Senimar extends Lesson {}
abstract class CostStrategy {
abstract public function cost(Lesson $lesson);
abstract public function chargeType();
}
class TimedCostStrategy extends CostStrategy {
public function cost(Lesson $lesson){
return 5*($lesson->getDuration());
}
public function chargeType(){
return "Timed rate";
}
}
class FixedCostStrategy extends CostStrategy {
public function cost(Lesson $lesson){
return 30;
}
public function chargeType(){
return "Fixed rate";
}
}
//通过传递不同的CostStrategy对象,可以在代码运行时改变Lesson对象计算费用的方式。具有高度灵活性的代码。动态的组合及重组对象,
$lesson[]=new Lecture(4,new TimedCostStrategy());
$lesson[]=new Senimar(4,new FixedCostStrategy());
foreach($lesson as $v){
echo "{$v->cost()} {$v->chargeType()}";
echo "<br>";
}
不过多种方式动态的处理任务,导致代码的可读性降低。因为组合需要更多的对象类型,而这些类型的关系并不像在继承关系中那般固定的 可预见性,所以理解系统中类和对象的关系会有些困难。
下一节解耦:
相关文章推荐
- JavaScript高级程序设计之面向对象的程序设计之创建对象之组合使用构造函数模式和原型模式 第6.2.4讲笔记
- 读《深入php面向对象、模式与实践》有感(三)
- 《深入php面向对象、模式与实践》读书笔记——Clone
- 读《深入php面向对象、模式与实践》有感(一)
- 4.5 延迟静态绑定:static 关键字 《深入PHP:面向对象、模式与实践》
- 读《深入php面向对象、模式与实践》有感(二)
- 面向对象设计和过程式编程(6.2)《深入PHP:面向对象、模式与实践》
- javascript面向对象-组合使用构造函数和原型模式时在原型对象添加init函数
- JavaScript面向对象 组合使用构造函数模式和原型模式
- 读《深入php面向对象、模式与实践》有感(二)
- 读《深入PHP 面向对象、模式与实践》笔记
- 代码设计(6.1)《深入PHP:面向对象、模式与实践》
- 深入PHP面向对象、模式与实践——让面向对象编程更加灵活的模式(2)
- 读《深入php面向对象、模式与实践》有感(三)
- 读《深入php面向对象、模式与实践》有感(一)
- JavaScript面向对象 组合使用构造函数模式和原型模式
- 5.3-反射API《深入PHP:面向对象、模式与实践》
- 《深入PHP面向对象、模式与实践》学习笔记(一)
- javascript中创建自定义类型的最常见的方式_组合使用构造函数模型和原型模式,动态原型模式
- 敏捷思维: 架构设计中的方法学(7)--组合使用模式