Open/Closed Principle – SOLID Principles

Open/Closed Principle – SOLID Principles

Open/Closed Principle – SOLID Principles

A software module/class is open for extension and closed for modification.

Open for extension means that we must build any class/module so that new functionality can be added when there is new requirement in software.

Closed for modification means that we have already built a class, and we are done with unit testing, so we won’t change this class until and unless a bug has not been reported.

Confused??? Okay, let me explain with an example.

Suppose we are designing software for a bank. Initially the bank is starting business with Loan Account only.

Based on requirements, developer made 2 classes (One for LoanAccount and another to calculate interest)

?

And

?

Initially everything looked good and was working.

Let’s say, after 1-2 years, bank started working on retail as well and added one more account type (Saving Account)

To meet requirements, developers made changes and add one more account class.

?

And updating existing class like


By doing so, the developer was able to add one more account type successfully. Let’s say we need to add more account “Current Account”, so in same way, we can add one more class for Current account and update function Calculate Interest

But every time, when we add new account type, we need to modify “InterestCalculator” class function “CalculateInterest” which violates “Open close” principle.

To satisfy close principle, we shouldn’t modify “InterestCalculator” if we need to add more account type.

We can re-design this to meet “open for extension and close for modification” like below


We first made an interface having method defined as CalculateInterest.

?

?



?

And finally, Interest Calculator class like

?

Now, we can add as many as account types into software without modify InterestCalculator class

?

?

?

?

要查看或添加评论,请登录

Satya Prakash Chhikara的更多文章

  • Difference between RPC and REST

    Difference between RPC and REST

    Difference between RPC and REST Remote Procedure Call (RPC) and REST are two most followed architecture in API design…

  • Checksum

    Checksum

    Checksum Checksum is the outcome of running an algorithm, called a cryptographic hash function on a piece of data…

  • Proxy Server

    Proxy Server

    Proxy Server Definition A proxy server is a router that provides a gateway between users and the internet. It prevents…

  • Lambda Expressions in C#

    Lambda Expressions in C#

    Lambda Expressions in C# Lambda Expressions are shorthand writing for anonymous methods. Let’s look at anonymous method.

  • Anonymous Method with C#

    Anonymous Method with C#

    Anonymous Method As name suggests, anonymous method is a function without having a name. You can say code block with a…

  • Generic Delegates

    Generic Delegates

    Generic Delegate Before discussing generic delegates, it takes an example of delegates. Delegate is a pointer to an…

  • Multicast Delegate

    Multicast Delegate

    Multicast Delegate Multicast Delegate is a delegate which holds references of more than a function pointer having same…

  • Events, Delegates, Events Args and Event Handler in C#

    Events, Delegates, Events Args and Event Handler in C#

    Events, Delegates, Events Args and Event Handler in C# Events, delegates, events args and event handler are quite…

  • Finalize and Dispose in C#

    Finalize and Dispose in C#

    Finalize and Dispose in C# Finalize method 1. This method is used to release resources before the current object is…

  • Stack and Heap Memory in .NET

    Stack and Heap Memory in .NET

    Stack and Heap Memory in .NET Let’s try to understand what exactly happens when we declare a variable into .

社区洞察

其他会员也浏览了