Learn SOLID Principles in Arabic

ู‡ู†ุชูƒู„ู… ุนู† ุงู„ู€ SOLID principles ูˆู‡ู†ุดูˆู ุฅุฒุงูŠ ุงู„ู…ุจุงุฏุฆ ุฏูŠ ุจุชุณุงุนุฏู†ุง ู†ุญุณู† ุฌูˆุฏุฉ ุงู„ู€ software application ุงู„ู„ูŠ ุจู†ุจู†ูŠู‡. ุงู„ู€ SOLID principles ุจุชูˆูุฑู„ู†ุง ุดูˆูŠุฉ ู‚ูˆุงุนุฏ ุจุชุฎู„ูŠ ุงู„ู€ software ุจุชุงุนู†ุง:

  • ุงู„ู€ robust: ูŠุนู†ูŠ ู‚ูˆูŠ ูˆู…ุชูŠู†.
  • ุงู„ู€ maintainable: ูŠุนู†ูŠ ุณู‡ู„ ู†ุนุฏู„ ููŠู‡ ูˆู†ุตู„ุญู‡.
  • ุงู„ู€ reusable: ูŠุนู†ูŠ ู†ู‚ุฏุฑ ู†ุณุชุฎุฏู… ุฃุฌุฒุงุก ู…ู†ู‡ ุชุงู†ูŠ ููŠ ู…ุดุงุฑูŠุน ุชุงู†ูŠุฉ.

ุงู„ู€ SOLID Principles ุฏูŠ ุนุจุงุฑุฉ ุนู† ุฎู…ุณ ู…ุจุงุฏุฆ ุฃุณุงุณูŠุฉุŒ ูˆูƒู„ ุญุฑู ููŠ ูƒู„ู…ุฉ SOLID ุจูŠุฑู…ุฒ ู„ู…ุจุฏุฃ ู…ู†ู‡ู…. ูŠู„ุง ุจูŠู†ุง ู†ุดูˆู ุงู„ู…ุจุงุฏุฆ ุฏูŠ ูˆุงุญุฏุฉ ูˆุงุญุฏุฉ ูˆุจุฃู…ุซู„ุฉ ุนู…ู„ูŠุฉ.

1. Single Responsibility Principle - SRP

ุญุฑู S ููŠ SOLID ุจูŠุฑู…ุฒ ู„ู„ู€ Single Responsibility Principle ุฃูˆ ู…ุจุฏุฃ ุงู„ู…ุณุคูˆู„ูŠุฉ ุงู„ูˆุงุญุฏุฉ.

ูŠุนู†ูŠ ุฅูŠู‡ Single ResponsibilityุŸ

ุจุจุณุงุทุฉุŒ ุงู„ู€ principle ุฏู‡ ุจูŠู‚ูˆู„ ุฅู†: โ€œุงู„ู€ class ุงู„ู…ูุฑูˆุถ ูŠูƒูˆู† ู„ูŠู‡ุง ุณุจุจ ูˆุงุญุฏ ุจุณ ู„ู„ุชุบูŠูŠุฑ.โ€

ูŠุนู†ูŠ ุฅูŠู‡ ุงู„ูƒู„ุงู… ุฏู‡ุŸ ูŠุนู†ูŠ ู…ูŠู†ูุนุด ูŠูƒูˆู† ุนู†ุฏูƒ Class ูˆุงุญุฏุฉ ุจุชุนู…ู„ ุฃูƒุชุฑ ู…ู† ุญุงุฌุฉ ููŠ ู†ูุณ ุงู„ูˆู‚ุช. ู…ุด ู…ุญุชุงุฌูŠู† multi-tasking ู‡ู†ุง. ู„ูˆ ุงู„ู€ class ุจุชุนู…ู„ ุฃูƒุชุฑ ู…ู† ุญุงุฌุฉุŒ ู‡ูŠุฌูŠู„ูƒ ูˆู‚ุช ู„ูˆ ุญุงุฌุฉ ู…ู†ู‡ู… ุจุงุธุช ุฃูˆ ุญุจูŠุช ุชุนุฏู„ู‡ุงุŒ ู‡ุชุถุทุฑ ุชุนุฏู„ ููŠ ุงู„ู€ class ูƒู„ู‡ุงุŒ ูˆุฏู‡ ู…ู…ูƒู† ูŠุจูˆุธ ุญุงุฌุงุช ุชุงู†ูŠุฉ ู…ุนุชู…ุฏุฉ ุนู„ูŠู‡ุง ุฌูˆู‡ ู†ูุณ ุงู„ู€ class ู„ูˆ ููŠู‡ complex logic ุจูŠุฑุจุทู‡ู….

  • ุงู„ุฎู„ุงุตุฉ: ู„ุงุฒู… ู†ูุตู„ ุงู„ู…ุณุคูˆู„ูŠุงุช. ูƒู„ class ุชูƒูˆู† ู…ุณุคูˆู„ุฉ ุนู† ุญุงุฌุฉ ูˆุงุญุฏุฉ ุจุณุŒ ูˆุจุงู„ุชุงู„ูŠ ูŠูƒูˆู† ู„ูŠู‡ุง ุณุจุจ ูˆุงุญุฏ ุจุณ ูŠุฎู„ูŠู†ุง ู†ุบูŠุฑ ููŠู‡ุง. โ€œู„ุง ู„ู„ู…ุงู„ุชูŠ ุชุงุณูƒูŠู†ุฌ ููŠ ุงู„ุฃูˆุจุฌูƒุช ุฃูˆุฑูŠู†ุชุฏ ุณูˆูุช ูˆูŠุฑโ€.
  • ู†ูุณ ุงู„ู…ูู‡ูˆู… ู„ู„ู€ functions: ู„ูˆ ุนู†ุฏูƒ Function ุจุชุนู…ู„ ุฃูƒุชุฑ ู…ู† ุญุงุฌุฉุŒ ุญุงูˆู„ ุชู‚ุณู…ู‡ุง ู„ู€ functions ุฃุตุบุฑุŒ ูƒู„ ูˆุงุญุฏุฉ ุจุชุนู…ู„ ู…ู‡ู…ุฉ ู…ุญุฏุฏุฉ. ู…ุงูŠู†ูุนุด ู…ุซู„ุงู‹ function ุชุถูŠู user ูˆููŠ ู†ูุณ ุงู„ูˆู‚ุช ุชุจุนุชู„ู‡ notification ุฃูˆ email. ู„ุฃุŒ ูุตู„ ุฅุฑุณุงู„ ุงู„ู€ email ููŠ function ู„ูˆุญุฏู‡ุงุŒ ูˆุฎู„ูŠ ุงู„ู€ function ุจุชุงุนุฉ ุฅุถุงูุฉ ุงู„ู€ user ู…ุฎุตุตุฉ ู„ุฅุถุงูุฉ ุงู„ู€ users ูˆุจุณ.

ู…ุซุงู„: ุฎุฏู…ุฉ ุงู„ุฅูŠู…ูŠู„ุงุช (EmailingService)

ุชุฎูŠู„ ุนู†ุฏู†ุง EmailingService ุจุชุนู…ู„ ุงู„ุขุชูŠ:

  1. ุงู„ู€ SendEmail(): ุจุชุจุนุช ุฅูŠู…ูŠู„ (ู…ู…ูƒู† ูŠูƒูˆู† ููŠู‡ุง ุงู„ู€ logic ุจุชุงุน ุงู„ู€ SMTP server).
  2. ุงู„ู€ LoadEmailTemplate(): ุจุชุญู…ู„ ุดูƒู„ ุงู„ุฅูŠู…ูŠู„ (ุงู„ู€ template).
  3. ุงู„ู€ UpdateEmailTemplate(): ุจุชุนุฏู„ ููŠ ุงู„ู€ template.

ุงู„ูƒูˆุฏ ู‚ุจู„ ุชุทุจูŠู‚ SRP (ู…ุซุงู„ ุณูŠุก):

// Assume this class does too much
public class EmailingServiceBeforeSRP
{
    public void SendEmail(string recipient, string subject, string body)
    {
        // Logic to configure SMTP and send email
        Console.WriteLine($"C#: Email sent to {recipient} with subject: {subject}.");
    }
 
    public string LoadEmailTemplate(string templateName)
    {
        // Logic to load template from file or DB
        Console.WriteLine($"C#: Loading template: {templateName}.");
        return $"Template content for {templateName}";
    }
 
    public void UpdateEmailTemplate(string templateName, string newContent)
    {
        // Logic to update template
        Console.WriteLine($"C#: Updating template: {templateName}.");
    }
}

ุงู„ู…ุดูƒู„ุฉ ู‡ู†ุง ุฅูŠู‡ุŸ ุงู„ู€ EmailingServiceBeforeSRP ุฏูŠ ุจุชุนู…ู„ ุฃูƒุชุฑ ู…ู† ุญุงุฌุฉ:

  • ู„ูˆ ุนุงูŠุฒ ุฃุบูŠุฑ ููŠ ุทุฑูŠู‚ุฉ ุฅุฑุณุงู„ ุงู„ุฅูŠู…ูŠู„ุงุช (ุงู„ู€ SMTP configuration)ุŒ ู‡ุนุฏู„ ููŠ ุงู„ู€ class ุฏูŠ.
  • ู„ูˆ ุนุงูŠุฒ ุฃุบูŠุฑ ููŠ ุดูƒู„ ุงู„ุฅูŠู…ูŠู„ (ุงู„ู€ templateุŒ ุณูˆุงุก ูƒุงู† plain text ุฃูˆ HTML rendered template ุฒูŠ ุงู„ู€ newsletter)ุŒ ุจุฑุถู‡ ู‡ุนุฏู„ ููŠ ู†ูุณ ุงู„ู€ class.

ูƒุฏู‡ ู…ุจู‚ุงุด ููŠู‡ โ€œุณุจุจ ูˆุงุญุฏ ู„ู„ุชุบูŠูŠุฑโ€ุŒ ุจู‚ู‰ ููŠู‡ ุฃูƒุชุฑ ู…ู† ุณุจุจ. ุฏู‡ ุจู†ุณู…ูŠู‡ violation ู„ู„ู…ุจุฏุฃ.

