In this blog, we will discuss about the
open-closed principle and the open-closed principle states
"software application source codes
should be open for extension but should be closed for modification."
In different word, the entity’s behavior can
be changed without changing the source code.
If module satisfy the open-closed principle
that have two benefits.
Open for extension - means module’s behaviors can be changed
Closed
of modification - it is not allowed to change. This can be achieved by using
Inheritance
Here is an example to how to implement the open-closed
principle by using inheritance in object orient programming
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
///
/// Order">
public virtual void Save(Order Order)
{
}
///
/// This method used to delete the order record from table
///
/// Order">
public void Delete(Order Order)
{
}
}
public class OrderDetails : Order
{
public string Address
{ get; set;
}
public string City
{ get; set;
}
public string ZipCode
{ get; set;
}
///
/// This method used to save details order information
///
/// Order">
public override void Save(Order Order)
{
}
}
In OrderDetails Class, we are override the
behavior Save() method of Base Class
Benefits of Open/Closed Principle:
·
Loose
coupling
·
Reduced
risk of breaking existing functionality be adding/inheriting new classes
Other Links:
Thanks for Visiting!!
No comments:
Post a Comment