Public GarageStation(IGarageUtility garageUtil) A new interface called IGarageUtility is created and gate related methods are moved to different class called GarageStationUtility.
SOLID PRINCIPLES TUTORIAL CODE
We can correctly apply SRP by refactoring of above code by introducing interface. Public void PerformService(Vehicle vehicle) The code below, violates SRP principle as it mixes open gate and close gate responsibilities with the main function of servicing of vehicle. It has 3 main functions open gate, close gate and performing service. Let’s take a scenario of Garage service station functionality. This also reduces number of bugs and improves development speed and most importantly makes developer’s life lot easier.
SOLID PRINCIPLES TUTORIAL SOFTWARE
Classes, software components and modules that have only one responsibility are much easier to explain, implement and understand than ones that give a solution for everything. The Single Responsibility Principle provides another benefit.
And it’s easy to change in class as it has single responsibility. It’s easy to verify its working as per logic defined. If a class has only a single responsibility, it is likely to be very robust. Or as it states, a class/module should have not more than one reason to change. In simple terms, a module or class should have a very small piece of responsibility in the entire application. Single Responsibility Principle is one of the five SOLID principles which guide developers as they write code or design an application. The reason is that more changes requested in the future, the more changes the class need to apply. In layman terminology, this means that a class should not be loaded with multiple responsibilities and a single responsibility should not be spread across multiple classes or mixed with other responsibilities. Martin gave this definition in his book Agile Software Development, Principles, Patterns and Practices and later republished in the C# version of the book Agile Principles, Patterns and Practices in C#. SOLID Principlesĭependency Inversion Principle (DIP) Single Responsibility Principle (SRP)ĭefinition: A class should have only one reason to change. These principles are a subset of many principles promoted by Robert C. In Object Oriented Programming (OOP), SOLID is an acronym, introduced by Michael Feathers, for five design principles used to make software design more understandable, flexible, and maintainable.
NET Design Patterns: Real-World Examples