Design patterns Adapter Adapter Pattern (PHP)


Example

A real world example using a scientific experiment where certain routines are performed on different types of tissue. The class contains two functions by default to get the tissue or routine separately. In a later version we have then adapted it using a new class to add a function that gets both. This means we have not edited the original code and therefore do not run any risk of breaking our existing class (and no retesting).

class Experiment {
    private $routine;
    private $tissue;
    function __construct($routine_in, $tissue_in) {
        $this->routine = $routine_in;
        $this->tissue  = $tissue_in;
    }
    function getRoutine() {
        return $this->routine;
    }
    function getTissue() {
        return $this->tissue;
    }
}

class ExperimentAdapter {
    private $experiment;
    function __construct(Experiment $experiment_in) {
        $this->experiment = $experiment_in;
    }
    function getRoutineAndTissue() {
        return $this->experiment->getTissue().' ('. $this->experiment->getRoutine().')';
    }
}