<?php
/**
* 策略模式实例
*
*/
//抽象策略角色《为接口或者抽象类,给具体策略类继承》
interface Strategy
{
public function computePrice($price);
}
//具体策略角色-普通会员策略类
class GenernalMember implements Strategy
{
public function computePrice($price)
{
return $price;
}
}
//具体策略角色-中级会员策略类
class MiddleMember implements Strategy
{
public function computePrice($price)
{
return $price * 0.8;
}
}
//具体策略角色-高级会员策略类
class HignMember implements Strategy
{
public function computePrice($price)
{
return $price * 0.7;
}
}
//环境角色实现类
class Price
{
//具体策略对象
private $strategyInstance;
//构造函数
public function __construct($instance)
{
$this->strategyInstance = $instance;
}
public function compute($price)
{
return $this->strategyInstance->computePrice($price);
}
}
//客户端使用
$p = new Price(new HignMember());
$totalPrice = $p->compute(100);
echo $totalPrice; //70
?>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有