ุงู„ุญู„ ุฅูŠู‡ุŸ ู†ูุตู„ ูƒู„ ุญุงุฌุฉ ู„ูˆุญุฏู‡ุง:

  1. ุงู„ู€ EmailSender: ู‡ูˆ class ู…ุณุคูˆู„ุฉ ุจุณ ุนู† ุฅุฑุณุงู„ ุงู„ุฅูŠู…ูŠู„ุงุช (ููŠู‡ุง ุงู„ู€ SMTP logic).
// Responsible only for sending emails
public class EmailSender
{
	public void SendEmail(string recipient, string subject, string body)
	{
		// SMTP logic to send email
		Console.WriteLine($"C#: EmailSender - Email sent to {recipient} with subject: {subject}.");
	}
}
  1. ุงู„ู€ EmailTemplateManager: ู‡ูˆ class ู…ุณุคูˆู„ุฉ ุนู† ุชุญู…ูŠู„ ูˆุชุนุฏูŠู„ ุงู„ู€ templates.
// Responsible only for template management
public class EmailTemplateManager
{
	public string LoadEmailTemplate(string templateName)
	{
		// Logic to load template
		Console.WriteLine($"C#: EmailTemplateManager - Loading template: {templateName}.");
		return $"Template content for {templateName}";
	}
 
	public void UpdateEmailTemplate(string templateName, string newContent)
	{
		// Logic to update template
		Console.WriteLine($"C#: EmailTemplateManager - Updating template: {templateName}.");
	}
}

ุงู„ูุงูŠุฏุฉ ู…ู† ูƒุฏู‡ ุฅูŠู‡ุŸ

  • ุงู„ู€ classes ุจู‚ุช ุฃุตุบุฑ ูˆุฃุณู‡ู„ ููŠ ุงู„ูู‡ู… ูˆุงู„ู‚ุฑุงุกุฉ (more readable).
  • ุจู‚ุช ุฃุณู‡ู„ ููŠ ุฅุนุงุฏุฉ ุงู„ุงุณุชุฎุฏุงู… (reusability). ูŠุนู†ูŠ ู…ู…ูƒู† ุฃุณุชุฎุฏู… EmailSender ู„ูˆุญุฏู‡ ู…ู† ุบูŠุฑ ู…ุง ุฃูƒูˆู† ู…ุฑุจูˆุท ุจุงู„ู€ template management.

ู…ุซุงู„ ุนู…ู„ูŠ: OrderManager

ู„ุชูˆุถูŠุญ ุงู„ููƒุฑุฉุŒ ู‡ู†ูุชุฑุถ ูˆุฌูˆุฏ ุงู„ูƒู„ุงุณุงุช ุงู„ุจุณูŠุทุฉ ุฏูŠ:

public class Order // Helper class for the example
{
    public int OrderId { get; set; }
    public decimal TotalAmount { get; set; }
    // Other order properties...
}
 
public class PaymentDetails // Helper class for the example
{
    public string PaymentMethod { get; set; } // e.g., "Visa", "Mastercard"
    // Other payment details...
}

ุชุฎูŠู„ ุนู†ุฏู†ุง class ุงุณู…ู‡ุง OrderManagerBeforeSRP ุจุชุนู…ู„ ุงู„ุขุชูŠ:

  • ุงู„ู€ ProcessOrder(Order order): ุจุชุนุงู„ุฌ ุงู„ุทู„ุจ.
  • ุงู„ู€ ProcessPayment(Order order, PaymentDetails details): ุจุชุนุงู„ุฌ ุนู…ู„ูŠุฉ ุงู„ุฏูุน.
  • ุงู„ู€ SendEmailNotification(Order order, String message): ุจุชุจุนุช ุฅูŠู…ูŠู„ ุชุฃูƒูŠุฏ.

ุงู„ูƒูˆุฏ ู‚ุจู„ ุชุทุจูŠู‚ ุงู„ู€ SRP (ู…ุซุงู„ ุณูŠุก):

public class OrderManagerBeforeSRP
{
    public void ProcessOrder(Order order)
    {
        Console.WriteLine($"C#: OrderManagerBeforeSRP - Processing order: {order.OrderId}.");
        // Complex order processing logic
    }
 
    public void ProcessPayment(Order order, PaymentDetails details)
    {
        Console.WriteLine($"C#: OrderManagerBeforeSRP - Processing payment for order {order.OrderId} via {details.PaymentMethod}.");
        // Payment gateway integration, validation, etc.
    }
 
    public void SendEmailNotification(Order order, string message)
    {
        Console.WriteLine($"C#: OrderManagerBeforeSRP - Sending email for order {order.OrderId}: {message}.");
        // Email sending logic
    }
}

ุงู„ู…ุดูƒู„ุฉ: ุงู„ู€ OrderManagerBeforeSRP ุฏู‡ ุจูŠุนู…ู„ ุชู„ุงุช ุญุงุฌุงุช ู…ุฎุชู„ูุฉุŒ ูˆุฏู‡ violation ู„ู„ู€ SRP. ู„ูˆ ุญุตู„ ุชุบูŠูŠุฑ ููŠ ู†ุธุงู… ุงู„ู€ notificationsุŒ ุฃูˆ ููŠ ุทุฑูŠู‚ุฉ ุงู„ุฏูุนุŒ ุฃูˆ ููŠ ุทุฑูŠู‚ุฉ ู…ุนุงู„ุฌุฉ ุงู„ุทู„ุจุงุชุŒ ูƒู„ ู…ุฑุฉ ู‡ู†ูŠุฌูŠ ู†ุนุฏู„ ููŠ ู†ูุณ ุงู„ู€ class.

ุงู„ุญู„ (ุชุทุจูŠู‚ ุงู„ู€ SRP):

  1. ุงู„ู€ OrderProcessingService: ู…ุณุคูˆู„ุฉ ุนู† ู…ุนุงู„ุฌุฉ ุงู„ุทู„ุจุงุช ูู‚ุท.
// Handles only order processing logic
public class OrderProcessingService
{
	public void ProcessOrder(Order order)
	{
		Console.WriteLine($"C#: OrderProcessingService - Processing order: {order.OrderId}.");
		// Complex order processing logic
	}
}
  1. ุงู„ู€ PaymentProcessingService: ู…ุณุคูˆู„ุฉ ุนู† ู…ุนุงู„ุฌุฉ ุงู„ุฏูุน ูู‚ุท.
// Handles only payment processing
public class PaymentProcessingService // Renamed from PaymentService for clarity
{
	public void ProcessPayment(Order order, PaymentDetails details)
	{
		Console.WriteLine($"C#: PaymentProcessingService - Processing payment for order {order.OrderId} via {details.PaymentMethod}.");
		// Payment gateway integration, validation, etc.
	}
}
  1. ุงู„ู€ NotificationSendingService: ู…ุณุคูˆู„ุฉ ุนู† ุฅุฑุณุงู„ ุงู„ุฅุดุนุงุฑุงุช ูู‚ุท.
// Handles only notifications
public class NotificationSendingService // Renamed from NotificationService for clarity
{
	public void SendEmailNotification(Order order, string message)
	{
		Console.WriteLine($"C#: NotificationSendingService - Sending email for order {order.OrderId}: {message}.");
		// Email sending logic
	}
}

ุจูƒุฏู‡ุŒ ูƒู„ class ุจู‚ุช ู…ุณุคูˆู„ุฉ ุนู† ุญุงุฌุฉ ูˆุงุญุฏุฉ ุจุณุŒ ูˆูŠูƒูˆู† ู„ูŠู‡ุง ุณุจุจ ูˆุงุญุฏ ู„ู„ุชุบูŠูŠุฑ:

  • ุงู„ู€ NotificationSendingService: ุฃูŠ ุชุบูŠูŠุฑ ููŠ ู†ุธุงู… ุงู„ุฅุดุนุงุฑุงุช ู‡ูŠุชู… ู‡ู†ุง. ู†ู‚ุฏุฑ ู†ุณุชุฎุฏู…ู‡ุง ููŠ ุฃูŠ ุญุชุฉ ููŠ ุงู„ู€ application.
  • ุงู„ู€ PaymentProcessingService: ุฃูŠ ุชุนุฏูŠู„ ููŠ ุทุฑู‚ ุงู„ุฏูุน ู‡ูŠุชู… ู‡ู†ุง.
  • ุงู„ู€ OrderProcessingService: ุฃูŠ ุชุนุฏูŠู„ ููŠ ุทุฑูŠู‚ุฉ ู…ุนุงู„ุฌุฉ ุงู„ุทู„ุจุงุช ู‡ูŠุชู… ู‡ู†ุง.

ูˆุฏู‡ ู‡ูˆ ุงู„ู€ Single Responsibility Principle.

2. Open-Closed Principle - OCP

ุญุฑู O ููŠ SOLID ุจูŠุฑู…ุฒ ู„ู„ู€ Open/Closed Principle.

ูŠุนู†ูŠ ุฅูŠู‡ Open/ClosedุŸ

ุงู„ู…ุจุฏุฃ ุฏู‡ ุจูŠู‚ูˆู„ ุฅู†: โ€œุงู„ู€ software entities (ุฒูŠ ุงู„ู€ classes, modules, functions) ุงู„ู…ูุฑูˆุถ ุชูƒูˆู† ู…ูุชูˆุญุฉ ู„ู„ุชูˆุณูŠุน (open for extension)ุŒ ู„ูƒู† ู…ู‚ููˆู„ุฉ ุนู† ุงู„ุชุนุฏูŠู„ (closed for modification).โ€

