SRP- Principle of Single Responsibility [closed]

2

In the Request class, which contains the business rules for requesting a sales order, please inform which of the following options violates the principle of single responsibility (SRP)?

  • Request.AddItem ():
  • Order.ApplyDiscount ();
  • Order.SaveData ();
  • Request.ReprogramDelivery ();
  • Request.Cancel ();
asked by anonymous 02.04.2018 / 07:40

2 answers

2

SRP - Principle of Single Responsibility

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

Therefore:

  • Add Item
  • Apply Discount
  • Save Data
  • Reschedule Delivery
  • Cancel

The Cancel method is part of the order.

The ApplyDiscount method is part of the order.

The SaveData method is part of the order, as long as it is specifically in the order.

The AddItem method is part of the order, as long as it is specifically in the order.

ReprogramDelivery violates, this method should be within the delivery class, with the request related, not within the request.

    
02.04.2018 / 09:52
-1

So, from my experience and the way I've worked, I would create 3 objects. Request, RequestRepository, and ServiceOrder, and so would leave a responsibility for each one and would also use the Repository pattern.

Order - > It would be the model, with only the properties to be saved;

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

Repository request - > It would be the object responsible for persisting in the database;

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

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

   -> SalvarDados(Pedido)
   -> Cancelar(idPedido)
   -> AplicarDesconto(idPedido, valorDesconto)
   -> ReprogramarEntrega(idPedido)
    
02.04.2018 / 13:52