Answer the question
In order to leave comments, you need to log in
Is this (delegation) a violation of the Single Responsibility Principle - SRP?
Suppose I have an Order class that should be able to get a list of products from different sources, in addition to working with orders. It is logical that a separate class, ProductList
, will deal with the product storage , but to select products, this class needs to be configured, different parameters, drivers and dependencies must be set.
Now, if in the class Order , create a separate method getProductList , in which the ProductList class will be initialized , configured as necessary, this method will return a list of products according to the passed criteria.
Two questions:
1. Is this the right delegation?
2. Does it violate the principle
SRP , if the Order class can now, in addition to working with orders, receive a list of goods from different sources ?
PS: the example is not entirely successful, but the essence is the same.
Answer the question
In order to leave comments, you need to log in
If the Order object has a ProductList dependency, then it does not violate SRP, since this object (judging by the name) is a VO and the state of this object is necessary for Order to work.
But the way to work with it raises questions:
create a separate getProductList method in which the class will be initialized
class Order
{
private ProductStorageInterface $products;
public function __construct(ProductStorageInterface $products)
{
$this->products = $products;
}
public function refundProduct(Product $product): void
{
// логика возврата товара
// и соответственно изменение состава $this->products
}
}
getProductList()
, it's terrible! Call it as it is: createProductList()
or loadProductList()
, well, depending on the logic. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question