ูŠุนู†ูŠ ุฅูŠู‡ ุงู„ูƒู„ุงู… ุฏู‡ุŸ ูŠุนู†ูŠ ู„ูˆ ุนู†ุฏูƒ Function ุฃูˆ Class ุจุชุนู…ู„ ุญุงุฌุฉ ู…ุนูŠู†ุฉุŒ ูˆุนุงูŠุฒ ุชุฒูˆุฏ ุนู„ูŠู‡ุง functionality ุฌุฏูŠุฏุฉุŒ ุงู„ู…ูุฑูˆุถ ุชู‚ุฏุฑ ุชุนู…ู„ ุฏู‡ ู…ู† ุบูŠุฑ ู…ุง ุชุนุฏู„ ุณุทุฑ ูƒูˆุฏ ูˆุงุญุฏ ููŠ ุงู„ู€ source code ุงู„ุฃุตู„ูŠ ุจุชุงุนู‡ุง. ุฏู‡ ู…ู‡ู… ุฌุฏุงู‹ ู„ูˆ ุจุชุชุนุงู…ู„ ู…ุน legacy code ุฎุงูŠู ุชุนุฏู„ ููŠู‡ ููŠุจูˆุธ ุญุงุฌุฉ ุดุบุงู„ุฉ ููŠ ุงู„ู€ production. ุงู„ุฃู†ุชูŠุชูŠ ุจุชุงุนุช ุงู„ุณูˆูุชูˆูŠุฑ ุชูƒูˆู† ุจุชุณู…ุญู„ูƒ ุชุทูˆุฑ ูˆุชุฒูˆุฏ ููŠู‡ุง ู…ู† ุบูŠุฑ ู…ุง ุชุบูŠุฑ ุญุงุฌุฉ ู…ูˆุฌูˆุฏุฉ ููŠ ุงู„ู€ source code.

ู…ุซุงู„: ุญุงุณุจุฉ ุงู„ุฎุตูˆู…ุงุช (DiscountCalculator)

ู„ุชูˆุถูŠุญ ุงู„ููƒุฑุฉุŒ ู‡ู†ูุชุฑุถ ูˆุฌูˆุฏ ุงู„ูƒู„ุงุณ ุฏู‡:

public class Product // Helper class
{
    public string Type { get; set; } // e.g., "electronics", "clothing", "books"
    public decimal Price { get; set; }
}

ุชุฎูŠู„ ุนู†ุฏู†ุง DiscountCalculator ููŠ e-commerce application:

ุงู„ูƒูˆุฏ ู‚ุจู„ ุชุทุจูŠู‚ OCP (ู…ุซุงู„ ุณูŠุก):

public class DiscountCalculatorBeforeOCP
{
    public decimal CalculateDiscount(Product product)
    {
        decimal discount = 0;
        // This is often done with a switch statement as well, which is also a violation
        if (product.Type == "electronics")
        {
            discount = product.Price * 0.10m; // 10% for electronics
        }
        else if (product.Type == "clothing")
        {
            discount = product.Price * 0.20m; // 20% for clothing
        }
        else if (product.Type == "books")
        {
            discount = product.Price * 0.15m; // 15% for books
        }
        // If we add a new Product type (e.g., "homeappliances"), we MUST modify this class
        return discount;
    }
}

ุงู„ู€ class ุฏูŠ ุจุณูŠุทุฉ ูˆุจุชุญู‚ู‚ ุงู„ู€ SRP (ู…ุณุคูˆู„ูŠุชู‡ุง ุญุณุงุจ ุงู„ุฎุตู… ุจุณ).

ุงู„ู…ุดูƒู„ุฉ: ุงู„ู€ class ุฏูŠ ุจุชุฎุงู„ู ุงู„ู€ OCP. ู„ูŠู‡ุŸ ู„ุฃู† ู„ูˆ ุญุจูŠู†ุง ู†ุถูŠู ู†ูˆุน ู…ู†ุชุฌ ุฌุฏูŠุฏ ุจุฎูŽุตู… ุฌุฏูŠุฏ (ู…ุซู„ุงู‹ุŒ ุฃุฏูˆุงุช ู…ู†ุฒู„ูŠุฉ)ุŒ ู‡ู†ุถุทุฑ ู†ุนุฏู„ ููŠ ุงู„ู€ CalculateDiscount ูˆู†ุฒูˆุฏ if-else ุฌุฏูŠุฏุฉ (ุฃูˆ case ุฌุฏูŠุฏุฉ ููŠ ุงู„ู€switch). ูƒุฏู‡ ุฅุญู†ุง โ€œุนุฏู„ู†ุงโ€ ููŠ ุงู„ูƒูˆุฏ ุงู„ุฃุตู„ูŠ.

ุงู„ุญู„ (ุชุทุจูŠู‚ ุงู„ู€ OCP ุจุงุณุชุฎุฏุงู… Strategy Pattern):

  1. ู†ุนู…ู„ interface ู„ู„ู€ discount strategy:
public interface IDiscountStrategy
{
	decimal CalculateDiscount(Product product);
}
  1. ู†ุนู…ู„ Concrete Class ู„ูƒู„ ู†ูˆุน ุฎุตู…:
public class ElectronicsDiscountStrategy : IDiscountStrategy
{
	public decimal CalculateDiscount(Product product)
	{
		return product.Price * 0.10m; // 10%
	}
}
 
public class ClothingDiscountStrategy : IDiscountStrategy
{
	public decimal CalculateDiscount(Product product)
	{
		return product.Price * 0.20m; // 20%
	}
}
 
public class BooksDiscountStrategy : IDiscountStrategy
{
	public decimal CalculateDiscount(Product product)
	{
		return product.Price * 0.15m; // 15%
	}
}
 
// To extend (open for extension): add a new strategy class for a new product type
public class HomeAppliancesDiscountStrategy : IDiscountStrategy
{
	public decimal CalculateDiscount(Product product)
	{
		return product.Price * 0.05m; // 5% for home appliances
	}
}
  1. ู†ุนุฏู„ ุงู„ู€ DiscountCalculator ุนุดุงู† ูŠุณุชุฎุฏู… ุงู„ู€ strategy:
public class DiscountCalculatorAfterOCP
{
	// Closed for modification: This class doesn't change when new discount types are added.
	private readonly IDiscountStrategy _discountStrategy;
 
	public DiscountCalculatorAfterOCP(IDiscountStrategy discountStrategy)
	{
		_discountStrategy = discountStrategy; // Dependency is Injected
	}
 
	public decimal CalculateDiscount(Product product)
	{
		// Delegates calculation to the injected strategy
		return _discountStrategy.CalculateDiscount(product);
	}
}

ุงู„ูุงูŠุฏุฉ ู…ู† ูƒุฏู‡ ุฅูŠู‡ุŸ ู„ูˆ ุนุงูŠุฒูŠู† ู†ุถูŠู ู†ูˆุน ุฎุตู… ุฌุฏูŠุฏ (ู…ุซู„ุงู‹ ู„ู„ุฃุฏูˆุงุช ุงู„ู…ู†ุฒู„ูŠุฉ):

  1. ู‡ู†ุนู…ู„ class ุฌุฏูŠุฏุฉ HomeAppliancesDiscountStrategy : IDiscountStrategy.
  2. ู„ู…ุง ู†ูŠุฌูŠ ู†ุณุชุฎุฏู… DiscountCalculatorAfterOCPุŒ ู‡ู†ุณู„ู‘ู… ู„ูŠู‡ ุงู„ู€ instance ู…ู† ุงู„ู€ strategy ุงู„ุฌุฏูŠุฏุฉ ุฏูŠ ููŠ ุงู„ู€ constructor: new DiscountCalculatorAfterOCP(new HomeAppliancesDiscountStrategy()).

ุฅุญู†ุง ูƒุฏู‡ โ€œูˆุณุนู†ุงโ€ ุงู„ู€ system ุจูˆุธูŠูุฉ ุฌุฏูŠุฏุฉ ู…ู† ุบูŠุฑ ู…ุง โ€œู†ุนุฏู„โ€ ุฃูŠ ุญุงุฌุฉ ููŠ DiscountCalculatorAfterOCP ุงู„ุฃุตู„ูŠ ุฃูˆ ุงู„ู€ strategies ุงู„ู‚ุฏูŠู…ุฉ. ุงู„ู€ DiscountCalculatorAfterOCP ุจู‚ู‰ closed for modification ูˆ open for extension. ุงู„ู„ูŠ ุนู…ู„ู†ุงู‡ ุฏู‡ ู…ุซุงู„ ุนู„ู‰ ุงุณุชุฎุฏุงู… Strategy Design Pattern.

ู…ุซุงู„ ุนู…ู„ูŠ: PaymentProcessor

ูุงู„ู…ุซุงู„ ุงู„ู„ูŠ ููˆู‚ ู„ูˆ ุฌูŠู†ุง ู†ุดูˆู ู‡ู†ู„ุงู‚ูŠู‡ ุจูŠุญู‚ู‚ ุงู„ู€ SRP ุจุณ ู…ุด ุจูŠุญู‚ู‚ ุงู„ู€ OCP

// Interface for payment strategies
public interface IPaymentStrategy
{
    void ProcessPayment(decimal amount);
}
 
// Concrete payment strategies
public class VisaPaymentStrategy : IPaymentStrategy
{
    public void ProcessPayment(decimal amount)
    {
        Console.WriteLine($"Processing Visa payment of {amount:C}.");
    }
}
 
public class MasterCardPaymentStrategy : IPaymentStrategy
{
    public void ProcessPayment(decimal amount)
    {
        Console.WriteLine($"Processing MasterCard payment of {amount:C}.");
    }
}
 
public class AmericanExpressPaymentStrategy : IPaymentStrategy
{
    public void ProcessPayment(decimal amount)
    {
        Console.WriteLine($"Processing American Express payment of {amount:C}.");
    }
}
 
// New payment strategy (extending functionality)
public class PayPalPaymentStrategy : IPaymentStrategy
{
    public void ProcessPayment(decimal amount)
    {
        Console.WriteLine($"Processing PayPal payment of {amount:C}.");
    }
}
 
// Payment Processor class - closed for modification
public class PaymentProcessor
{
    private readonly IPaymentStrategy _paymentStrategy;
 
    // Dependency injection through constructor
    public PaymentProcessor(IPaymentStrategy paymentStrategy)
    {
        _paymentStrategy = paymentStrategy;
    }
 
