What are the SOLID Principles?
SOLID Principles is an acronym of 5 principles in Object Oriented Design (OOD). Robert C. Martin introduced these 5 principles in his 2000 paper “Design Principles and Design Patterns”.
S – Single Responsibility Principle
O – Open Closed Principle
L – Liskov Substitution Principle
I – Interface Segregation Principle
D – Dependency Inversion Principle
Describe the Single Responsibility Principle (SRP).
In Object Oriented Programming, Single Responsibility (SRP) ensures that every module or class should be responsible for single functionality supported by the software system. In other words, Every class should have one and only reason to change it.
For example, In ASP.NET MVC Home Controller class should be responsible related to Home Page functionality of software system.
Describe the Open Close Principle (OCP).
Open Close Principle (OCP) states or ensures that A class, component or entity should be open for extension but close for modification. In detail, We can extend any class via Interface, Inheritance or Composition whenever it’s required instead of opening a class and modifying it’s code.
For example, suppose you have implemented a functionality to calculate area of Rectangle and after some time you need to calculate the area of Square, then In this case you should not modify your original class code to add extra code for square. Instead you should create one base class initially and now you should extend this base class by your square class.
Solid Principle Interview Question
Explain Liskov Substitution Principle (LSP).
Liskov Substitution Principle (LSP) states that Objects in a program can be replaced by the instances of their subtypes without modifying the correctness of a program.
In other words, if A is subtype of B then instances of B may be replaced by the instances of A without altering the program correctness.
Explain Interface Segregation Principle (ISP).
Interface Segregation Principle (ISP) states that use many client specific interfaces instead of one general purpose interface.
In other words No client should be forced to implement other methods which it does not require. It means it’s better to create a separate interface and allow your classes to implement multiple interfaces.
What is YAGNI Principle?
YAGNI principle states that “You Aren’t Gonna Need It” means implement the functionality that really you need not the functionality you think that “Just in some case we need them”.
Advance Solid Principle Interview Question
How Design Principles are different from Design Patterns?
Design Principles are the core principles which we are supposed to follow while designing any software system on any platform using any programming language. Examples of Design Principles:
Dependency on abstraction not concrete classes
Encapsulate that varies
Program to interfaces not implementations
Design Patterns are the solutions to common occurring general problems, they are not exact program that can be fit to solve your problem rather you need to customize according to your problem. Design Patterns are already invented solutions which are well tested and safe to use. Examples of Design Patterns:
Single Design Pattern: When you want only one instance of a class.
Repository Design Pattern: To separate different layers of application (Business Repository, Data Repository)
Describe the Interface Isolation Principle (IIP)
The Interface Segregation Principle (ISP) is called the Interface Segregation Principle in English. The basic definition is that clients should not rely on interfaces that it does not need. The client should only rely on the methods it uses because if an interface has several methods, it means that its implementation class must implement all interface methods, which is very bloated from the code structure.
Now we look at the next example that violates the principle of interface isolation. From the above class structure diagram, multiple users need to operate the Operation class. If User1 only needs the operation1 method, User2 only needs the operation2 method, and User3 only needs the operation3 method, then it is obvious to User1 that you should not see the two methods operation2 and operation3. The dependence, to prevent the modification of operation2 and operation3 methods in the Operation class, affect the function of User1.
Based on the principle of interface isolation, all we need to do is to reduce the definition of an extensive and complete interface. The interface to be implemented by the class should be decomposed into multiple interfaces, and then implemented according to the required function, and where the interface method is used, use the corresponding The interface type is declared, so that the caller and the object’s non-related methods can be released from the dependency relationship. To summarize, the interface isolation principle’s primary function is to control the granularity of the interface, prevent exposure to the client without relevant code and methods, ensure high cohesion of the interface, and reduce coupling with the client.
What is the Dependency inversion principle (DIP)
Dependency Inversion Principle (DIP) Dependency Inversion Principle (DIP), the basic definition are as follows:
- High-level modules should not depend on low-level modules, but should rely on abstractions together;
- Abstraction should not depend on details. Details should depend on abstractions.
- The abstract here is the interface and the abstract class, and the details are the class generated by implementing the interface or inheriting the abstract class.
How to understand that “high-level modules should not depend on low-level modules, but should rely on abstraction together”? If the high-level module depends on the low-level module, the low-level module’s change is likely to affect the top-level module, resulting in the high-level module being forced to change, which makes it difficult to reuse the high-level module.
Solid Principle Interview Question