SOLID and Design Patterns Flashcards
- What are SOLID principles?
- SOLID is an acronym for the first five object-oriented design (OOD) principles which is given by Robert C. Martin.
- SOLID stands for:
S - Single-responsibility Principle
O - Open-closed Principle
L - Liskov Substitution Principle
I - Interface Segregation Principle
D - Dependency Inversion Principle - Single-responsibility Principle states that a class should have one and only one reason to change, which means that a class should have only one job.
- Open-closed Principle states that the Objects or entities should be open for extension but closed for modification.
- Liskov Substitution Principle aims to enforce consistency so that the parent Class or its child Class can be used in the same way without any errors.
- Interface Segregation Principle aims at splitting a set of actions into smaller sets so that a Class executes ONLY the set of actions it requires.
- Dependency Inversion Principle aims at reducing the dependency of a high-level Class on the low-level Class by introducing an interface.
_Aman Shankar
Why is it important to utilise the SOLID design principles?
- SOLID is a popular set of design principles that are used in object-oriented software development developed by Robert C. Martin.
- Software becomes rigid, fragile, immobile, and viscous without good design principles. The SOLID principles were developed to combat these problematic design patterns.
- The broad goal of the SOLID principles is to reduce dependencies(loosely-coupled) so that developers can change one area of software without impacting others and also they’re intended to make designs easier to understand, maintain, and extend. Ultimately, using these design principles makes it easier for software engineers to avoid issues and build effective, and agile software.
by Sai Krishna
What is the single responsibility principle? Explain with an example.
The Single Responsibility Principle is a design principle that states that a class should have only one reason to change. In other words, a class should have only one responsibility or job to do.
This principle is one of the SOLID principles in object-oriented programming, which helps to create maintainable, scalable, and testable code.
K Dhanush
What is the open close principle?Explain with an example.
=>This principle states that objects or entities should be open for extensions and closed for modifications.
=>Changing the current behaviour of a Class will affect all the systems using that Class.
=>if you want the class to perform more functionalities ,then the ideal approach is to add the functionalities to the existing class without modifying it.
=>This principle aims to extend the class’s behaviour without changing the existing behaviour of the class,this is to avoid causing bugs whenever the class is being used.
example:
Suppose developer A needs to release an update for a library or framework and developer B wants some modification or add some feature on that then developer B is allowed to extend the existing class created by developer A but developer B is not supposed to modify the class directly
=>this principle separates the existing code from the modified code so it provides better stability, maintainability and minimizes changes as in your code.
-Mounika B
What is the Liskov substitution principle?Explain with an example.
The Liskov Substitution Principle (LSP) states, “you should be able to use any derived class instead of a parent class and have it behave in the same manner without modification.”
Example : A father is a doctor, whereas his son wants to become a cricketer. So here, the son can’t replace his father even though they belong to the same family hierarchy.
What is the interface segregation principle?Explain with an example.
-
What is the dependency inversion principle?Explain with an example.
The Dependency Inversion Principle (DIP) states that High-Level Modules/Classes should not depend on Low-Level Modules/Classes. Both should depend upon Abstractions. Secondly, Abstractions should not depend upon Details.
for example - CustomerBusinessLogic depends on the DataAccess class, so CustomerBusinessLogic is a high-level module and DataAccess is a low-level module. So, as per the first rule of DIP, CustomerBusinessLogic should not depend on the concrete DataAccess class, instead both classes should depend on abstraction.
-Yugaraj
What is ‘Don’t repeat yourself (DRY)’ Principle?Explain with an example.
The “Don’t Repeat Yourself” (DRY) principle is a fundamental principle in software development that emphasizes the importance of avoiding code duplication. This principle is closely related to the SOLID principles, which provide guidelines for creating maintainable and scalable software.
In C#, the DRY principle can be implemented in several ways, including:
Extracting common code into reusable methods or functions.
Using inheritance and polymorphism to avoid duplicating code.
Utilizing interfaces to abstract away implementation details and enable code reuse.
Employing generic types to create reusable code that can work with a variety of data types.
Applying design patterns such as the Template Method pattern or the Strategy pattern to reuse code in different contexts.
By following the DRY principle in conjunction with SOLID principles, C# developers can create more maintainable and scalable software that is easier to understand , modify,code reusability and helps to improve code quality.
Example:
public abstract class Shape {
public abstract double CalculateArea();
}
public class Rectangle : Shape {
public double Width { get; set; }
public double Height { get; set; }
public override double CalculateArea() {
return Width * Height;
}
}
public class Square : Shape {
public double Side { get; set; }
public override double CalculateArea() {
return Side * Side;
}
}
Nclass program{
public static void Main(string[] args)
{
Square s=new Square();
s.Side=5;
Console.Write(s.CalculateArea());
Rectangle r=new Rectangle(); r.Width=2; r.Height=3; Console.Write(r.CalculateArea()); } } Now, both Rectangle and Square inherit from Shape and implement the CalculateArea() method. This eliminates the code duplication and also adheres to the SOLID principle of Single Responsibility, as each class now has only one responsibility.
By : Siva Billudu
Explain KISS design principle.
What is YAGNI Principle?
What was your role in the last Project related to SOLID Principles?
Try to add individual answers on this
It’s based on your role and responsibilities assigned to you and what functionality you implemented using SOLID Principles in your project. This question is generally asked in every interview.
————– write your possible answers here————
Ans 1:
I worked as a software developer and was responsible for implementing SOLID principles in the codebase. My role was to ensure that the code was modular, extensible, and easy to maintain.
we applied the Single Responsibility Principle by designing classes that had a clear and specific responsibility and also those classes are followed Open-Close principle.
challenges we faced was implementing the Liskov Substitution Principle because it required us to carefully design the class hierarchy to ensure that all subtypes could be substituted for their base types and also need to ensure that the Interface Segregation Principle was followed, so that clients were not forced to depend on methods they did not use.
At last we need take care of no higher level modules should not depend on lower level modules.
by - Bhargav Bhat
Ans 2
by - yourname
Ans 3
by - yourname
What are design patterns?
How are design patterns categorised?
Types of Design Patterns
Gang of Four have categorised the design patterns in to 3 types based on different problems encountered in the real world applications. They are Creational, Structural and Behavioural.
Creational design patterns: These patterns deal with object creation and initialization. Creational pattern gives the program more flexibility in deciding which objects need to be created for a given case.
Examples of Creational design patterns category : Singleton , Factory and Abstract Factory etc.
Structural design patterns : This pattern deals with class and object composition. In simple words, This pattern focuses on decoupling interface, implementation of classes and its objects.
Examples of Structural design patterns category : Adapter, Facade and Bridge etc.
Behavioural design patterns : These patterns deal with communication between Classes and objects.
Examples of Behavioural design patterns : Chain of Responsibility, Command and Interpreter etc.
-sohail
Describe the factory pattern.
Describe the singleton pattern along with its advantages and disadvantages.
The Singleton pattern is a creational design pattern that restricts the instantiation of a class to a single object. This means that only one instance of a class can be created and used throughout the lifetime of an application.
Advantages:
1. Controlled Access - Since only one instance of the Singleton class exists, it provides a single point of access for all clients. This can be useful in situations where you want to restrict access to a resource or service.
2. Consistency: Because the Singleton object is shared across the application, it ensures that the same configuration or data is used consistently throughout the system.
3. Memory management: The Singleton pattern can help manage memory usage by ensuring that objects are not unnecessarily duplicated.
Disadvantages:
1. The Singleton pattern can introduce a global state into an application, which can make it difficult to test and maintain.
2. This pattern reduces the potential for parallelism within a program because to access the singleton in a multi-threaded system, an object must be serialized
- Bhargav Bhat