SOLID Design Principles: Transform Your Code Quality

class Employee:  
    def __init__(self, name, position):  
        self.name = name  
        self.position = position  
  
    def calculate_payroll(self):  
        # Logic for payroll calculation  
  
    def generate_report(self):  
        # Logic for generating report  

Solution:

class Employee:  
    def __init__(self, name, position):  
        self.name = name  
        self.position = position  
  
class PayrollCalculator:  
    def calculate_payroll(self, employee):  
        # Logic for payroll calculation  
  
class ReportGenerator:  
    def generate_report(self, employee):  
        # Logic for generating report  
class Notification:  
    def send(self, message, type):  
        if type == "email":  
            # Send email  
        elif type == "sms":  
            # Send SMS  

Solution:

class Notification:  
    def send(self, message):  
        pass  
  
class EmailNotification(Notification):  
    def send(self, message):  
        # Send email  
  
class SMSNotification(Notification):  
    def send(self, message):  
        # Send SMS  
  
class NotificationService:  
    def send_notification(self, notification, message):  
        notification.send(message)  

By using inheritance and polymorphism, we can extend the Notification class with new types (e.g., EmailNotification, SMSNotification) without modifying the existing Notification class. The NotificationService class can send any type of notification, adhering to OCP.

class Bird:  
    def fly(self):  
        pass  
  
class Sparrow(Bird):  
    def fly(self):  
        # Sparrow flies  
  
class Ostrich(Bird):  
    def fly(self):  
        raise NotImplementedError("Ostriches cannot fly")  

The Ostrich class violates LSP because it throws an exception when the fly method is called. This means you cannot use Ostrich objects in place of Bird objects without causing errors.

Solution:

class Bird:  
    def move(self):  
        pass  
  
class Sparrow(Bird):  
    def move(self):  
        # Sparrow flies  
  
class Ostrich(Bird):  
    def move(self):  
        # Ostrich runs  
class MultiFunctionDevice:  
    def print(self, document):  
        pass  
  
    def scan(self, document):  
        pass  
  
    def fax(self, document):  
        pass  
  
class OldPrinter(MultiFunctionDevice):  
    def print(self, document):  
        # Print document  
  
    def scan(self, document):  
        raise NotImplementedError("This printer cannot scan")  
  
    def fax(self, document):  
        raise NotImplementedError("This printer cannot fax")  

Solution:

class Printer:  
    def print(self, document):  
        pass  
  
class Scanner:  
    def scan(self, document):  
        pass  
  
class Fax:  
    def fax(self, document):  
        pass  
  
class OldPrinter(Printer):  
    def print(self, document):  
        # Print document
class LightBulb:  
    def turn_on(self):  
        # Logic to turn on the light bulb  
  
class Switch:  
    def __init__(self, bulb):  
        self.bulb = bulb  
  
    def operate(self):  
        self.bulb.turn_on()  
class Switchable:  
    def turn_on(self):  
        pass  
  
class LightBulb(Switchable):  
    def turn_on(self):  
        # Logic to turn on the light bulb  
  
class Fan(Switchable):  
    def turn_on(self):  
        # Logic to turn on the fan  
  
class Switch:  
    def __init__(self, device):  
        self.device = device  
  
    def operate(self):  
        self.device.turn_on()  
  
# Usage  
bulb = LightBulb()  
switch = Switch(bulb)  
switch.operate()  
  
fan = Fan()  
switch = Switch(fan)  
switch.operate()  

You May Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *

×