    public void ExecutePayment(Order order) // Assuming Order has a TotalAmount
    {
        // No if/else or switch for payment type here!
        _paymentStrategy.ProcessPayment(order.TotalAmount);
    }
}
 
// Example usage in a Main function or client code
public class OCPClientCode
{
    public static void ProcessOrderPayment()
    {
        Order myOrder = new Order { OrderId = 101, TotalAmount = 150.75m };
 
        // Process with Visa
        IPaymentStrategy visaStrategy = new VisaPaymentStrategy();
        PaymentProcessor visaProcessor = new PaymentProcessor(visaStrategy);
        visaProcessor.ExecutePayment(myOrder);
 
        // Process with PayPal (newly added without modifying PaymentProcessor)
        IPaymentStrategy paypalStrategy = new PayPalPaymentStrategy();
        PaymentProcessor paypalProcessor = new PaymentProcessor(paypalStrategy);
        paypalProcessor.ExecutePayment(myOrder);
    }
}

ู„ูˆ ุนุงูŠุฒูŠู† ู†ุถูŠู ุทุฑูŠู‚ุฉ ุฏูุน ุฌุฏูŠุฏุฉ ุฒูŠ PayPalุŒ ูƒู„ ุงู„ู„ูŠ ู‡ู†ุนู…ู„ู‡ ุฅู†ู†ุง ู†ู†ุดุฆ class ุฌุฏูŠุฏุฉ ุฒูŠ PayPalPaymentStrategy ุจุชุนู…ู„ implement ู„ู„ู€ IPaymentStrategy. ุงู„ู€ PaymentProcessor ู†ูุณู‡ ู…ุด ู‡ูŠุชุนุฏู„.

3. Liskov Substitution Principle - LSP

ุญุฑู L ููŠ SOLID ุจูŠุฑู…ุฒ ู„ู„ู€ Liskov Substitution Principle.

ูŠุนู†ูŠ ุฅูŠู‡ Liskov SubstitutionุŸ

ุงู„ู…ุจุฏุฃ ุฏู‡ ุจูŠู†ุต ุนู„ู‰ ุฅู†: โ€œู„ูˆ ุงู„ู€ class S ู‡ูŠ subclass ู…ู† ุงู„ู€ class TุŒ ูŠุจู‚ู‰ ุงู„ู…ูุฑูˆุถ ู†ู‚ุฏุฑ ู†ุณุชุฎุฏู… objects ู…ู† ุงู„ู†ูˆุน S ู…ูƒุงู† objects ู…ู† ุงู„ู†ูˆุน T ู…ู† ุบูŠุฑ ู…ุง ู†ุจูˆุธ ุฃูŠ ุญุงุฌุฉ ููŠ ุงู„ุจุฑู†ุงู…ุฌ ุฃูˆ ู†ุบูŠุฑ ุณู„ูˆูƒู‡ ุงู„ู…ุชูˆู‚ุน.โ€

ุจู…ุนู†ู‰ ุฃุตุญุŒ ุงู„ู€ subclass ู„ุงุฒู… ุชุญุงูุธ ุนู„ู‰ โ€œุนู‚ุฏโ€ ุงู„ู€ superclass ุจุชุงุนู‡ุง. ู…ุงูŠู†ูุนุด ุงู„ู€ child class ูŠุนู…ู„ ุญุงุฌุฉ ุชุฎุงู„ู ุงู„ู„ูŠ ุงู„ู€ parent class ุจูŠูˆุนุฏ ุจูŠู‡ุง. ู„ูˆ ูƒู„ู…ุช method ุจูŠุนู…ู„ู‡ุง ุงู„ุฃุจุŒ ุงู„ู…ูุฑูˆุถ ุชุชู†ูุฐ ุจุงู„ุณู„ูˆูƒ ุงู„ู„ูŠ ุฃู†ุง ุดุงูŠูู‡ ููŠ ุงู„ุฃุจ.

ู…ุซุงู„: ุงู„ู…ูˆุธููŠู† ูˆุญุณุงุจ ุงู„ู…ุฑุชุจุงุช (Employee & Salary Calculation)

ุชุฎูŠู„ ุนู†ุฏู†ุง class ุงุณู…ู‡ุง Employee:

public class Employee
{
    public string Name { get; set; }
    public int HoursWorked { get; set; }
 
    // Base salary calculation logic
    public virtual decimal CalculateSalary() // Made virtual to allow override
    {
        // Default calculation, e.g., for a generic employee or contractor
        return HoursWorked * 10.0m; // Example: 10 units per hour
    }
}

ูˆุนู†ุฏู†ุง ุฃู†ูˆุงุน ู…ุฎุชู„ูุฉ ู…ู† ุงู„ู…ูˆุธููŠู†:

  1. FullTimeEmployee:
public class FullTimeEmployee : Employee
{
	public decimal FixedSalary { get; set; } = 2000.0m; // Example fixed part
 
	public override decimal CalculateSalary()
	{
		// Specific calculation for full-time: fixed salary + bonus for extra hours
		return (HoursWorked * 10.0m) + FixedSalary;
	}
}
  1. PartTimeEmployee:
public class PartTimeEmployee : Employee
{
	public decimal HourlyWage { get; set; } = 8.0m;
 
	public override decimal CalculateSalary()
	{
		// Specific calculation for part-time
		return HoursWorked * HourlyWage;
	}
}

ุงู„ู…ุดูƒู„ุฉ: ุฅู† ู„ูˆ ูƒู„ subclass ุบูŠุฑุช ุงู„ู€ logic ุจุชุงุน CalculateSalary ุจุดูƒู„ ุฌุฐุฑูŠ ู„ุฏุฑุฌุฉ ุฅู†ู‡ุง ู…ุจู‚ุชุด โ€œุชุญุชุฑู…โ€ ุงู„ู…ุนู†ู‰ ุงู„ุฃุตู„ูŠ ู„ู„ู…ูŠุซูˆุฏ ููŠ ุงู„ู€ base classุŒ ุฏู‡ ู…ู…ูƒู† ูŠุคุฏูŠ ู„ู…ุดุงูƒู„. ู…ุซู„ุงู‹ุŒ ู„ูˆ CalculateSalary ููŠ ุงู„ู€ base class ู„ูŠู‡ุง ู…ุนู†ู‰ ู…ุนูŠู† (ุฒูŠ ุฅู†ู‡ุง ู…ุฑุชุจุทุฉ ู…ุจุงุดุฑุฉ ุจุนุฏุฏ ุงู„ุณุงุนุงุช HoursWorked)ุŒ ูˆุฌู‡ subclass ุนู…ู„ override ูˆุฑุฌุน ู‚ูŠู…ุฉ ุซุงุจุชุฉ ู…ุงู„ู‡ุงุด ุฏุนูˆุฉ ุจู€ HoursWorked ุฎุงู„ุตุŒ ุฏู‡ ู…ู…ูƒู† ูŠูƒูˆู† violation ู„ูˆ ุงู„ู„ูŠ ุจูŠุณุชุฎุฏู… ุงู„ู€ base class ุจูŠุชูˆู‚ุน ุณู„ูˆูƒ ู…ุนูŠู† ุจู†ุงุกู‹ ุนู„ู‰ HoursWorked.

ุงู„ุฎู„ุงุตุฉ ุฅู† ุงู„ู€ override ู„ุงุฒู… ูŠูƒูˆู† ู…ู†ุทู‚ูŠ ููŠ ุณูŠุงู‚ ุงู„ู€ base method.

ุงู„ุญู„ : ูุตู„ ุงู„ู€ salary calculation ููŠ interface ู…ู†ูุตู„ ู„ูˆ ุงู„ู€ logic ุจูŠุฎุชู„ู ุฌุฏุงู‹.

public interface ISalaryCalculator
{
    decimal CalculateSalary(EmployeeDetails details); // EmployeeDetails could hold HoursWorked, Type, etc.
}
 
public class EmployeeDetails // Helper for ISalaryCalculator
{
    public string EmployeeType { get; set; }
    public int HoursWorked { get; set; }
    public decimal BaseRate { get; set; }
    public decimal FixedComponent { get; set; }
}
 
// Base Employee class might not have CalculateSalary directly anymore,
// or it could have a default one if ISalaryCalculator is optional.
public class EmployeeBase // Renamed to avoid confusion
{
    public string Name { get; set; }
    // Other common properties
}
 
 
public class FullTimeEmployeeStrategy : ISalaryCalculator
{
    public decimal CalculateSalary(EmployeeDetails details)
    {
        // Logic specific to full-time, e.g. FixedComponent + (details.HoursWorked * bonusRate)
        Console.WriteLine("C#: Calculating FullTime Employee Salary using Strategy.");
        return details.FixedComponent + (details.HoursWorked * details.BaseRate); // Example logic
    }
}
 
public class PartTimeEmployeeStrategy : ISalaryCalculator
{
    public decimal CalculateSalary(EmployeeDetails details)
    {
        // Logic specific to part-time
        Console.WriteLine("C#: Calculating PartTime Employee Salary using Strategy.");
        return details.HoursWorked * details.BaseRate;
    }
}
 
// Client code would use the strategy
public class SalaryService
{
    private readonly ISalaryCalculator _salaryCalculator;
    private readonly EmployeeDetails _employeeDetails;
 
    public SalaryService(ISalaryCalculator salaryCalculator, EmployeeDetails employeeDetails)
    {
        _salaryCalculator = salaryCalculator;
        _employeeDetails = employeeDetails;
    }
 
    public decimal GetSalary()
    {
        return _salaryCalculator.CalculateSalary(_employeeDetails);
    }
}

