<?php
abstract class Question{
protected $prompt;
protected $marker;
function __construct($prompt,Marker $marker){
$this->prompt=$prompt;
$this->marker=$marker;
}
function mark($response){
return $this->marker->mark($response);
}
}
class TextQuestion extends Question{}
class AVQuestion extends Question{}
abstract class Marker{
protected $test;
function __construct($test){
$this->test=$test;
}
abstract function mark($response);
}
class MarkLogicMarker extends Marker{
private $engine;
function __construct($test){
parent::__construct($test);
$this->engine=new MarkParse($test);
}
function mark($response){
return $this->engine->evaluate($response);
}
}
class MarkParse extends MarkLogicMarker{
private $eq;
function __construct($eq){
$this->eq=$eq;
}
function evaluate($response){
return ($this->eq + $response);
}
}
class MatchMarker extends Marker{
function mark($response){
return ($this->test==$response);
}
}
class RegexpMarker extends Marker{
function mark($response){
return (preg_match($this->test,$response));
}
}
$array=array(
new MatchMarker('five'),
new RegexpMarker("/f.ve/")
);
foreach($array as $marker){
$question=new TextQuestion('how',$marker);
$reclass=get_class($marker);
foreach(array('five','four') as $val){
if($reclass=='MatchMarker'){
if($question->mark($val)){
echo "{$val}: 两者相等 <br>";
}else{
echo "{$val}: 两者不相等 <br>";
}
}else if($reclass=='RegexpMarker'){
if($question->mark($val)){
echo "{$val}: 匹配到类型 <br>";
}else{
echo "{$val}: 未匹配到 <br>";
}
}
}
}
?>
php策略模式,可以单独调用,也可以组合调用
最新推荐文章于 2023-10-25 17:22:41 发布