装饰模式的应用
来源:互联网 发布:e52软件下载 编辑:程序博客网 时间:2024/06/04 19:33
<?php
//装饰模式
abstract class Food
{
abstract public function getDes();
abstract public function getCost();
}
//一个具体类
class Rice extends Food
{
private $des = '饭';
private $cost = 0.50;
public function getDes()
{
return $this->des;
}
public function getCost()
{
return $this->cost;
}
}
//装饰类
class Vegetables extends Food
{
public $obj;
public function __construct( Food $obj )
{
$this->obj=$obj;
}
public function getDes()
{
$this->obj->getDes();
}
public function getCost()
{
$this->obj->getCost();
}
}
//修饰类:土豆丝
class Potato extends Vegetables
{
var $des= '土豆丝';
var $cost= 6.00;
public function getDes()
{
return $this->des.$this->obj->getDes();
}
public function getCost()
{
return $this->obj->getCost() + $this->cost;
}
}
//修饰类:西红柿
class Tomato extends Vegetables
{
var $des= '西红柿';
var $cost= 5.00;
public function getDes()
{
return $this->des.$this->obj->getDes();
}
public function getCost()
{
return $this->obj->getCost() + $this->cost;
}
}
//修饰类:咸菜
class Pickle extends Vegetables
{
var $des= '咸菜';
var $cost= 3.50;
public function getDes()
{
return $this->des.$this->obj->getDes();
}
public function getCost()
{
return $this->obj->getCost() + $this->cost;
}
}
//工厂类
class FoodFactory
{
const VEGETABLES_TYPE_POTATO = 1;
const VEGETABLES_TYPE_TOMATO = 2;
const VEGETABLES_TYPE_PICKLE = 3;
var $parm = array();
var $obj = null;
public function __construct( $items )
{
$this->parm = $items;
}
public function getResult()
{
$this->obj = new Rice();
if( $this->parm )
{
foreach ( $this->parm as $item )
{
switch ( $item )
{
//土豆丝
case self::VEGETABLES_TYPE_POTATO:
$this->obj = new Potato( $this->obj );
break;
//西红柿
case self::VEGETABLES_TYPE_TOMATO:
$this->obj = new Tomato( $this->obj );
break;
//咸菜
case self::VEGETABLES_TYPE_PICKLE:
$this->obj = new Pickle( $this->obj );
break;
}
}
}
return $this->obj->getDes().":".$this->obj->getCost();
}
}
//在这里使用
//数组里面可以是1,2,3中的任意元素
//$vegetables = array();
//$vegetables = array( 1 , 2, 3 );
$vegetables = array( 1 , 2 ,3 );
//$vegetables = array( 1 , 3 );
$lunch = new FoodFactory( $vegetables );
echo $lunch->getResult();
?>
- 装饰模式的应用
- 装饰模式的应用
- 装饰者模式的应用
- Java 装饰模式的应用
- java io 装饰者模式的应用
- 装饰者模式的简单应用
- 设计模式的应用场景(9)--装饰模式
- 装饰者模式实际应用
- 装饰者模式及其应用
- 装饰者模式及其应用
- 全站压缩---(装饰模式、过滤器的应用)
- 装饰者模式在Android上的应用 之 Context
- Java实际应用中经常遇到的"装饰模式"总结
- 装饰器模式以及继承的应用场景
- Servlet API中包装类装饰模式的应用
- 装饰器模式在IO流当中的应用
- 装饰者模式的应用——InputStream
- Android设计模式应用-装饰器模式
- A + B problem II
- android system.img的解包和打包
- 一个程序员的10年工作总结
- msg2033触摸屏驱动可模拟KEY_HOME KEY_BACK KEY_MENU
- SAP定义应收应付容差组
- 装饰模式的应用
- android (4)
- 初解SSH
- 黑马学习---------------反射(构造函数)
- 黑马学习-------------------枚举
- SHSetValue 最后一个参数字节数是否要加上\0验证
- 现要拆分以下字符串到一个表:so1101111/2/32/a1/4a2/5/63/b832/8/39
- dd命令使用
- 黑马学习-------------反射(获取类的三种方式)