One thing will have only one responsibilities to do thing
It state that there should not be more than one responsibility or dependency on one thing, and It should have one reason to change.
Every object should have single responsibility, and that responsibility should be entirely encapsulated by the class – Wikipedia
There should never be more then one reason for a class to change -Robert C. “Uncle Bob” Martine
Let’s have an example, where this is violating the rule than will correct the issue.
public class Sale
{
public void ProcessOrder()
{
// Product price calculation and their quentity
// Reduce the stock or inventory
// Place the order
// Order to be assign to Customer
}
}
Here we can see there is lots of activity is happening in one place, rather it should be divided into sub module or parts.
Here we can see how we can divided this using the Interface:
public class Sale
{
private readonly IPriceCalculator priceCalculator;
private readonly IStockManager stockManager;
private readonly IPlaceOrder placeOrder;
private readonly IAssignOrdertoCustomer assignOrdertoCustomer;
public Sale(IPriceCalculator priceCalculator,
IStockManager stockManager,
IPlaceOrder placeOrder,
IAssignOrdertoCustomer assignOrdertoCustomer)
{
this.priceCalculator = priceCalculator;
this.stockManager = stockManager;
this.placeOrder = placeOrder;
this.assignOrdertoCustomer = assignOrdertoCustomer;
}
public void ProcessOrder()
{
priceCalculator.CalculatePrice();
stockManager.UpdateInventory();
placeOrder.OrderPlace();
assignOrdertoCustomer.Assign();
}
}