Open closed principle (OCP) in C-Sharp

In previous article we were learn about Single responsibility principle (SRP) the first principle of SOLID design. Today we we are going to understand second principle of SOLID pattern i.e. Open closed principle (OCP).

Today again we will take and example with problem and end article with a solution to the problem.

Lets begin with same previous class Customer. Lets add a new functionality to customer. Out customer can be Normal, Corporative and Senior. we assign them number 1,2 and 3 respectively. We will add a new property CustomerType to Customer Class. Apply new functionality to calculate interest given by bank on the basis of customer type.

//Single responsibility principle public static class ErrorLogger { public static void ErrorHandler(string error) { System.IO.File.WriteAllText(@"c:\Error.txt", error); } } //Single responsibility principle public class Customer { public int CustomerType { get; set; } public void AddCustomer() { try { // Business logic and database logic goes here. } catch (Exception ex) { ErrorLogger.ErrorHandler(ex.ToString()); } } public double getInterest(double CurrentAmt) { if (CustomerType==1) { return (CurrentAmt * 3) / 100; } else if (CustomerType == 2) { return (CurrentAmt * 2.5) / 100; } else { return (CurrentAmt * 3.5) / 100; } } }

Problem:

Here is again a big problem. Suppose again we want to add a new customer type for Student Account. Again we need to modify Customer Class Code for Adding if else condition, Calculating student interest paid by banker. Again need to test each and every previous implemented functionality for old customers for working properly as before.

So every time we need to add functionality and code testing when ever a new Customer type is created.

Solution:

According to Open closed principle (OCP), Software application program should be Open for extension but should be closed for modifications.

To follow OCP we will create one virtual method for Customer Class. Which will be open for overriding and follow SRP for CustomerTypes derived from Customer class again as follows.

//SRP public static class ErrorLogger { public static void ErrorHandler(string error) { System.IO.File.WriteAllText(@"c:\Error.txt", error); } } public class Customer { public virtual void AddCustomer() { try { // Business logic and database logic goes here. } catch (Exception ex) { ErrorLogger.ErrorHandler(ex.ToString()); } } public virtual double getInterest(double CurrentAmt) { return (CurrentAmt * 3) / 100; } } public class CoorporativeCustomer:Customer { public override void AddCustomer() { // Business logic and database logic goes here. } public override double getInterest(double CurrentAmt) { return (CurrentAmt * 3.5) / 100; } } public class SeniorCustomer : CoorporativeCustomer { public override void AddCustomer() { // Business logic and database logic goes here. } public override double getInterest(double CurrentAmt) { return (CurrentAmt * 4.5) / 100; } }

In next article, we will learn Liskov substitution principle (LSP)

If you have any query or question or topic on which, we might have to write an article for your interest or any kind of suggestion regarding this post, Just feel free to write us, by hit add comment button below or contact via Contact Us form.


Your feedback and suggestions will be highly appreciated. Also try to leave comments from your valid verified email account, so that we can respond you quickly.

 
 

{{c.Content}}

Comment By: {{c.Author}}  On:   {{c.CreatedDate|date:'dd/MM/yyyy'}} / Reply


Categories