SRP- Principle of Single Liability

Asked

Viewed 100 times

2

In the Order class, which contain the business rules for requesting a sales order, state which of the options below violates the principle of sole liability (SRP- Single Responsibility Principle)?

  • Request.Add():
  • Order.Apply Discount();
  • Request.Save data();
  • Ordering.Reprogramming();
  • Request.Cancel();
  • 2

    I closed it wide because without knowing the requirements there’s no way to know. Note that the two answers posted interpret differently, because the question gives room for this. I could answer that SalvarDados() is that it violates and would be right from a certain point of view.

2 answers

2

SRP - Principle of Single Liability

Single Responsibility Principle (SRP), or, Single Responsibility Principle. This principle says that classes must be cohesive, that is, have a single responsibility. Classes like this tend to be more reusable, simpler, and propagate fewer changes to the rest of the system.

Therefore:

  • Item
  • Discount
  • Save Data
  • Reprogram Delivery
  • Cancel

The method Cancel is part of the request.

The method Discount is part of the request.

The method Save data is part of the application, provided that it is specifically part of the application.

The method Addendum is part of the application, provided that it is specifically part of the application.

Reprogramming viola, this method should be within the delivery class, with the related order, and not within the order.

-1

So, from my experience and the way I’ve been working, I would create 3 objects. Request, Pedidorepositorio and Pedidoservico, and so would leave a responsibility for each and would also already use the Repository standard.

Request -> Would be the model, only with properties to be saved;

   -> AdicionarItem(PedidoItem -> Seria uma outra classe para os itens e 
      ficaria como uma propriedade do tipo List na classe Pedido)

Pedidorepositorio -> Would be the object responsible for persisting in the database;

   -> SalvarDados(Pedido)
   -> Cancelar(idPedido)
   -> AplicarDesconto(idPedido, valorDesconto)
   -> ReprogramarEntrega(idPedido)

Requested -> (Layer responsible for validating the data before calling repository class, it communicates with the repository and the UI.)

   -> SalvarDados(Pedido)
   -> Cancelar(idPedido)
   -> AplicarDesconto(idPedido, valorDesconto)
   -> ReprogramarEntrega(idPedido)
  • -1 Matheus the question is about Single Responsibility Principle, so reprogrammingEntrega is not part of the request, just as Tiago answered.

  • I agree, why my class is Pedidoservico, she has the responsibility of saving the order, cancel, apply discount, reprogram the delivery of the order, this is your responsibility. So each layer has a responsibility, the repository layer to crud, the service layer to validate the data and call the repository and the requested class that is the model.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.