vendredi 24 février 2017

OO Method responsibility

In object oriented design Im struggling to know what is the responsibility of a method.

When checking conditional statements internally etc... who should be responsible? The receiving function or the calling function? Below is very simplified example

Example 1 the "calling function"

public function purchase($product)
{
    if ($product->isInStock) {
        $this->addToCart($product);
    }
}

protected function addToCart($product)
{
    $this->cart[] = $product;
}

Example 2 the "receiving function"

public function purchase($product)
{
    $this->addToCart($product);
}

protected function addToCart($product)
{
    if ($product->isInStock) {
        $this->cart[] = $product;
    }        
}

Aucun commentaire:

Enregistrer un commentaire