ุจูƒุฏู‡ุŒ ุงู„ู€ Employee class ู†ูุณู‡ุง (ุฃูˆ EmployeeBase ููŠ ุงู„ู…ุซุงู„ ุฏู‡) ู…ุจู‚ุงุด ููŠู‡ุง ุงู„ู€ CalculateSalary ุจุงู„ู€ logic ุงู„ู…ุฎุชู„ูุŒ ูˆุจู‚ู‰ ุงู„ู€ logic ุฏู‡ ู…ูˆุฌูˆุฏ ููŠ strategies ู…ู†ูุตู„ุฉ. ุฏู‡ ุจูŠุถู…ู† ุฅู† ุฃูŠ EmployeeBase object ู„ูŠู‡ ู†ูุณ ุงู„ุฎุตุงุฆุต ุงู„ุฃุณุงุณูŠุฉุŒ ูˆุงู„ู€ calculation ุจูŠุชู… ุจุทุฑูŠู‚ุฉ ูˆุงุถุญุฉ ูˆู…ูุตูˆู„ุฉ.

ู…ุซุงู„ ุนู…ู„ูŠ: ุงู„ุทู„ุจุงุช ูˆุชูƒุงู„ูŠู ุงู„ุดุญู† (Order & ShippingCost)

// Base Order class
public class OrderLSP
{
    public string Name { get; set; }
    public decimal Price { get; set; }
    // protected decimal ShippingCost { get; set; } // Made protected
 
    public virtual decimal GetTotalPrice()
    {
        // Base implementation might assume some default shipping or that shipping is added by subclasses
        Console.WriteLine($"C#: OrderLSP - GetTotalPrice called for {Name}. Price: {Price}");
        return Price; // Default: price without shipping
    }
}
 
// DeliveryOrder expects shipping to be added
public class DeliveryOrder : OrderLSP
{
    public decimal ShippingCost { get; private set; } = 25.0m; // Example shipping cost
 
    public DeliveryOrder(string name, decimal price)
    {
        Name = name;
        Price = price;
    }
    public override decimal GetTotalPrice()
    {
        decimal basePrice = base.GetTotalPrice(); // Good practice to call base if relevant
        decimal totalPrice = basePrice + ShippingCost;
        Console.WriteLine($"C#: DeliveryOrder - Total for {Name}: {basePrice} + Shipping {ShippingCost} = {totalPrice}");
        return totalPrice;
    }
}
 
// PickupOrder should NOT have shipping costs.
public class PickupOrder : OrderLSP
{
    public PickupOrder(string name, decimal price)
    {
        Name = name;
        Price = price;
    }
    // By inheriting GetTotalPrice from OrderLSP, it correctly returns just the Price,
    // which is what we want for a PickupOrder (no shipping).
    // If OrderLSP's GetTotalPrice always added a base shipping, PickupOrder would have
    // to override it to *remove* or *zero-out* shipping, which could be an LSP violation
    // if clients expect GetTotalPrice to always behave in one way w.r.t shipping.
 
    // If OrderLSP.GetTotalPrice implicitly included a default non-zero shipping cost,
    // PickupOrder would violate LSP if it just returned Price.
    // The current structure is okay.
}

ุงู„ู…ุดูƒู„ุฉ (ู„ูˆ ุงู„ู€ base class ุงูุชุฑุถุช ุณู„ูˆูƒ ุฏุงูŠู…ุงู‹ ููŠู‡ ุดุญู†): ู„ูˆ ุงู„ู€ OrderLSP.GetTotalPrice() ูƒุงู†ุช ุฏุงูŠู…ุงู‹ ุจุชุถูŠู ุฑู‚ู… ุดุญู† (ุญุชู‰ ู„ูˆ ุตูุฑ)ุŒ ูˆุฌู‡ ุงู„ู€ PickupOrder ุญุจ ูŠู„ุบูŠ ุงู„ุดุญู† ุฏู‡ ุฎุงู„ุต ุจุทุฑูŠู‚ุฉ ุบูŠุฑ ู…ุชูˆู‚ุนุฉ (ู…ุซู„ุงู‹ ูŠุทุฑุญ ู‚ูŠู…ุฉ ุฃูˆ ูŠุฑุฌุน ุงู„ู€ Price ุจุณ ู„ู…ุง ุงู„ู€ client ู…ุชูˆู‚ุน ุฅู† ุงู„ุดุญู† ู…ุญุณูˆุจ)ุŒ ุฏู‡ ู…ู…ูƒู† ูŠุจู‚ู‰ LSP violation.

ุงู„ุญู„ ุงู„ู…ู‚ุชุฑุญ (ูุตู„ ุญุณุงุจ ุงู„ุดุญู†):

public interface IShippingCostCalculator
{
    decimal CalculateShippingCost(OrderLSP order);
}
 
public class StandardShippingCalculator : IShippingCostCalculator
{
    public decimal CalculateShippingCost(OrderLSP order)
    {
        // Logic for standard shipping, maybe based on weight, distance, or a flat fee
        return 25.0m; // Example flat fee
    }
}
 
public class NoShippingCalculator : IShippingCostCalculator // For Pickup Orders
{
    public decimal CalculateShippingCost(OrderLSP order)
    {
        return 0m; // No shipping cost
    }
}
 
// Modified OrderLSP to use the calculator strategy
public class OrderLSP_Refactored
{
    public string Name { get; set; }
    public decimal Price { get; set; }
    private readonly IShippingCostCalculator _shippingCalculator;
 
    public OrderLSP_Refactored(string name, decimal price, IShippingCostCalculator shippingCalculator)
    {
        Name = name;
        Price = price;
        _shippingCalculator = shippingCalculator;
    }
 
    public decimal GetTotalPrice()
    {
        decimal shipping = _shippingCalculator.CalculateShippingCost(this); // 'this' might not be needed if calculator has all info
        Console.WriteLine($"C#: OrderLSP_Refactored - Price: {Price}, Shipping: {shipping}");
        return Price + shipping;
    }
}
 
// Client code would then provide the correct calculator
// var deliveryOrder = new OrderLSP_Refactored("Laptop", 1200, new StandardShippingCalculator());
// var pickupOrder = new OrderLSP_Refactored("Mouse", 25, new NoShippingCalculator());
// Console.WriteLine(deliveryOrder.GetTotalPrice());
// Console.WriteLine(pickupOrder.GetTotalPrice());

ูƒุฏู‡ ุงู„ู€ base class (OrderLSP_Refactored) ู†ูุณู‡ุง ู…ุจู‚ุชุด ู…ุณุคูˆู„ุฉ ุนู† ุงู„ู€ logic ุงู„ุฎุงุต ุจุญุณุงุจ ุชูƒู„ูุฉ ุงู„ุดุญู†ุŒ ูˆุงู„ู€ Subtypes (DeliveryOrder ูˆ PickupOrder) ุจูŠุชู… ุชุญุฏูŠุฏ ุณู„ูˆูƒ ุงู„ุดุญู† ุจุชุงุนู‡ู… ุนู† ุทุฑูŠู‚ IShippingCostCalculator ุงู„ู„ูŠ ุจูŠุชู… ุญู‚ู†ู‡. ุฃูŠ object ู…ู† OrderLSP_Refactored ู‡ูŠุชุตุฑู ุจุดูƒู„ ู…ุชูˆู‚ุน ุจู†ุงุกู‹ ุนู„ู‰ ุงู„ู€ calculator ุงู„ู„ูŠ ู…ุนุงู‡.

4. Interface Segregation Principle - ISP

ุญุฑู I ููŠ SOLID ุจูŠุฑู…ุฒ ู„ู„ู€ Interface Segregation Principle.

ูŠุนู†ูŠ ุฅูŠู‡ Interface SegregationุŸ

ุงู„ู…ุจุฏุฃ ุฏู‡ ุจุณูŠุท ูˆุจูŠู‚ูˆู„: โ€œู…ููŠุด code ุงู„ู…ูุฑูˆุถ ูŠูƒูˆู† ู…ุฌุจุฑ ุฅู†ู‡ ูŠุนุชู…ุฏ ุนู„ู‰ methods ู‡ูˆ ู…ุด ู‡ูŠุณุชุฎุฏู…ู‡ุง ุฃูˆ ู…ุด ู‡ูŠุนู…ู„ู‡ุง implementation.โ€

ุงู„ู€ ISP ุจูŠู‚ูˆู„ูƒ ู‚ุณู… ุงู„ู€ interfaces ุงู„ูƒุจูŠุฑุฉ (ุงู„ู„ูŠ ููŠู‡ุง methods ูƒุชูŠุฑุŒ ุจูŠุณู…ูˆู‡ุง โ€œfat interfacesโ€) ู„ู€ interfaces ุฃุตุบุฑ ูˆุฃูƒุซุฑ ุชุฎุตุตู‹ุง (ุจูŠุณู…ูˆู‡ุง โ€œrole interfacesโ€ ุฃูˆ โ€œfine-grained interfacesโ€). ูƒู„ class ุชุนู…ู„ implement ู„ู„ู€ interface ุงู„ู„ูŠ ููŠู‡ ุงู„ู€ methods ุงู„ู„ูŠ ู‡ูŠ ู…ุญุชุงุฌุงู‡ุง ูุนู„ุงู‹ ุจุณ.

ู…ุซุงู„: ู…ุฏูŠุฑ ุงู„ู…ู‡ุงู… (TaskManager)

ู„ุชูˆุถูŠุญ ุงู„ููƒุฑุฉุŒ ู‡ู†ูุชุฑุถ ูˆุฌูˆุฏ ุงู„ูƒู„ุงุณุงุช ุฏูŠ:

public class TaskItem // Helper class
{
    public int Id { get; set; }
    public string Description { get; set; }
}
 
public class User // Helper class
{
    public string Name { get; set; }
}

ุชุฎูŠู„ ุนู†ุฏู†ุง interface ุงุณู…ู‡ ITaskManagerFat ุจูŠุนู…ู„ ุญุงุฌุงุช ูƒุชูŠุฑ:

// "Fat" interface - Bad example
public interface ITaskManagerFat
{
    void CreateTask(TaskItem task);
    void AssignTask(TaskItem task, User user);
    void SendNotification(TaskItem task, string message); // Notification-related method
}

ูˆุนู†ุฏู†ุง TaskService ุจูŠุนู…ู„ implement ู„ู„ู€ interface ุฏู‡:

public class TaskServiceUsingFatInterface : ITaskManagerFat
{
    public void CreateTask(TaskItem task)
    {
        Console.WriteLine($"C#: TaskService - Task '{task.Description}' created.");
    }
 
