This
blog will discuss about the single responsibility principle and will
demonstrates how to implement single responsibility principle in C# with
example.
A single responsibility should be entirely encapsulated by the class so there should be a single reason for making the change to a class and every behavior/function of class should have just one reason to exist.
As per Single responsibility Principle, there should be two separate classes, one to maintain order record and another to generate order report.
The
Letter 'S' of 'SOLID' stands for Single Responsibility Principle
and The Single Responsibility Principle (SRP) states
"Every
class should have a single responsibility",
In different words that
a class should have only one reason to change
A single responsibility should be entirely encapsulated by the class so there should be a single reason for making the change to a class and every behavior/function of class should have just one reason to exist.
For
good quality of code the single responsibility principle is
essential.
Here
is an example of single responsibility principle:
There
is an order class, which perform database crud operation like create, update
and delete for order entity and also generate details report for order entity
now this order class have two responsibilities:
- Update/create/delete order record from
table
- Generate detail report for Order.
public class Order
{
public int OrderID
{ get; protected set; }
public string OrderStatus
{ get; protected set; }
public DateTime Date
{ get; protected set; }
public int CustomerID
{ get; protected set; }
/// This method used to add or update order table
public void Save(Order Order)
{
}
/// This method used to update the existing order record
public void Update(Order Order)
{
}
/// This method used to delete the order record from table
public void Delete(Order Order)
{
}
/// This method used to generate Order details report
public string GenerateReport(DateTime
start, DateTime End)
{
}
}
This
class have 4 methods, first 3 methods have common purpose to maintain (create,
update or delete) record in order table but GenerateReport() method have
completely different purpose to generate OrderReportData Memory string based on
input
As per Single responsibility Principle, there should be two separate classes, one to maintain order record and another to generate order report.
- Order Class- update/create/delete
order record from table
- OrderReport Class – Generate order
details report
Here is an Order Class:
public class Order
{
public int OrderID
{ get; protected set; }
public string OrderStatus
{ get; protected set; }
public DateTime Date
{ get; protected set; }
public int CustomerID
{ get; protected set; }
/// This method used to add or update order table
public void Save(Order Order)
{
}
/// This method used to update the existing order record
public void Update(Order Order)
{
}
/// This method used to delete the order record from table
public void Delete(Order Order)
{
}
}
Here is an OrderReport Class -
generate order details report.
public class OrderReport
{
/// This method used to generate order details reports
public string GenerateOrderReport(Order order)
{
}
}
Other related posts:
Thanks for Visiting!!
No comments:
Post a Comment