T
T
topuserman2020-04-21 16:20:29
OOP
topuserman, 2020-04-21 16:20:29

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

1 answer(s)
M
Maxim Fedorov, 2020-04-21
@topuserman

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

Why not in the constructor of the Order object? Or why not from external dependency (DI) to get goods at all?
class Order
{
     private ProductStorageInterface $products;

     public function __construct(ProductStorageInterface $products) 
     {
           $this->products = $products;
     }
     
     public function refundProduct(Product $product): void
     {
           // логика возврата товара 
           // и соответственно изменение  состава $this->products
     }
}

And goods can be loaded from memory, database or external API.
Here at the same time the principle of dependency inversion
UPD: Do not call the method getProductList(), it's terrible! Call it as it is: createProductList()or loadProductList(), well, depending on the logic.
get is too general and doesn't say much about what's going on. If everything around is a service with get methods, then you will have fun ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question