    public void AssignTask(TaskItem task, User user)
    {
        Console.WriteLine($"C#: TaskService - Task '{task.Description}' assigned to {user.Name}.");
    }
 
    // This TaskService might not be responsible for sending notifications,
    // but it's forced to implement it because of the fat interface.
    public void SendNotification(TaskItem task, string message)
    {
        Console.WriteLine($"C#: TaskService - (Forced Impl) Notification for '{task.Description}': {message}. Maybe not its job.");
        // Or it might throw new NotSupportedException("SendNotification is not supported by TaskService.");
        // Or leave it empty. All are bad signs.
    }
}

ุงู„ู…ุดูƒู„ุฉ ู‡ู†ุง ุฅูŠู‡ุŸ

  1. ุงู„ู€ SRP violation (ู…ุญุชู…ู„): ุงู„ู€ TaskService ุจู‚ู‰ ู…ุณุคูˆู„ ุนู† ุฅุฏุงุฑุฉ ุงู„ู…ู‡ุงู… ูˆูƒู…ุงู† ุฅุฑุณุงู„ ุงู„ุฅุดุนุงุฑุงุช (ู„ูˆ ู‡ูˆ ุงู„ู„ูŠ ุจูŠู†ูุฐู‡ุง ูุนู„ุงู‹).
  2. ISP violation:
    • ุงู„ู€ TaskServiceUsingFatInterface ู…ุฌุจุฑ ูŠุนู…ู„ implement ู„ู„ู€ SendNotification ุญุชู‰ ู„ูˆ ู…ุด ู‡ูŠุณุชุฎุฏู…ู‡ุง ุฃูˆ ู…ุด ุฏูŠ ู…ุณุคูˆู„ูŠุชู‡. ู…ู…ูƒู† ูŠุณูŠุจ ุงู„ู€ method body ูุงุถูŠ ุฃูˆ ูŠุฑู…ูŠ NotSupportedException.
    • ู„ูˆ ุนู†ุฏู†ุง class ุชุงู†ูŠุฉ ูƒู„ ู‡ู…ู‡ุง ุฅุฑุณุงู„ ุงู„ุฅุดุนุงุฑุงุช (ู…ุซู„ุงู‹ StandaloneNotificationService)ุŒ ู‡ุชุถุทุฑ ุชุนู…ู„ implement ู„ู„ู€ ITaskManagerFat ูƒู„ู‡ุŒ ุจู…ุง ููŠู‡ CreateTask ูˆ AssignTask ุงู„ู„ูŠ ู‡ูŠ ู…ุด ู…ุญุชุงุฌุงู‡ู…ุŒ ุจุณ ุนุดุงู† ุชุณุชุฎุฏู… SendNotification.

ุงู„ุญู„ (ุชุทุจูŠู‚ ุงู„ู€ ISP): ู†ู‚ุณู… ุงู„ู€ interface ุงู„ูƒุจูŠุฑ ู„ู€ interfaces ุฃุตุบุฑ ูˆุฃูƒุซุฑ ุชุฎุตุตู‹ุง:

  1. ุงู„ู€ ITaskOperations: ู„ู„ุนู…ู„ูŠุงุช ุงู„ุฎุงุตุฉ ุจุงู„ู…ู‡ุงู… ูู‚ุท.
// Interface for task operations
public interface ITaskOperations
{
	void CreateTask(TaskItem task);
	void AssignTask(TaskItem task, User user);
}
  1. ุงู„ู€ INotificationService: ู„ุนู…ู„ูŠุงุช ุฅุฑุณุงู„ ุงู„ุฅุดุนุงุฑุงุช ูู‚ุท.
// Interface for notification sending
public interface INotificationService // Renamed for clarity from INotificationSender
{
	void SendNotification(string recipientIdentifier, string message); // More generic now
}

ุงู„ู€ classes ุจุนุฏ ุงู„ุชุนุฏูŠู„:

  • ุงู„ู€ TaskManagementService ู‡ูŠุนู…ู„ implement ู„ู„ู€ ITaskOperations:
public class TaskManagementService : ITaskOperations // Implements only what it needs
{
	public void CreateTask(TaskItem task)
	{
		Console.WriteLine($"C#: TaskManagementService - Task '{task.Description}' created.");
	}
 
	public void AssignTask(TaskItem task, User user)
	{
		Console.WriteLine($"C#: TaskManagementService - Task '{task.Description}' assigned to {user.Name}.");
	}
	// Not forced to implement SendNotification
}
  • ู„ูˆ ููŠู‡ class ู…ุณุคูˆู„ุฉ ุนู† ุงู„ุฅุดุนุงุฑุงุช (ุฒูŠ EmailNotificationService)ุŒ ู‡ุชุนู…ู„ implement ู„ู„ู€ INotificationService:
public class EmailNotificationService : INotificationService
{
	public void SendNotification(string emailAddress, string message)
	{
		Console.WriteLine($"C#: EmailNotificationService - Email sent to {emailAddress}: {message}");
	}
}
  • ู„ูˆ ุงู„ู€ TaskManagementService ู…ุญุชุงุฌ ูŠุจุนุช ุฅุดุนุงุฑุงุชุŒ ู…ู…ูƒู† โ€œูŠุณุชุฎุฏู…โ€ (ูŠุนู…ู„ composition ูˆูŠุญู‚ู† dependency) ู„ู€ instance ู…ู† INotificationServiceุŒ ุฃูˆ ู…ู…ูƒู† ุญุชู‰ ูŠุฎุชุงุฑ ูŠุนู…ู„ implement ู„ู€ INotificationService ู‡ูˆ ูƒู…ุงู† ู„ูˆ ุฏู‡ ู…ู†ุทู‚ูŠ ููŠ ุณูŠุงู‚ู‡ (ู…ุน ุงู„ุญุฐุฑ ู…ู† ู…ุฎุงู„ูุฉ SRP). ุงู„ู€ TaskManagementService ู…ุจู‚ุงุด ู…ุฌุจุฑ ูŠุนู…ู„ implement ู„ุญุงุฌุฉ ู…ุด ู‡ูŠุณุชุฎุฏู…ู‡ุง.

ู…ุซุงู„ ุนู…ู„ูŠ: UserManagement

ู„ุชูˆุถูŠุญ ุงู„ููƒุฑุฉุŒ ู‡ู†ูุชุฑุถ ูˆุฌูˆุฏ ุงู„ูƒู„ุงุณุงุช ุฏูŠ:

// Helper class for User
// public class User { public string Name { get; set; } /* ... other properties ... */ } already defined
// Helper class for Product
// public class Product { public string Name { get; set; } /* ... other properties ... */ } already defined

ุชุฎูŠู„ ุนู†ุฏู†ุง IUserManagementFat interface:

// Original "fat" interface
public interface IUserManagementFat
{
    void UpdateUserProfile(User user);
    void ChangePassword(User user, string newPassword);
    void SubscribeToNewProductAvailability(User user, Product product);
    void SubscribeToSmsNotifications(User user, string topic);
}

ูˆุนู†ุฏู†ุง Customer ุจูŠุนู…ู„ implement ู„ูƒู„ ุฏูˆู„. ู„ูƒู† ุนู†ุฏู†ุง GuestSubscriber (ู…ู…ูƒู† ูŠูƒูˆู† guest user ุนู…ู„ subscribe ู…ู† ุบูŠุฑ account ูƒุงู…ู„)ุŒ ู‡ูˆ ุจุณ ุนุงูŠุฒ ูŠุนู…ู„ subscribe ู„ู€ product availability ุฃูˆ SMS notificationsุŒ ูˆู…ุด ู…ุญุชุงุฌ UpdateUserProfile ุฃูˆ ChangePassword. ู‡ู†ุง ุงู„ู€ GuestSubscriber ู…ุฌุจุฑ ูŠุนู…ู„ implement ู„ู€ methods ู…ุด ู‡ูŠุณุชุฎุฏู…ู‡ุง.

ุงู„ุญู„ (ุชุทุจูŠู‚ ุงู„ู€ ISP):

  1. ุงู„ู€ IAccountManagement: ู„ู„ุนู…ู„ูŠุงุช ุงู„ุฎุงุตุฉ ุจุงู„ุญุณุงุจ.
// Interface for account specific actions
public interface IAccountManagement
{
	void UpdateUserProfile(User user);
	void ChangePassword(User user, string newPassword);
}
  1. ุงู„ู€ ISubscriptionManagement: ู„ู„ุนู…ู„ูŠุงุช ุงู„ุฎุงุตุฉ ุจุงู„ุงุดุชุฑุงูƒุงุช.
// Interface for subscription actions
public interface ISubscriptionManagement // Renamed from ISubscriptionService for clarity
{
	void SubscribeToNewProductAvailability(User user, Product product);
	void SubscribeToSmsNotifications(User user, string topic);
}

ุงู„ู€ classes ุจุนุฏ ุงู„ุชุนุฏูŠู„:

  • ุงู„ู€ RegisteredCustomer ู‡ูŠุนู…ู„ implement ู„ู„ุงุชู†ูŠู†: IAccountManagement ูˆ ISubscriptionManagement.
public class RegisteredCustomer : IAccountManagement, ISubscriptionManagement
{
	private User _user;
	public RegisteredCustomer(User user) { _user = user; }
 
	// From IAccountManagement
	public void UpdateUserProfile(User user) { Console.WriteLine($"C#: RegisteredCustomer {_user.Name} profile updated."); }
	public void ChangePassword(User user, string newPassword) { Console.WriteLine($"C#: RegisteredCustomer {_user.Name} password changed."); }
 
	// From ISubscriptionManagement
	public void SubscribeToNewProductAvailability(User user, Product product) { Console.WriteLine($"C#: RegisteredCustomer {_user.Name} subscribed to product {product.Name}."); }
	public void SubscribeToSmsNotifications(User user, string topic) { Console.WriteLine($"C#: RegisteredCustomer {_user.Name} subscribed to SMS for {topic}."); }
}
  • ุงู„ู€ GuestSubscriber ู‡ูŠุนู…ู„ implement ู„ู„ู€ ISubscriptionManagement ุจุณ.
