Q31. What are SOLID principles?
Q32. Why one should follow SOLID Principles?
Q33. Explain SRP, Single Responsibility Principle with example?
Q34. Explain ISP, Interface Segregation Principle with example?
Q35. Explain OCP, Open Close Principle with example?
--------------------------------------------------------------------------------------------------------------------------
Q31. What are SOLID principles?
Answer:
There are 5 design principles to make your software flexible and maintainable.
S - Single Responsibility PrincipleO - Open Close Principle
L - Liskov Substitution Principle
I - Interface Segregation Principle
D - Dependency Inversion Principle
--------------------------------------------------------------------------------------------------------------------------
Q32. Why one should follow SOLID Principles?
Answer:
1. Maintainability - When project matures the maintainability become difficult with time. Old developers are no longer available and documentation are longer in sync. Hence we should design a application which can be easily maintain by new developers.
2. Testability - Test driven application are very important for large scale applications.
3. Flexibility and Extensibility - During development of project at times the requriments changes completely. Hence our application must be flexible enough to handle the changes.
4. Loose coupling
--------------------------------------------------------------------------------------------------------------------------
Q33. Explain SRP, Single Responsibility Principle with example?
Answer:
Single Responsibility Principle
A class should have one, and only one, reason to change. class should be having one and only one responsibility.
Each class and module must focus only on single task at a time.
There can be as many members in a class as long as they related to single responsibility.
Example : we have user login and registration page. There are 4 activitites that can be done
1. User can login. (Login)
2. User can register. (Register)
3. User will get email notification in case of registration. (emailNotification)
4. Log the exceptions. (logs)
There could be two approach to this 1) Create a single Interface will all responsibility 2) create multiple interface keeping together only similar work
Second approach is SRP
we can create 3 Interface
Interface IUser to implement Login and Register
Interface Iemail to implement emailNotification
Interface Ilogger to implement logs
https://howtodoinjava.com/best-practices/5-class-design-principles-solid-in-java/#:~:text=SOLID%20Principles%20in%20Java%20%5Bwith%20Examples%5D%201%20Single,Segregation%20Principle.%20...%205%20Dependency%20Inversion%20Principle.%20
--------------------------------------------------------------------------------------------------------------------------
Q34. Explain ISP, Interface Segregation Principle with example?
Answer:
Interface Segregation Principle
Clients should not be forced to implement unnecassary methods which they will not use. We should have multiple interfaces than 1 fat interface.
Example : we have user login and registration page. There are 4 activitites that can be done
1. User can login. (Login)
2. User can register. (Register)
3. User will get email notification in case of registration. (emailNotification)
4. Log the exceptions. (logs)
There could be two approach to this 1) Create a single Interface will all responsibility 2) create multiple interface keeping together only similar work
Second approach is SRP
we can create 3 Interface
Interface IUser to implement Login and Register
Interface Iemail to implement emailNotification
Interface Ilogger to implement logs
https://www.youtube.com/watch?v=CWrRwC8iB30&list=PL6n9fhu94yhXjG1w2blMXUzyDrZ_eyOme&index=3
--------------------------------------------------------------------------------------------------------------------------
Q35. Explain OCP, Open Close Principle with example?
Answer:
Open Close Principle
Classes and modules are open for extension but closed for modification. Means any new functionality must be added by adding new classes and modules instead of making changes in old classes and modules.
The simplest way to implement OCP is to implement the new functionality on new derived class. This will use the concept of Virtual/ Override concept.
example. We have to calculate salary of an employee based on weather he is permanent employee or temporary employee.
1st way is to create a class and a method to calculate bonus. put if condition within bonus methods if employee is permanent/ temporary.
2nd way (OCP). Create an absctract class with bonus method in it. make it virutal. create two more classes. one of permanent employees and 2nd of temporary employees. inherit above abstract class and implement the method as per requirement.
https://youtu.be/wo06oCBuYYI
No comments:
Post a Comment