php策略模式的研究
<?php
abstract class Moshi{
private $num;
public $price;
const Ted=1;
const Sed=2;
public function __construct($num,$price)
{
$this->num = $num;
$this->price = $price;
}
public function cost(){
if($this->price == self::Ted) return 30;
elseif($this->price == self::Sed) return 5*$this->num;
else { $this->price == self::Ted; return 35;}
}
public function ccc(){
if($this->price == self::Ted) return 'a';
elseif($this->price == self::Sed) return 'b';
else{ $this->price == self::Ted; return 35;}
}
}
class Aa extends Moshi{}
class Bb extends Moshi{}
var_dump(Moshi::Ted);//类中常量的调用
$a=new Aa(5,Moshi::Ted);
print "{$a->cost()}--{$a->ccc()}\n";
$b=new Aa(3,Moshi::Sed);
print "{$b->cost()}--{$b->ccc()}\n";
$c=new Aa(5,4);
print "{$c->cost()}--{$c->ccc()}--{$c->price}\n";
abstract class Moshi{
private $num;
public $price;
const Ted=1;
const Sed=2;
public function __construct($num,$price)
{
$this->num = $num;
$this->price = $price;
}
public function cost(){
if($this->price == self::Ted) return 30;
elseif($this->price == self::Sed) return 5*$this->num;
else { $this->price == self::Ted; return 35;}
}
public function ccc(){
if($this->price == self::Ted) return 'a';
elseif($this->price == self::Sed) return 'b';
else{ $this->price == self::Ted; return 35;}
}
}
class Aa extends Moshi{}
class Bb extends Moshi{}
var_dump(Moshi::Ted);//类中常量的调用
$a=new Aa(5,Moshi::Ted);
print "{$a->cost()}--{$a->ccc()}\n";
$b=new Aa(3,Moshi::Sed);
print "{$b->cost()}--{$b->ccc()}\n";
$c=new Aa(5,4);
print "{$c->cost()}--{$c->ccc()}--{$c->price}\n";
<?php
abstract class Moshi2{
private $num;
private $price;
public function __construct($num,Sget $sget)
{
$this->num = $num;
$this->price = $sget;
}
public function cost(){
return $this->price->cost($this);
}
public function ccc(){
return $this->price->charge();
}
public function gets(){
return $this->num;
}
}
class Aa extends Moshi2{}
class Bb extends Moshi2{}
abstract class Sget{
abstract function cost(Moshi2 $moshi2);
abstract function charge();
}
class Ee extends Sget{
function cost(Moshi2 $moshi2){
return $moshi2->gets() * 5;
}
function charge(){
return 'a';
}
}
class Ff extends Sget{
function cost(Moshi2 $moshi2){
return 30;
}
function charge(){
return 'b';
}
}
$bb = new Aa(4,new Ee());
print "{$bb->cost()}\n";
$les[] = new Aa(4,new Ee());
$les[] = new Bb(4,new Ff());
var_dump($les);
foreach($les as $val){
print "{$val->cost()}--";
print "{$val->ccc()}\n";
}
abstract class Moshi2{
private $num;
private $price;
public function __construct($num,Sget $sget)
{
$this->num = $num;
$this->price = $sget;
}
public function cost(){
return $this->price->cost($this);
}
public function ccc(){
return $this->price->charge();
}
public function gets(){
return $this->num;
}
}
class Aa extends Moshi2{}
class Bb extends Moshi2{}
abstract class Sget{
abstract function cost(Moshi2 $moshi2);
abstract function charge();
}
class Ee extends Sget{
function cost(Moshi2 $moshi2){
return $moshi2->gets() * 5;
}
function charge(){
return 'a';
}
}
class Ff extends Sget{
function cost(Moshi2 $moshi2){
return 30;
}
function charge(){
return 'b';
}
}
$bb = new Aa(4,new Ee());
print "{$bb->cost()}\n";
$les[] = new Aa(4,new Ee());
$les[] = new Bb(4,new Ff());
var_dump($les);
foreach($les as $val){
print "{$val->cost()}--";
print "{$val->ccc()}\n";
}