public class GuestSubscriber : ISubscriptionManagement // Only needs subscription capabilities
{
	private User _user; // Could be a simpler representation like just an email or phone
	public GuestSubscriber(User user) { _user = user; }
 
	// From ISubscriptionManagement
	public void SubscribeToNewProductAvailability(User user, Product product) { Console.WriteLine($"C#: GuestSubscriber for {_user.Name} subscribed to product {product.Name}."); }
	public void SubscribeToSmsNotifications(User user, string topic) { Console.WriteLine($"C#: GuestSubscriber for {_user.Name} subscribed to SMS for {topic}."); }
	// Not forced to implement account management methods
}

ูƒุฏู‡ ูƒู„ class ุจุชุงุฎุฏ ุงู„ู€ interface ุงู„ู„ูŠ ูŠู†ุงุณุจู‡ุง ูˆู…ุจู‚ุงุด ููŠ ุฅุฌุจุงุฑ ุนู„ู‰ methods ู…ุด ู‡ุชุณุชุฎุฏู…ู‡ุง.

5. Dependency Inversion Principle - DIP

ุญุฑู D ููŠ SOLID ุจูŠุฑู…ุฒ ู„ู„ู€ Dependency Inversion Principle. ุฑูƒุฒูˆุง ููŠ ุฏู‡ ุนุดุงู† ู…ู‡ู… ูˆู†ุงุณ ูƒุชูŠุฑ ุจุชุชู„ุฎุจุท ููŠู‡.

ูŠุนู†ูŠ ุฅูŠู‡ Dependency InversionุŸ

ุงู„ู…ุจุฏุฃ ุฏู‡ ุจูŠู†ุต ุนู„ู‰ ุญุงุฌุชูŠู†:

  1. ุงู„ู€ high-level modules ู…ูŠู†ูุนุด ุชุนุชู…ุฏ ู…ุจุงุดุฑุฉ ุนู„ู‰ ุงู„ู€ low-level modules. ุงู„ุงุชู†ูŠู† ู„ุงุฒู… ูŠุนุชู…ุฏูˆุง ุนู„ู‰ Abstraction (ุฒูŠ ุงู„ู€ interfaces ุฃูˆ abstract classes).
  2. ุงู„ู€ abstractions ู†ูุณู‡ุง ู…ูŠู†ูุนุด ุชุนุชู…ุฏ ุนู„ู‰ ุงู„ุชูุงุตูŠู„ (ุงู„ู€ concrete implementations). ุงู„ุนูƒุณ ู‡ูˆ ุงู„ุตุญูŠุญ: ุงู„ุชูุงุตูŠู„ (ุงู„ู€ concrete classes) ู‡ูŠ ุงู„ู„ูŠ ุงู„ู…ูุฑูˆุถ ุชุนุชู…ุฏ ุนู„ู‰ ุงู„ู€ abstractions (ุนู† ุทุฑูŠู‚ ุฅู†ู‡ุง ุชุนู…ู„ู‡ุง implement ุฃูˆ ุชูˆุฑุซ ู…ู†ู‡ุง).

ูŠุนู†ูŠ ุฅูŠู‡ ุงู„ูƒู„ุงู… ุฏู‡ุŸ ุชุฎูŠู„ ุนู†ุฏูƒ OrderProcessingService (ุฏู‡ high-level module ู„ุฃู†ู‡ ููŠู‡ business logic ูˆู…ุณุคูˆู„ ุนู† ุชู†ุณูŠู‚ ุนู…ู„ูŠุฉ ูƒุจูŠุฑุฉ). ูˆุนู†ุฏูƒ MySqlOrderDataAccess (ุฏู‡ low-level module ู„ุฃู†ู‡ ุชูุตูŠู„ุฉ ุชู‚ู†ูŠุฉ ู„ู„ูˆุตูˆู„ ู„ู„ุฏุงุชุง ุจูŠุฒ).

ู„ูˆ ุงู„ู€ OrderProcessingService ุจูŠุนู…ู„ new MySqlOrderDataAccess() ุฌูˆุงู‡ ู…ุจุงุดุฑุฉุŒ ูƒุฏู‡ ุงู„ู€ high-level ุจูŠุนุชู…ุฏ ุนู„ู‰ ุชูุตูŠู„ุฉ low-level (concrete implementation). ุฏู‡ ุจูŠุฎู„ูŠ ุงู„ู€ service ุจุชุงุนุชูƒ ู…ุฑุจูˆุทุฉ ุฌุงู…ุฏ ุจุงู„ู€ MySQL. ู„ูˆ ุญุจูŠุช ุชุบูŠุฑ ู„ู€ PostgreSQL ุฃูˆ ุชุณุชุฎุฏู… mock ููŠ ุงู„ู€ testingุŒ ู‡ุชุจู‚ู‰ ู…ุดูƒู„ุฉ.

ุงู„ู€ DIP ุจูŠู‚ูˆู„ูƒ ู„ุฃุŒ ู…ุชุนู…ู„ุด ูƒุฏู‡. ุฎู„ูŠ ุงู„ุงุชู†ูŠู† ูŠุนุชู…ุฏูˆุง ุนู„ู‰ abstraction:

  1. ุงู„ู€ OrderProcessingService (high-level) ูŠุนุชู…ุฏ ุนู„ู‰ interface (ู…ุซู„ุงู‹ IOrderDataAccess).
  2. ุงู„ู€ MySqlOrderDataAccess (low-level) ูŠุนู…ู„ implement ู„ู„ู€ interface IOrderDataAccess.
  3. ูŠุชู… โ€œุญู‚ู†โ€ (inject) ุงู„ู€ concrete implementation (ุฒูŠ MySqlOrderDataAccess) ุฌูˆู‡ ุงู„ู€ OrderProcessingService (ุนุงุฏุฉู‹ ุนู† ุทุฑูŠู‚ ุงู„ู€ constructor).

ูƒุฏู‡ ุงู„ุงุชู†ูŠู† (ุงู„ู€ high-level ูˆุงู„ู€ low-level) ุจู‚ูˆุง ุจูŠุนุชู…ุฏูˆุง ุนู„ู‰ abstraction ุฒูŠ (IOrderDataAccess). ูˆุงู„ุงุนุชู…ุงุฏูŠุฉ ุงุชุนูƒุณุช: ุจุฏู„ ู…ุง ุงู„ู€ business logic ุจูŠุดุงูˆุฑ ุนู„ู‰ ุงู„ู€ data accessุŒ ุจู‚ู‰ ุงู„ู€ data access ู‡ูˆ ุงู„ู„ูŠ ุจูŠุดุงูˆุฑ (ุจูŠุนู…ู„ implement) ู„ู„ู€ abstraction ุงู„ู„ูŠ ุงู„ู€ business logic ุจูŠุนุฑูู‡ุง ูˆุจูŠุณุชุฎุฏู…ู‡ุง.

ู…ุซุงู„: OrderManagementService ูˆ OrderDataAccess

ุงู„ุดูƒู„ ุงู„ุบู„ุท (ู…ุฎุงู„ูุฉ ุงู„ู€ DIP) - ู‡ุงูŠ ู„ูŠูู„ ุจูŠุนุชู…ุฏ ุนู„ู‰ ู„ูˆ ู„ูŠูู„:

// Low-level module (concrete data access implementation)
public class MySqlOrderDataAccessNoDIP
{
    public List<Order> FetchAllOrdersFromDB()
    {
        Console.WriteLine("C#: MySqlOrderDataAccessNoDIP - Fetching all orders from MySQL.");
        // Actual database query logic here
        return new List<Order>();
    }
    // Other methods like SaveOrder, etc.
}
 
// High-level module directly depending on the low-level concrete class
public class OrderManagementServiceNoDIP
{
    private readonly MySqlOrderDataAccessNoDIP _dataAccess; // Direct dependency on a CONCRETE class
 
    public OrderManagementServiceNoDIP()
    {
        // Instantiation of concrete class inside the high-level module
        _dataAccess = new MySqlOrderDataAccessNoDIP();
    }
 
    public List<Order> GetAndProcessOrders()
    {
        Console.WriteLine("C#: OrderManagementServiceNoDIP - Getting and processing orders.");
        List<Order> orders = _dataAccess.FetchAllOrdersFromDB();
        // Further processing logic on orders...
        return orders;
    }
}

ู‡ู†ุง ุงู„ู€ OrderManagementServiceNoDIP (high-level) ุจูŠุนุชู…ุฏ ู…ุจุงุดุฑุฉ ุนู„ู‰ MySqlOrderDataAccessNoDIP (low-level).

ุงู„ุญู„ (ุชุทุจูŠู‚ ุงู„ู€ DIP) - ุงู„ุงุชู†ูŠู† ุจูŠุนุชู…ุฏูˆุง ุนู„ู‰ ุฃุจุณุชุฑุงูƒุดู†:

  1. ู†ุนู…ู„ abstraction (interface) ู„ู„ุฏุงุชุง ุฃูƒุณุณ:
// Abstraction (Interface) for data access
public interface IOrderDataAccess // Renamed from IOrderRepository for consistency with video's naming flow
{
	List<Order> FetchAllOrders();
	// void SaveOrder(Order order); // Example of other methods
}
  1. ุงู„ู€ low-level modules (ุงู„ุชูุงุตูŠู„) ุชุนู…ู„ implement ู„ู„ู€ interface:
// Low-level module (concrete MySQL implementation) implementing the abstraction
public class MySqlOrderDataAccess : IOrderDataAccess
{
	public List<Order> FetchAllOrders()
	{
		Console.WriteLine("C#: MySqlOrderDataAccess - Fetching all orders from MySQL (via IOrderDataAccess).");
		return new List<Order>();
	}
}
 
