SOLID PRINCIPLES

SOLID is an Acronym which makes the software design more understandable, flexible and maintainable

  • S : Single Responsibility Principle
  • O : Open Closed Principle
  • L : Liscov's Substitution Principle
  • I  : Interface Segregation Principle
  • D : Dependency Inversion Principle
Single Responsibility Principle(SRP) :
A class should have only a single responsibility or one job to do i.e. changes to only one part of the software specification should be able to affect the specification of the class
Each and every class we create or change should have single responsibility
It mean when we design a class, it should not do multiple things at a time. We need to make sure we have single task to perform which in turn enhances the readability.
Eg : If we consider that our app has to communicate with the rest API and all the other modules use that class for network communications. If we have to change the base URL, then we just need to change in that one particular class and that applies to all the modules that are utilizing it

Open Closed Principle(OCP) :
Software entities i.e. Classes & Modules should be open for extension but closed for modifications
Lets say we have a class Car and we extend it from another class called MyCar. Closed for modifications refers to making the variables of the base class and few methods private. In this way by using access modifiers we can control modifications.

Liscov's Substitution Principle(LSP) :
Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program
Every child/sub class should be a substitution to its parent class
Suppose MyCar extends Car class and we override a method called accelerate(), then we should implement it with respect to acceleration and not with respect to brake applied.
Child classes should never break the parent class type definitions
So when we try to inherit we should keep it in mind like if we are trying to access something from parent class then it should not break the behavior of it

Interface Segregation Principle(ISP) : 
Many client specific interfaces are better than one general purpose interface
A client should never be forced to implement an interface or methods that it doesn't use.
Make fine grained interface that are client specific
Assume we have a application and the view controller need some specific information. So from payload we get set of information and all information is not relevant to our view controller. In such cases we should create interfaces specific to it so that the code will be readable so that we need not bombard our view controller with more information which is not relevant

Dependency Inversion Principle(DIP) : 
Entities must depend on abstraction and not on concretions
High level modules should not depend on low level modules, both should depend on Abstractions
If our modules or components are dependent on each other, that will cause problem if we actually make use of another component in the future.

Comments

Popular posts from this blog

Android - Using KeyStore to encrypt and decrypt the data

Stack and Queue

Java Reflection API