[php]策略模式模式面向对象设计
2012/10/11 23:07:00 人气 3191 编程语言区文件1 stategy.php(代码中的美元符号应该是1个)
<?php
//01_staategy.php
abstract class TaxStrategy{
protected $$num;//存纳税金额
protected $$tax;//存放纳税额
abstract public function count($$num);//计算谁金额
abstract public function printtax();//打印纳税信息
}
class HighTaxstrategy extends TaxStrategy{
public function count($$num){
$$this->num = $$num;
$$this->tax = $$this->num*0.5;
return $$this->tax;
}
public function printtax(){
echo‘HighTaxstrategy::printtax()<br />‘;
echo ‘我们将提供上门服务,您的纳税金额为‘.$$this->num.‘;您应缴纳金额为:‘.$$this->tax.‘<br />‘;
}
}
class LowTaxstrategy extends TaxStrategy{
public function count($$num){
$$this->num = $$num;
$$this->tax = $$this->num*0.05;
return $$this->tax;
}
public function printtax(){
echo‘LowRateTax::notice()<br />‘;
echo ‘恭喜你!税率很低啊!,你的纳税金额为‘.$$this->num.‘;你应缴纳金额为:‘.$$this->tax.‘我们地税局期待你的到来!<br />‘;
}
}
?>
文件2 bj.php(网站原因美元符号自动变俩了)
<?php
header(‘content-type:text/html;charset=utf-8‘);
require_once ‘01_strategy.php‘;
//每个地区独立类
abstract class Tax{
protected $$num;//输入纳税金额,系统计算
protected $$strategy;//保存纳税策略
//输入纳税金额,但是按抓哦什么税率计算委托给策略类
//TaxStrategy $$strategy的理解
//有一个参数,该参数为$$strategy;并且该参数必须是TaxStrategy的类或者其子类的实例化对象
public function __construct($$num,TaxStrategy $$strategy){
$$this->num = $$num;
$$this->strategy = $$strategy;
}
public function count(){
$$tax = $$this->strategy->count($$this->num);
return $$tax;
}
public function notice(){
$$this->strategy->printtax();
}
abstract public function doSomething();//不同地区附加共不同
}
class BJTax extends Tax{
public function doSomething(){
echo ‘BJtax::doSomethng()<br />北京地税欢迎你!<br />‘;
}
}
class SHTax extends Tax{
public function doSomething(){
echo ‘SHTax::doSomethng()<br />上海地税欢迎你!<br />‘;
}
}
//北京低税率纳税示例
$$num = 3000;
$$strategy = new LowTaxstrategy();
$$tax = new BJTax($$num,$$strategy);//自动调用其父类构造函数
$$tax ->count();
$$tax ->notice();
$$tax ->doSomething();
?>
<?php
//01_staategy.php
abstract class TaxStrategy{
protected $$num;//存纳税金额
protected $$tax;//存放纳税额
abstract public function count($$num);//计算谁金额
abstract public function printtax();//打印纳税信息
}
class HighTaxstrategy extends TaxStrategy{
public function count($$num){
$$this->num = $$num;
$$this->tax = $$this->num*0.5;
return $$this->tax;
}
public function printtax(){
echo‘HighTaxstrategy::printtax()<br />‘;
echo ‘我们将提供上门服务,您的纳税金额为‘.$$this->num.‘;您应缴纳金额为:‘.$$this->tax.‘<br />‘;
}
}
class LowTaxstrategy extends TaxStrategy{
public function count($$num){
$$this->num = $$num;
$$this->tax = $$this->num*0.05;
return $$this->tax;
}
public function printtax(){
echo‘LowRateTax::notice()<br />‘;
echo ‘恭喜你!税率很低啊!,你的纳税金额为‘.$$this->num.‘;你应缴纳金额为:‘.$$this->tax.‘我们地税局期待你的到来!<br />‘;
}
}
?>
文件2 bj.php(网站原因美元符号自动变俩了)
<?php
header(‘content-type:text/html;charset=utf-8‘);
require_once ‘01_strategy.php‘;
//每个地区独立类
abstract class Tax{
protected $$num;//输入纳税金额,系统计算
protected $$strategy;//保存纳税策略
//输入纳税金额,但是按抓哦什么税率计算委托给策略类
//TaxStrategy $$strategy的理解
//有一个参数,该参数为$$strategy;并且该参数必须是TaxStrategy的类或者其子类的实例化对象
public function __construct($$num,TaxStrategy $$strategy){
$$this->num = $$num;
$$this->strategy = $$strategy;
}
public function count(){
$$tax = $$this->strategy->count($$this->num);
return $$tax;
}
public function notice(){
$$this->strategy->printtax();
}
abstract public function doSomething();//不同地区附加共不同
}
class BJTax extends Tax{
public function doSomething(){
echo ‘BJtax::doSomethng()<br />北京地税欢迎你!<br />‘;
}
}
class SHTax extends Tax{
public function doSomething(){
echo ‘SHTax::doSomethng()<br />上海地税欢迎你!<br />‘;
}
}
//北京低税率纳税示例
$$num = 3000;
$$strategy = new LowTaxstrategy();
$$tax = new BJTax($$num,$$strategy);//自动调用其父类构造函数
$$tax ->count();
$$tax ->notice();
$$tax ->doSomething();
?>
自从来到了琴剑听月,忽然找到了生活最大的乐趣!琴剑听月!我的最爱!
暂无回复!
发表回复