SOLID design principles in C# are basic design principles
- S: Single Responsibility Principle (SRP) ---------------------------------------------------------------The Single Responsibility Principle (SRP) is one of the SOLID principles of object-oriented design. It states that a class should have only one reason to change, meaning it should have only one job or responsibility.
using System;
using System.IO;
// Report class responsible for holding report data
public class Report
{
public string Title { get; set; }
public string Content { get; set; }
}
// ReportPrinter class responsible for printing reports
public class ReportPrinter
{
public void PrintReport(Report report)
{
Console.WriteLine("Title: " + report.Title);
Console.WriteLine("Content: " + report.Content);
}
}
// ReportSaver class responsible for saving reports to files
public class ReportSaver
{
public void SaveToFile(Report report, string filePath)
{
File.WriteAllText(filePath, "Title: " + report.Title + "\nContent: " + report.Content);
}
}
class Program
{
static void Main(string[] args)
{
// Creating a report
Report report = new Report
{
Title = "Monthly Report",
Content = "This is the content of the monthly report."
};
// Printing the report
ReportPrinter printer = new ReportPrinter();
printer.PrintReport(report);
// Saving the report to a file
ReportSaver saver = new ReportSaver();
saver.SaveToFile(report, "report.txt");
Console.WriteLine("Report saved to file.");
}
}
- O: Open-closed Principle (OCP) ----------------------------------------------------------------------The Open/Closed Principle (OCP) is one of the five SOLID principles of object-oriented design. It states that a class should be open for extension but closed for modification. This means you should be able to add new functionality to a class without changing its existing code.
using System;
namespace OpenClosedPrincipleExample
{
// Define an abstract Discount class
public abstract class Discount
{
public abstract double Calculate(double amount);
}
// Implement different discount types
public class RegularDiscount : Discount
{
public override double Calculate(double amount)
{
return amount * 0.1; // 10% discount for regular customers
}
}
public class PremiumDiscount : Discount
{
public override double Calculate(double amount)
{
return amount * 0.2; // 20% discount for premium customers
}
}
// Now, the Invoice class uses the Discount abstract class
public class Invoice
{
private readonly Discount _discount;
public Invoice(Discount discount)
{
_discount = discount;
}
public double GetDiscount(double amount)
{
return _discount.Calculate(amount);
}
}
class Program
{
static void Main(string[] args)
{
double amount = 1000;
// Regular customer
Discount regularDiscount = new RegularDiscount();
Invoice regularInvoice = new Invoice(regularDiscount);
Console.WriteLine("Regular customer discount: " + regularInvoice.GetDiscount(amount));
// Premium customer
Discount premiumDiscount = new PremiumDiscount();
Invoice premiumInvoice = new Invoice(premiumDiscount);
Console.WriteLine("Premium customer discount: " + premiumInvoice.GetDiscount(amount));
}
}
}
Comments
Post a Comment