Wednesday, August 2, 2017

SOLID Principles - Open Closed Principle

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 { getprotected set; }

        public string OrderStatus { getprotected set; }

        public DateTime Date { getprotected set; }

        public int CustomerID { getprotected 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  { getset; }

        public string City { getset; }

        public string ZipCode { getset; }


        ///



        /// 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:

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...