To understand what these principles are, I have kept them inline comments in the code. I will keep updating the code portions that will act as examples to quickly understand each of them.
The format of the code is as follows:
SOLID principles one line definitions
Pick each letter from SOLID, i.e., each principle and create separate namespace followed by partition line as a series of "=".
namespace SOLIDDesignPrinciples
{
// S: Single responsibility principle (SRP)
// A class/method should concentrate on one task - doing one thing only
// O: Open/Close principle (OCP)
// A component should be open for extension but close for change
// Change a class behavior using inheritance/polymorphism
// L: Liskov Substitution principle (LSP)
// S is derived from B then object of B can be replaced with objects of S
// I: Interface Segregation principle (ISP)
// No client should be forced to use/depend on methods it doesn't use. Keep interfaces small and cohesive
// D: Dependency Inversion principle (DIP)
// A. High level modules should not depend upon low level modules. Both should depend upon abstractions.
// B. Abstractions should not depend upon details. Details should depend upon abstractions.
// Use lots of interfaces and abstractions
class MainClass
{
public static void Main (string[] args)
{
Console.WriteLine ("Hello World!");
}
}
}
====================================================================
namespace SOLIDDesignPrinciples
{
namespace SingleResponsibilityPrinciple
{
/// <summary>
/// Single responsibility - this class will act as a single responsibility class.
/// As an example, let it be an employee class. There can be many implementations to it,
/// where in the class, in addition to keeping employee's basic info, we will calculate bonus, taxes etc.
/// Note that this class becomes complex if we calculate information related to the basic information.
/// Rather, we can have separate classes for each job.
/// We will have two classes in order to follow SRP.
/// </summary>
public class EmployeeInformation
{
public int Salary { get; set; }
public int Age { get; set; }
public string Name { get; set; }
public EmployeeInformation ()
{
}
}
public class Calculations
{
private EmployeeInformation empInfo;
public EmployeeInformation EmployeeInformation
{ get; private set; }
public Calculations (EmployeeInformation empInfo)
{
this.empInfo = empInfo;
}
public int Tax { get; private set; }
public void CalculateTax ()
{
// Implementation here
}
public int Bonus { get; private set; }
public void CalculateBonus (int percentage)
{
// Implementation here
}
}
}
}
====================================================================
The format of the code is as follows:
SOLID principles one line definitions
Pick each letter from SOLID, i.e., each principle and create separate namespace followed by partition line as a series of "=".
namespace SOLIDDesignPrinciples
{
// S: Single responsibility principle (SRP)
// A class/method should concentrate on one task - doing one thing only
// O: Open/Close principle (OCP)
// A component should be open for extension but close for change
// Change a class behavior using inheritance/polymorphism
// L: Liskov Substitution principle (LSP)
// S is derived from B then object of B can be replaced with objects of S
// I: Interface Segregation principle (ISP)
// No client should be forced to use/depend on methods it doesn't use. Keep interfaces small and cohesive
// D: Dependency Inversion principle (DIP)
// A. High level modules should not depend upon low level modules. Both should depend upon abstractions.
// B. Abstractions should not depend upon details. Details should depend upon abstractions.
// Use lots of interfaces and abstractions
class MainClass
{
public static void Main (string[] args)
{
Console.WriteLine ("Hello World!");
}
}
}
====================================================================
namespace SOLIDDesignPrinciples
{
namespace SingleResponsibilityPrinciple
{
/// <summary>
/// Single responsibility - this class will act as a single responsibility class.
/// As an example, let it be an employee class. There can be many implementations to it,
/// where in the class, in addition to keeping employee's basic info, we will calculate bonus, taxes etc.
/// Note that this class becomes complex if we calculate information related to the basic information.
/// Rather, we can have separate classes for each job.
/// We will have two classes in order to follow SRP.
/// </summary>
public class EmployeeInformation
{
public int Salary { get; set; }
public int Age { get; set; }
public string Name { get; set; }
public EmployeeInformation ()
{
}
}
public class Calculations
{
private EmployeeInformation empInfo;
public EmployeeInformation EmployeeInformation
{ get; private set; }
public Calculations (EmployeeInformation empInfo)
{
this.empInfo = empInfo;
}
public int Tax { get; private set; }
public void CalculateTax ()
{
// Implementation here
}
public int Bonus { get; private set; }
public void CalculateBonus (int percentage)
{
// Implementation here
}
}
}
}
====================================================================
No comments:
Post a Comment