mardi 3 février 2015

Change routing to comply with Law of demeter


I have a Task, Owner and Plan. Charge values are kept in a plan, owner is on a particular plan and task knows it's owner.


A task needs to setup it's charges based on the knowledge the owner has. Owner however needs to consult a plan to get those done. According to LOD I am not allowed to do:



class Task {

/**
* @var Owner
*/
private $owner;

public function prepareCharges() {
$plan = $this->owner->getPlan();
$plan->chargeFor(...);
}


So I am wondering if this sort of routing trick is a viable solution.



class Task {

/**
* @var Owner
*/
private $owner;

public function prepareCharges() {
$this->owner->helpMeDoTheCharges($this);
}

public function doPlanCharges(Plan $plan) {
$distanceCharge = new Money();
$plan->chargeFor('distance', $distanceCharge);

$fixedCharge = new Money();
$plan->chargeFor('fixed', $fixedCharge);
}

}


Task basically tells it's owner to push the plan to it so it can send it messages.



class Plan {

public function chargeFor($type, Money $money) {

}

}

class Owner {

private $plan;

public function helpTaskDoTheCharges(Task $task) {
$task->doPlanCharges($this->plan);
}

}




Aucun commentaire:

Enregistrer un commentaire