Skip to main content

SOLID principles in a simple way

The SOLID principles are a set of five design principles that help software developers create more maintainable, flexible, and scalable software systems. These principles were introduced by Robert C. Martin and are widely used in object-oriented programming and software design.


Single Responsibility Principle (SRP): A class should have only one reason to change, meaning it should have only one responsibility. In other words, a class should have only one job.

Example in C#:

       
        // Incorrect implementation
        class Employee
        {
            public void CalculateSalary()
            {
                // ... calculate salary logic
            }

            public void SaveToDatabase()
            {
                // ... save to database logic
            }
        }

        // Correct implementation
        class Employee
        {
            public void CalculateSalary()
            {
                // ... calculate salary logic
            }
        }

        class EmployeeRepository
        {
            public void SaveToDatabase(Employee employee)
            {
                // ... save to database logic
            }
        }


Open/Closed Principle (OCP): Software entities (classes, modules, functions, etc.) should be open for extension but closed for modification. This means you should be able to add new functionality without changing existing code.

Example in C#:


       
        // Incorrect implementation
        class Circle {
            public double Radius { get; set; }
        }

        class AreaCalculator {
            public double CalculateArea(Circle circle) {
                return Math.PI * circle.Radius * circle.Radius;
            }
           
            public double CalculateArea(Rectangle rectangle) {
                return rectangle.Width * rectangle.Height;
            }
        }

        // Correct implementation using abstraction and inheritance
        abstract class Shape {
            public abstract double CalculateArea();
        }

        class Circle : Shape {
            public double Radius { get; set; }

            public override double CalculateArea() {
                return Math.PI * Radius * Radius;
            }
        }

        class Rectangle : Shape {
            public double Width { get; set; }
            public double Height { get; set; }

            public override double CalculateArea() {
                return Width * Height;
            }
        }

Liskov Substitution Principle (LSP): Subtypes must be substitutable for their base types without affecting the correctness of the program. In other words, objects of derived classes should be able to replace objects of the base class without altering the desirable properties of the program.

Example in C#:

       
        // Incorrect implementation
        class Bird {
            public virtual void Fly() {
                // ... flying logic
            }
        }

        class Ostrich : Bird {
            public override void Fly() {
                throw new InvalidOperationException("Ostriches can't fly!");
            }
        }

        // Correct implementation
        interface IFlyable {
            void Fly();
        }

        class Bird : IFlyable {
            public void Fly() {
                // ... flying logic
            }
        }

        class Ostrich : Bird {
            // Ostrich doesn't need to override Fly() since it inherits the
            // implementation from Bird.
        }

Interface Segregation Principle (ISP): This principle states that a class should not be forced to implement interfaces it doesn't use. In other words, a client should not be forced to depend on interfaces it doesn't need.

Example in C#:

       
        // Incorrect implementation
        interface IWorker {
            void Work();
            void Eat();
        }

        class Manager : IWorker {
            public void Work() {
                // ... manager-specific work logic
            }
           
            public void Eat() {
                // ... manager-specific eat logic
            }
        }

        class Developer : IWorker {
            public void Work() {
                // ... developer-specific work logic
            }
           
            public void Eat() {
                // ... developer-specific eat logic
            }
        }

        // Correct implementation using interface segregation
        interface IWorker {
            void Work();
        }

        interface IEater {
            void Eat();
        }

        class Manager : IWorker, IEater {
            public void Work() {
                // ... manager-specific work logic
            }
           
            public void Eat() {
                // ... manager-specific eat logic
            }
        }

        class Developer : IWorker {
            public void Work() {
                // ... developer-specific work logic
            }
        }

Dependency Inversion Principle (DIP): This principle suggests that high-level modules (which provide complex logic) should not depend on low-level modules (which implement details), but both should depend on abstractions. Additionally, abstractions should not depend on details; details should depend on abstractions.

Example in C#:

       
        // Incorrect implementation
        class LightBulb {
            public void TurnOn() {
                // ... logic to turn on the light bulb
            }
           
            public void TurnOff() {
                // ... logic to turn off the light bulb
            }
        }

        class LightSwitch {
            private LightBulb _bulb;

            public LightSwitch() {
                _bulb = new LightBulb();
            }

            public void Toggle() {
                if (/* some condition */) {
                    _bulb.TurnOn();
                } else {
                    _bulb.TurnOff();
                }
            }
        }

        // Correct implementation using dependency inversion
        interface ISwitchable {
            void TurnOn();
            void TurnOff();
        }

        class LightBulb : ISwitchable {
            public void TurnOn() {
                // ... logic to turn on the light bulb
            }
           
            public void TurnOff() {
                // ... logic to turn off the light bulb
            }
        }

        class LightSwitch {
            private ISwitchable _device;

            public LightSwitch(ISwitchable device) {
                _device = device;
            }

            public void Toggle() {
                if (/* some condition */) {
                    _device.TurnOn();
                } else {
                    _device.TurnOff();
                }
            }
        }


Hope it will help

Comments

Popular posts from this blog

Interview Questions of SPFx SharePoint

What is SPFx? The SharePoint Framework (SPFx) is a web part model that provides full support for client-side SharePoint development, it is easy to integrate with SharePoint data, and extend Microsoft Teams. With the SharePoint Framework, you can use modern web technologies and tools in your preferred development environment to build productive experiences and apps that are responsive and mobile-ready. Scenario Based asking Scenario 1: Scenario: Your team is developing a SharePoint Framework (SPFx) web part that needs to retrieve data from an external API and display it on a SharePoint site. The API requires authentication using OAuth 2.0. The web part should also allow users to refresh the data manually. Question 1: How would you approach implementing this functionality in an SPFx web

Interview Question of Advanced SharePoint SPFx

1. What is WebPack? A module bundling system built on top of Node. js framework is known as a WebPack. It is capable to handle the combination and minification of JavaScript and CSS files, also other files such as images by using plugins. WebPack is the recommended way of bundling the files in JS framework and .Net frameworks. In the SPFx it is used with React Js. 2. What is PowerShell.? PowerShell is a platform introduced by Microsoft to perform cross-platform task automation and configuration management framework, it is made up of a command-line shell, a scripting language. it can be run on Windows, Linux, and macOS. 3. What is bundling and Minification? The terms bundling and minification are the processes of code compressing, which is used to improve the load and request time, It is used in modern JavaScript frameworks and .Net frameworks. It improves the load time by reducing the number of requests to the s

Top20 - SharePoint Framework (SPFx) Interview Questions

1.  Which tool we can use to generate a SharePoint Framework (SPFx) solution? Developers can use  Yeoman to generate SharePoint Framework (SPFx) solution, it is a client-side scaffolding open-source tool to help in web-based development. 2. How developer can ensure that the solution deployed was immediately available to all site collections in SPFx?  To ensure the availability of the deployed solution on all the site collections developer has to configure  skipFeatureDeployment: true  in package-solution.json {     "solution" : {       "name" : "theultimateresources-deployment-client-side-solution" ,       "id" : "as3feca4-4j89-47f1-a0e2-78de8890e5hy" ,       "version" : "2.0.0.0" ,       "skipFeatureDeployment" : true     },     "paths" : {       "zippedPackage" : "solution/theultimateresources-deploy-true.sppkg"     }  }