Design patterns
책임의 사슬
수색…
책임 체인 예제 (PHP)
하나의 객체에서 호출 된 메소드는 호출을 적절하게 처리 할 수있는 객체가 발견 될 때까지 객체 체인을 위로 이동합니다. 이 특별한 예는 실험의 제목, 실험 ID 또는 실험에 사용 된 조직을 얻을 수있는 기능을 가진 과학적 실험을 사용합니다.
abstract class AbstractExperiment {
abstract function getExperiment();
abstract function getTitle();
}
class Experiment extends AbstractExperiment {
private $experiment;
private $tissue;
function __construct($experiment_in) {
$this->experiment = $experiment_in;
$this->tissue = NULL;
}
function getExperiment() {
return $this->experiment;
}
//this is the end of the chain - returns title or says there is none
function getTissue() {
if (NULL != $this->tissue) {
return $this->tissue;
} else {
return 'there is no tissue applied';
}
}
}
class SubExperiment extends AbstractExperiment {
private $experiment;
private $parentExperiment;
private $tissue;
function __construct($experiment_in, Experiment $parentExperiment_in) {
$this->experiment = $experiment_in;
$this->parentExperiment = $parentExperiment_in;
$this->tissue = NULL;
}
function getExperiment() {
return $this->experiment;
}
function getParentExperiment() {
return $this->parentExperiment;
}
function getTissue() {
if (NULL != $this->tissue) {
return $this->tissue;
} else {
return $this->parentExperiment->getTissue();
}
}
}
//This class and all further sub classes work in the same way as SubExperiment above
class SubSubExperiment extends AbstractExperiment {
private $experiment;
private $parentExperiment;
private $tissue;
function __construct($experiment_in, Experiment $parentExperiment_in) { //as above }
function getExperiment() { //same as above }
function getParentExperiment() { //same as above }
function getTissue() { //same as above }
}
Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow