I am using Laravel and building a shopping cart.
Consider the following:
CartService:
public function __constructor(public ShippingService $shippingService)
{}
// Various standard cart methods such as add() etc
...
protected function getItems()
{
return $this->store->items;
}
protected function getShippingOptions()
{
return $this->shippingService->getOptionsForCart();
}
ShippingService
public function __constructor(public CartService $cartService)
{}
protected function getOptionsForCart()
{
if($this->cartService->getItems() === X){
return ['option1', 'option2'];
}
return ['option2'];
}
This will break because there is a circular dependency. CartService relies on ShippingService and vice versa, yet it does seem logical that these would depend on each other....
I could do something like
CartService:
public function __constructor(public ShippingService $shippingService)
{}
// Various standard cart methods such as add() etc
...
protected function getItems()
{
return $this->store->items;
}
protected function getShippingOptions()
{
return $this->shippingService->getOptionsForCart($this);
}
ShippingService
public function __constructor()
{}
protected function getOptionsForCart(CartService $cartService)
{
if($cartService->getItems() === X){
return ['option1', 'option2'];
}
return ['option2'];
}
But this feels messy.
Is there a design pattern to deal with this particular kind of scenario?
Aucun commentaire:
Enregistrer un commentaire