Welcome to CodeYourCraft, where we learn and grow together! Today, we're diving into the fascinating world of software engineering principles. We'll be exploring the Open-Closed Principle, a fundamental concept that helps us write more flexible, maintainable, and scalable code.
The Open-Closed Principle is a software design principle that states a software entity should be open for extension (i.e., new functionality can be added) but closed for modification (i.e., the existing code should not be changed).
Why is this principle crucial? It helps us maintain code that is easier to understand, more robust, and less prone to errors. Let's delve deeper and see how we can apply it in our code.
To illustrate the Open-Closed Principle, let's consider a simple example of a program that calculates the total cost of items with different tax rates.
Imagine we have a Calculator class that calculates the total cost of items, considering a default tax rate of 10%. Now, we need to extend this class to accommodate two more tax rates, 5% and 15%.
class Calculator:
def __init__(self, price, tax_rate=0.1):
self.price = price
self.tax_rate = tax_rate
def calculate_total(self):
return self.price + (self.price * self.tax_rate)Changing the tax rate for each item would require modifying the existing Calculator class, which goes against the Open-Closed Principle.
To solve this problem, we'll use the Open-Closed Principle by creating an abstract class for tax calculators and subclasses for each tax rate.
class TaxCalculator:
def __init__(self, price):
self.price = price
def calculate_tax_rate(self, tax_rate):
return self.price * tax_rate
class DefaultTaxCalculator(TaxCalculator):
def calculate_total(self):
tax_amount = self.calculate_tax_rate(0.1)
return self.price + tax_amount
class FivePercentTaxCalculator(TaxCalculator):
def calculate_total(self):
tax_amount = self.calculate_tax_rate(0.05)
return self.price + tax_amount
class FifteenPercentTaxCalculator(TaxCalculator):
def calculate_total(self):
tax_amount = self.calculate_tax_rate(0.15)
return self.price + tax_amountNow, when we want to calculate the total cost for an item with a different tax rate, we simply create an instance of the appropriate tax calculator class.
def main():
item_price = 100
# Default tax rate
default_tax_calculator = DefaultTaxCalculator(item_price)
print(f"Total cost with default tax: ${default_tax_calculator.calculate_total()}")
# Five percent tax rate
five_percent_tax_calculator = FivePercentTaxCalculator(item_price)
print(f"Total cost with 5% tax: ${five_percent_tax_calculator.calculate_total()}")
# Fifteen percent tax rate
fifteen_percent_tax_calculator = FifteenPercentTaxCalculator(item_price)
print(f"Total cost with 15% tax: ${fifteen_percent_tax_calculator.calculate_total()}")
if __name__ == "__main__":
main()By applying the Open-Closed Principle, we've created a more flexible and maintainable codebase that allows for easy extension of tax rates without modifying the existing code.
Which design principle does the example above follow?
Thanks for joining us in exploring the Open-Closed Principle! Stay tuned for more exciting topics, and happy coding! 💻🙌