Design Patterns Reference: Visitor

“The Visitor Design Pattern constructs distinct objects containing an algorithm that, when consumed by a parent object in a standard way, apply that algorithm to the parent object.”

 

class CD
{
public $band;
public $title;
public $price;

 

public function __construct($band, $title, $price)
{
$this->band = $band;
$this->title = $title;
$this->price = $price;
}

 

public function buy()
{
// stub
}

 

public function acceptVisitor($visitor)
{
$visitor->visitCD($this);
}
}

 

class CDVisitorLogPurchase
{
public function visitCD($cd)
{
$logline = “{$cd->title} by {$cd->band} was purchased for {$cd->price} “;
$logline .= “at ” . sdate(‘r’) . “\n”;

 

file_put_contents(‘/logs/purchases.log’, $logline, FILE_APPEND);
}
}

 

// usage
$externalBand = ‘Never Again’;
$externalTitle = ‘Waste of a Rib’;
$externalPrice = 9.99;

 

$cd = new CD($externalBand, $externalTitle, $externalPrice);
$cd->buy();
$cd->acceptVisitor(new CDVisitorLogPurchase());

 

// adding a new Visitor to accomplish a new functionality is straight forward
class CDVisitorPopulateDiscountList
{
public function visitCD($cd)
{
if ($cd->price < 10)
{
$this->_populateDiscountList($cd);
}
}

 

protected function _populateDiscountList($cd)
{
// stub connects to sqlite and logs etc…
}
}

 

// again, usage
$cd = new CD($externalBand, $externalTitle, $externalPrice);
$cd->buy();
$cd->acceptVisitor(new CDVisitorLogPurchase());
$cd->acceptVisitor(new CDVisitorPopulateDiscountList());

 

source: http://www.wiley.com/WileyCDA/WileyTitle/productCd-0470496703,descCd-DOWNLOAD.html

 

 

Leave a Reply

You must be logged in to post a comment.