SOLID Principles

SOLID principles are used to design software which is understandable, flexible and maintainable

There are 5 design principles used to design the software

I created a project which explains all these principles. I added folders and added BEFORE/AFTER and violation classes with V as ISPVAdvancedCustomer – this means InterfaceSegregationPrincipleViolation

1. Single Responsiblity Principle: It states that each module should have only one responsibility

Look at the below code.

Here you can see, SRPBefore class has 3 responsibilites.

  1. Throwing exception if product is null
  2. SQL Query to get product
  3. Sending Email

It simply violates our SRP principle as it has 3 responsibilities.

If SQLConnection is changed to other DB types like Oracle or SMTP client is changed, it will reflect this class.

So we need a way to not depend on these things. See the below code.

Here you can see we are not depending on database types or SMTP clients as these are passed by the client. So if any DB/SMTP changes, it doesnt affect this code.

2. Open Closed Principle: It states that software entities like modules/classes are opened for extension but closed for modification

If any new requirements come, we need to add the changes easily without affecting the working code .

See below code which violates OCP

Here you can see 3 if conditions to check for a product type

if new producttype is introduced we are going to add another if condition where we are changing the working code.

To fix this, we need to inherit the product type needed

Here I created 3 product discounts like

So they inherit IProductDiscount to get discounts.

So after adding these changes, we will be passing the needed product discount by the client as

3.Liskov Substitue principle: As the name says, If X is a subtype of Y, then objects of Y type should be replaced by object of X type

See below LSP violation code

See the below after changing code.

We will be fixing this as:

4.InterfaceSegregationPrinciple: It states that Clients should not be forced to implement any methods where they dont use.

As creating interface which is too big to handle , we need to create simple and small interface where each interface serving one purpose

If you see below interface there are 3 methods to get voucher discounts

for Basic Customer, he only needs implementation of BasicDiscount method. He doesnt need to know about Itnermediate/Advanced.

See below code which violates ISP

To fix this we need to create separate intefaces for Basic/Intermediate/Advacne d customers like:

Now to fix this violation, Basic Customer client needs only Basic discount method

5.Dependency Inversion Principle: It states that high level modules should not depend on low level modules where they need to depend on abstractions. Abstractions should not depend upon details. Details should depend upon abstractions.

so to say we need an non-concrete interface/abstract class where we can’t create instance of it

See below code for DIP violation:

to fix DIP violation we need to create an interface of non concrete where we depend on abstraction

Code in my Github: https://github.com/pbndru/Phani.SOLID

Leave a comment