// Another possible low-level module (concrete MongoDB implementation)
public class MongoDbOrderDataAccess : IOrderDataAccess
{
	public List<Order> FetchAllOrders()
	{
		Console.WriteLine("C#: MongoDbOrderDataAccess - Fetching all orders from MongoDB (via IOrderDataAccess).");
		return new List<Order>();
	}
}
  1. ุงู„ู€ high-level module ูŠุนุชู…ุฏ ุนู„ู‰ ุงู„ู€ interface (ุนุงุฏุฉู‹ ุนู† ุทุฑูŠู‚ ุงู„ู€ constructor injection):
// High-level module depending on the abstraction (IOrderDataAccess)
public class OrderManagementServiceWithDIP
{
	private readonly IOrderDataAccess _dataAccessProvider; // Depends on ABSTRACTION
 
	// Dependency is injected via constructor (Constructor Injection)
	public OrderManagementServiceWithDIP(IOrderDataAccess dataAccessProvider)
	{
		_dataAccessProvider = dataAccessProvider;
	}
 
	public List<Order> GetAndProcessOrders()
	{
		Console.WriteLine("C#: OrderManagementServiceWithDIP - Getting and processing orders using injected provider.");
		List<Order> orders = _dataAccessProvider.FetchAllOrders(); // Uses the abstraction
		// Further processing logic on orders...
		return orders;
	}
}

ุจู†ุทุจู‚ ุงู„ู€ DIP ุนู† ุทุฑูŠู‚ ุงู„ู€ Dependency Injection (DI) ูˆุงู„ู„ูŠ ุนู…ู„ู†ุงู‡ ููˆู‚ ุฏุง ุงุณู…ู‡ Constructor Injection ูˆุฏุง ู†ูˆุน ู…ู† ุงู†ูˆุงุนู‡

ุงู„ูุงูŠุฏุฉ ู…ู† ูƒุฏู‡ ุฅูŠู‡ุŸ

  • ุงู„ู€ OrderManagementServiceWithDIP ู…ุจู‚ุงุด ู…ุฑุจูˆุท ุจู†ูˆุน database ู…ุนูŠู†. ู†ู‚ุฏุฑ ู†ุฏูŠู„ู‡ ุฃูŠ class ุจุชุนู…ู„ implement ู„ู„ู€ IOrderDataAccess (ุณูˆุงุก MySqlOrderDataAccess ุฃูˆ MongoDbOrderDataAccess ุฃูˆ ุญุชู‰ InMemoryDataAccess ุนุดุงู† ุงู„ู€ testing).
  • ุฏู‡ ุจูŠุณู‡ู„ ุงู„ู€ testing ุฌุฏุงู‹ (ุจุงุณุชุฎุฏุงู… mock objects ู„ู„ู€ IOrderDataAccess).
  • ุจูŠุฒูˆุฏ ุงู„ู€ flexibility ูˆ ุงู„ู€ extensibility ู„ู„ู€ system.
  • ุงู„ุงุนุชู…ุงุฏูŠุฉ ุงุชุนูƒุณุช: ุจุฏู„ ู…ุง ุงู„ู€ high-level module (ุงู„ู€Service) ุจูŠุนุฑู ูˆุจูŠุนู…ู„ instantiate ู„ู„ู€ low-level module (ุงู„ู€ Data Access)ุŒ ุจู‚ู‰ ุงู„ู€ high-level module ุจูŠุนุชู…ุฏ ุนู„ู‰ abstraction ุงู„ู„ูŠ ู‡ูˆ (interface)ุŒ ูˆุงู„ู€ low-level module ู‡ูˆ ุงู„ู„ูŠ ุจูŠุนู…ู„ implement ู„ู„ู€ abstraction ุฏูŠ. ูˆุงู„ู€ client ุฃูˆ ุงู„ู€ Dependency Injection Container ู‡ูˆ ุงู„ู„ูŠ ุจูŠุฑุจุทู‡ู… ุจุจุนุถ.

ู…ุซุงู„ ุนู…ู„ูŠ ุณุงุจู‚ (PaymentProcessor ูˆ IPaymentStrategy): ู…ุซุงู„ ุงู„ู€ PaymentProcessor ุงู„ู„ูŠ ุงุณุชุฎุฏู…ู†ุงู‡ ููŠ ุดุฑุญ ุงู„ู€ OCP ู‡ูˆ ูƒู…ุงู† ุจูŠุทุจู‚ ุงู„ู€ DIP ุจุดูƒู„ ูƒูˆูŠุณ:

  • ุงู„ู€ PaymentProcessor (high-level module) ูƒุงู† ุจูŠุนุชู…ุฏ ุนู„ู‰ IPaymentStrategy (abstraction).
  • ุงู„ู€ concrete strategies ุฒูŠ VisaPaymentStrategyุŒ MasterCardPaymentStrategy (low-level modules/ุงู„ุชูุงุตูŠู„) ูƒุงู†ูˆุง ุจูŠุนู…ู„ูˆุง implement ู„ู„ู€ IPaymentStrategy.
  • ูƒู†ุง ุจู†ุนู…ู„ inject (ุญู‚ู†) ู„ู„ู€ concrete strategy ููŠ ุงู„ู€ constructor ุจุชุงุน ุงู„ู€ PaymentProcessor.
// class PaymentProcessor
// {
//     private readonly IPaymentStrategy _paymentStrategy; // Depends on ABSTRACTION
//
//     public PaymentProcessor(IPaymentStrategy paymentStrategy) // DIP happens here via Constructor Injection
//     {
//         _paymentStrategy = paymentStrategy;
//     }
//
//     public void ExecutePayment(Order order)
//     {
//         _paymentStrategy.ProcessPayment(order.TotalAmount); // Uses the abstraction
//     }
// }

ุจูƒุฏู‡ุŒ ูุตู„ู†ุง ุงู„ุงุนุชู…ุงุฏูŠุฉ ุจุชุงุนุฉ ุงู„ู€ PaymentProcessor ุนู† ู†ูˆุน ุงู„ุฏูุน ุงู„ู„ูŠ ู‡ูŠุฌูŠู„ู‡ุŒ ูˆุฎู„ูŠู†ุง ุงู„ุงุชู†ูŠู† (ุงู„ู€ high-level ูˆุงู„ู€ low-level) ูŠุนุชู…ุฏูˆุง ุนู„ู‰ abstraction.

ุฎู„ุงุตุฉ ู…ุจุงุฏุฆ SOLID

ูƒุฏู‡ ุฅุญู†ุง ุดูˆูู†ุง ู…ุน ุจุนุถ ุงู„ุฎู…ุณ ู…ุจุงุฏุฆ ุจุชูˆุน SOLID ูˆุฃู‡ู…ูŠุชู‡ู…:

  1. ุงู„ู€ S - Single Responsibility Principle: ุงู„ู€ class ูŠูƒูˆู† ู„ูŠู‡ุง ุณุจุจ ูˆุงุญุฏ ุจุณ ู„ู„ุชุบูŠูŠุฑ.
  2. ุงู„ู€ O - Open/Closed Principle: ุงู„ู€ code ูŠูƒูˆู† ู…ูุชูˆุญ ู„ู„ุชูˆุณูŠุน (ุจุฅุถุงูุฉ functionality ุฌุฏูŠุฏุฉ ู…ู† ุบูŠุฑ ุชุนุฏูŠู„ ุงู„ูƒูˆุฏ ุงู„ู‚ุฏูŠู…) ูˆู…ู‚ููˆู„ ุนู† ุงู„ุชุนุฏูŠู„ ุงู„ู…ุจุงุดุฑ (ููŠ ุงู„ูƒูˆุฏ ุงู„ู‚ุฏูŠู… ุงู„ู„ูŠ ุดุบุงู„ ูˆุซุงุจุช).
  3. ุงู„ู€ L - Liskov Substitution Principle: ู†ู‚ุฏุฑ ู†ุณุชุฎุฏู… object ู…ู† subclass ู…ูƒุงู† object ู…ู† parent class ู…ู† ุบูŠุฑ ู…ุง ุงู„ุจุฑู†ุงู…ุฌ ูŠุถุฑุจ ุฃูˆ ุณู„ูˆูƒู‡ ูŠุชุบูŠุฑ ุจุดูƒู„ ุบูŠุฑ ู…ุชูˆู‚ุน. ุงู„ู€child ู„ุงุฒู… ูŠุญุชุฑู… โ€œุนู‚ุฏโ€ ุงู„ู€ parent.
  4. ุงู„ู€ I - Interface Segregation Principle: ู…ุชุฌุจุฑุด class ุฅู†ู‡ุง ุชุนู…ู„ implement ู„ู€ methods ู‡ูŠ ู…ุด ู…ุญุชุงุฌุงู‡ุงุ› ู‚ุณู… ุงู„ู€ interfaces ุงู„ูƒุจูŠุฑุฉ ู„ู€ interfaces ุฃุตุบุฑ ูˆุฃูƒุซุฑ ุชุฎุตุตู‹ุง.
  5. ุงู„ู€ D - Dependency Inversion Principle: ุงู„ู€ high-level modules ู…ุชุนุชู…ุฏุด ุนู„ู‰ low-level modules ู…ุจุงุดุฑุฉุŒ ุงู„ุงุชู†ูŠู† ูŠุนุชู…ุฏูˆุง ุนู„ู‰ abstractions. ูˆุงู„ุชูุงุตูŠู„ ู‡ูŠ ุงู„ู„ูŠ ุชุนุชู…ุฏ ุนู„ู‰ ุงู„ู€ abstractionsุŒ ู…ุด ุงู„ุนูƒุณ.

ุชุทุจูŠู‚ ุงู„ู…ุจุงุฏุฆ ุฏูŠ ุจูŠุฎู„ูŠ ุงู„ู€ software ุจุชุงุนู†ุง ุฃุญุณู† ุจูƒุชูŠุฑ ู…ู† ุญูŠุซ ุงู„ุฌูˆุฏุฉุŒ ูˆุณู‡ูˆู„ุฉ ุงู„ุตูŠุงู†ุฉุŒ ูˆุงู„ู‚ุงุจู„ูŠุฉ ู„ู„ุชูˆุณุน ูˆุงู„ุงุฎุชุจุงุฑ.