PythonPlacement Classroomnotes 07/Mar/2022

DRY Principle

  • DRY stands for “Don’t Repeat Yourself” a basic principle of software development aimed at reducing repetition of information.

SOLID

  • These are first 5 principles of Object Oriented Design.
  • SOLID Stands for
    • S: Single Responsiblity Principle
    • O: Open-Closed Principle
    • L: Liskov Substitution Principle
    • I: Interface Seggregation Principle
    • D: Depenedency Inversion Principle

Single Responsibility Principle

  • Lets look at the following Code
  • Example 1:
class Car:
    def __init__(self, model):
        """
        initalizer
        """
        self.model = model

    def get_model(self):
        """
        This method will return the model
        """
        return self.model

    def save(self, car: Car):
        """
        This method will save the car to the database
        """
        pass
  • The above Example is violating the Single Responsibility Principle. The initializer and get_model are managing Car properties while the save is saving the car to database.
  • Right approach would be
class Car:
    def __init__(self, model):
        """
        initalizer
        """
        self.model = model

    def get_model(self):
        """
        This method will return the model
        """
        return self.model

class CarDB:
    def save(self, car):
        pass

  • Example 2
class Page:
    def __init__(self, title):
        self._title = title

    def get_title(self):
        return self._title

    def set_title(self, title):
        self._title = title

    def format_json(self):
        pass
  • Here we have violated Single Responsibility Prinicple
class Page:
    def __init__(self, title):
        self._title = title

    def get_title(self):
        return self._title

    def set_title(self, title):
        self._title = title


class JsonPageFormatter:
    def format_json(page: Page):
        pass

Open/Closed Principle

  • Classes should be open for extensions and closed for modifications
  • Consider the following example
class Car:
    def __init__(self, model):
        """
        initalizer
        """
        self.model = model

    def get_model(self):
        """
        This method will return the model
        """
        return self.model

cars = [
    Car('HatchBack'),
    Car('SUV'),
    Car('Coupe')
]

def show_number_of_seats(cars: list):
    for car in cars:
        if car.model == "HatchBack":
            return 5
        elif car.model == "SUV":
            return 7
        elif car.model == "Coupe":
            return 2
  • As per open closed Principle
class Car:
    def __init__(self, model):
        """
        initalizer
        """
        self.model = model

    def get_model(self):
        """
        This method will return the model
        """
        return self.model

class HatchBack(Car):
    def number_of_seats(self):
        return 4

class SUV(Car):
    def number_of_seats(self):
        return 7

class Coupe(Car):
    def number_of_seats(self):
        return 2

cars = [
    HatchBack('swift'),
    SUV('XUV500'),
    Coupe('Ford Mustang')
]

def show_number_of_seats(cars: list):
    for car in cars:
        return car.number_of_seats()

  • Example 2:
class Discount:
    def __init__(self, customer, price):
        self.customer = customer
        self.price = price

    def give_discount(self):
        if self.customer == "nonmember":
            return 0.01 # 1 %
        elif self.customer == "premium":
            return 0.15 # 15%
        elif self.customer == "standard":
            return 0.07 # 7%
  • Best way to do this
class Discount:
    def __init__(self, customer, price):
        self.customer = customer
        self.price = price

    def give_discount(self):
        return 0

class NonMemberDiscount(Discount):
    def give_discount(self):
        return 0.01


class PremiumDiscount(Discount):
    def give_discount(self):
        return 0.15


class StandardDiscount(Discount):
    def give_discount(self):
        return 0.07

Design Patterns

  • These are typical solutions to common problems in software design.
  • Each pattern is like a blueprint that you can customize to solve a particular problem
  • The patterns are divided into three categories
    • Creational Patterns:
      • They provide object creation mechanisms that increase flexibility and resue of existing code
    • Structural Patterns:
      • They help you compose group of objects into larger structures, such as complex user interfaces or accounting data.
    • Behavioral Patterns:
      • They help you deifne the communication between objects in your system and control the flow in a complex program.
  • Most of the Design Patterns favor composition over inheritance

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About continuous learner

devops & cloud enthusiastic learner