Showing posts with label Open/Closed Principle. Show all posts
Showing posts with label Open/Closed Principle. Show all posts

Friday, September 20, 2024

SOLID Principles: Open/Closed Principle



In this series, I will highlight some areas where you can improve the quality of your code during the development process. In the first article, I explained why code quality is paramount. Then I introduced briefly some ideas that would help you improve the quality of your code. I will provide more details on these points in future articles. Some concepts will be illustrated with examples in Java and Python. I will begin this journey with you by introducing the SOLID principles in this article. They are some of the concepts that will help you improve the quality of your code and boost your abstract thinking in the object-oriented paradigm. After introducing the Single Responsibility Principle in the previous article, I will elucidate the Open/Closed Principle in this article, with examples in Java and Python.


1. Open/Closed Principle (OCP)

This principle means that software entities such as modules, classes, or functions should be open to extension but closed to modification. In other words, it is allowed to extend the behavior of these entities but it is prohibited to modify their source code. The reasons behind this principle are:


- Existing entities are already well-tested. Therefore, any modification could lead to bugs and unexpected behavior. It is better to avoid changing what already works well as much as possible. You can extend it whenever you need instead of modifying it.

- Testers do not need to test the entire flow, just the added parts.

- Maintenance remains easy.

- Single Responsibility Principle will be respected.


2. Example of OCP in Java

To see this principle in action, consider a well-known example that calculates the total price of a shopping cart. As a shopping cart contains products, we create an interface named ‘Product’ with a single ‘getPrice()’ method. Next, we suggest the ‘Item’ class to implement this interface. This class has three fields ‘name’, ‘description’, and ‘price’. We have overridden the ‘getPrice()’ method to return the price value.


public interface Product {

    double getPrice();

}


public class Item implements Product {

    private final String name;

    private final String description;

    private final double price;


    public Item(String name, String description, double price) {

        this.name = name;

        this.description = description;

        this.price = price;

    }


    @Override

    public double getPrice() {

        return price;

    }

}


We can now create the ‘ShoppingCart’ class with some fields like the currency used and the list of products to purchase. These two fields must be initialized at the time of instantiation of the class. We need two methods to fulfill the functionality of a shopping cart, ‘addProduct’ and ‘getTotalPrice’. The ‘addProduct’ method allows you to add a product to the shopping cart.


The ‘getTotalPrice()’ method calculates the total price of a collection of objects of the ‘Item’ class in a shopping cart. If the list containing the items is empty, the method returns 0.


If the products list is not empty, it maps each item within the list to its price, then reduces the stream to a single value (the sum of all prices) using Double::sum. Finally, it uses the ‘round’ method to round the calculated total price to two decimal places.


public class ShoppingCart {

    protected final String currency;

    protected final List<Product> products;

    public ShoppingCart(String currency, List<Product> products) {

        this.currency = currency;

        this.products = products;

    }

    public String getCurrency() {

        return currency;

    }


    public void addProduct(Product product) {

        products.add(product);

    }


    public double getTotalPrice() {

        if (products.isEmpty()) return 0;

        //Calculate the total price

        double totalPrice = products.stream()

.map(product -> product.getPrice()).reduce(Double::sum).get();

        //Round the price to 2 decimal places

        return round(totalPrice, 2);

    }

}


It is common to apply discounts on products. But, from an architectural point of view, it is not a good idea to be applied directly to the ‘ShoppingCart’ but to its sub-class. Here the adherence to the Open/Closed Principle comes into play. We do not open the ‘ShoppingCart’ class to add this behavior, but we extend it with a new sub-class to implement the new feature.


Now let’s create a new class called ‘DiscountedShoppingCart’ to implement the new behavior of applying discounts to specific items based on certain criteria. This class has two fields ‘priceLimit’ and ‘discount’ which need to be initialized during the class instantiation. The ‘priceLimit’ property determines the maximum price of a product, which must not be exceeded to apply a discount. The ‘discount’ property is the percentage of the discount.


The ‘DiscountedShoppingCart’ class will calculate the total price differently than its super-class. The sub-class will take into consideration the discount rule. Therefore, ‘DiscountedShoppingCart’ will replace the ‘getTotalPrice’ method with a new behavior.


The ‘getTotalPrice’ method calculates the total price of a shopping cart with a discount applied when certain conditions are met. It first calls the ‘getTotalPrice’ method of the super-class to get the base total price. It returns the base total price if the list of products in the shopping cart is empty. Otherwise, it calculates a discount based on the price of each product and a price limit (‘priceLimit’). It applies a discount rate (discount) to the price of each product if it is less than or equal to the ‘priceLimit’ value.

The discount obtained is subtracted from the base total price. The final total price is rounded to 2 decimal places using the ‘round’ and returned as a result.


public class DiscountedShoppingCart extends ShoppingCart {

    private final double priceLimit;

    private final double discount;

    public DiscountedShoppingCart(String currency, List<Product> products, 

double priceLimit, double discount) {

        super(currency, products);

        this.priceLimit = priceLimit;

        this.discount = discount;

    }


    @Override

    public double getTotalPrice() {

        double totalPrice = super.getTotalPrice();

        if (products.isEmpty()) return totalPrice;

        //Calculate the discount

        double discount = products.stream()

.filter(p->p.getPrice()<=this.priceLimit&&this.priceLimit>0)

                              .map(p->p.getPrice()*this.discount).reduce(Double::sum).get();

        totalPrice -= discount;

        //Round the price to 2 decimal places

        return round(totalPrice, 2);

    }

}


We suggest the ‘ShoppingCartDemo’ class, a simple demonstration of a shopping cart system. We create three items, put them in a shopping cart, and then print out the total price of the shopping cart in euros. We then create a discounted shopping cart, add the same items, and print the total discounted price. Finally, we print out the discounted total price.


public class ShoppingCartDemo {

    public static void main(String[] args) {

        //Create some items

        Product item1 = new Item("Shampoo", "Herbal essences, 350ml for Dry hair", 10.0);

        Product item2 = new Item("Cream", "Nivea Soft, 100ml", 31.99);

        Product item3 = new Item("Tablet", "iPad 6 (2020) | 9.7", 149.99);

        //Create a shopping cart

        ShoppingCart shoppingCart = new ShoppingCart("EUR", new ArrayList<>());

        shoppingCart.addProduct(item1);

        shoppingCart.addProduct(item2);

        shoppingCart.addProduct(item3);

        System.out.println("Total price: " + shoppingCart.getTotalPrice() 

+ " " + shoppingCart.getCurrency());

        //Create a discounted shopping cart

        DiscountedShoppingCart discShoppingCart = 

new DiscountedShoppingCart("EUR", new ArrayList<>(), 50.0, 0.25);

        discShoppingCart.addProduct(item1);

        discShoppingCart.addProduct(item2);

        discShoppingCart.addProduct(item3);


        System.out.println("Discounted Total price: " + discShoppingCart.getTotalPrice() 

+ " " + discShoppingCart.getCurrency());

    }

}


The execution of this code gives us the following results:


Total price: 191.98 EUR

Discounted Total price: 181.48 EUR


In summary, the ‘ShoppingCart’ class is closed for modification and the ‘DiscountedShoppingCart’ class is open for extension to add new functionality to apply discounts to certain products, without modifying the existing ‘ShoppingCart’ class.


3. Example of OCP in Python

We can demonstrate the open/closed principle in Python using a famous example of the ‘Account’ and ‘SaveAccount’ classes. A simple implementation of the ‘Account’ class can have two properties, ‘acc_num’ for the account name and the ‘balance’ property for the amount of money available.


Two methods are necessary for each account, ‘deposit’ and ‘withdraw’. The ‘deposit’ method consists of saving money in the account. The ‘withdraw’ method involves withdrawing money from the account. To show the account number and balance, we define the ‘show_state’ method.


class Account:

    def __init__(self, acc_num, balance):

        self.acc_num = acc_num

        self.balance = balance


    def deposit(self, amount):

        self.balance += amount


    def withdraw(self, amount):

        if amount <= self.balance:

            self.balance -= amount

        else:

            print("Your balance is not enough.")


    def show_state(self):

        return f"Account number: {self.acc_num}, Balance: {self.balance}"


To keep the ‘Account’ class representing the base account that encapsulates the common behavior of all accounts, we need to keep it closed and avoid any changes that might disrupt this correct functionality. But bank customers need other services, like saving money for a long time and earning interest. To do this, we extend our ‘Account’ class with a new class to complete this new feature.


Let’s create the ‘SaveAccount’ class that inherits from the ‘Account’ class and adds an interest rate property. We define a new method with the name ‘add_interest’ to calculate interest based on the balance and interest rate and add it to the balance.


We can say that this example adheres to the Open/Closed Principle because it extends the system with the ‘SaveAccount’ class without modifying the existing ‘Account’ class.


class SaveAccount(Account):

    def __init__(self, acc_num, balance, interest_rate):

        super().__init__(acc_num, balance)

        self.interest_rate = interest_rate


    def add_interest(self):

        interest = self.balance * self.interest_rate / 100

        self.balance += interest


    def show_state(self):

        return f"Saved " + super().show_state() + f", Interest Rate: {self.interest_rate}%"


# Some example operations on account

acc = Account("A952 7693 4621 7834", 7500.0)

print(acc.show_state())

acc.withdraw(350)

print(acc.show_state())

acc.deposit(1500.5)

print(acc.show_state())


print('')


# Some example of operations on save account

save_acc = SaveAccount("A5793 7693 4621 7834", 5700.0, 2.5)

print(save_acc.show_state())

save_acc.add_interest()

print(save_acc.show_state())


Running the above code gives us the following result:


Account number: A952 7693 4621 7834, Balance: 7500.0

Account number: A952 7693 4621 7834, Balance: 7150.0

Account number: A952 7693 4621 7834, Balance: 8650.5


Saved Account number: A5793 7693 4621 7834, Balance: 5700.0, Interest Rate: 2.5%

Saved Account number: A5793 7693 4621 7834, Balance: 5842.5, Interest Rate: 2.5%

SOLID Principles: Single Responsibility Principle



In this series, I will highlight some areas where you can improve the quality of your code during the development process. In the first article, I explained why code quality is paramount. Then I introduced briefly some ideas that would help you improve the quality of your code. I will provide more details on these points in future articles. Some concepts will be illustrated with examples in Java and Python. I will begin this journey with you by introducing the SOLID principles in this article. They are some of the concepts that will help you improve the quality of your code and boost your abstract thinking in the object-oriented paradigm. I will elucidate the first principle (Single Responsibility Principle), with examples in Java and Python.


1. SOLID principles

The SOLID principles are a collection of object-oriented design concepts for building a solid object-oriented software architecture. These principles help developers create software that are more readable, flexible, extensible, and maintainable.


It was Robert C. Martin who first introduced these principles. But it was Michael Feathers, who came up with the acronym SOLID and reworked these principles. The acronym SOLID stands for:


    • S: Single Responsibility Principle

    • O: Open/closed Principle

    • L: Liskov Substitution Principle

    • I: Interface Segregation Principle

    • D: Dependency Inversion Principle


2. Advantages of Using SOLID Principles

Compliance with SOLID principles has a big impact on the quality of your software. We can mention some benefits of using these principles as follows:


    • Reduce the complexity of the code.

    • Increase readability, extensibility, and maintenance.

    • Increase flexibility and reusability.

    • Reduce tight coupling.

    • Reduce errors for better testability.


We’ll show how using SOLID principles helps you develop a code with better quality that confirms the achievement of the advantages described above. In this series, we will explain each principle individually and illustrate with an example how each improves certain aspects of the software. But, we focus only on the Single Responsibility Principle (SRP)  in this article.


3. Single Responsibility Principle (SRP)

This principle means that a class should only have one responsibility. In other words, each class should only contain one feature or behavior. This principle helps you ensure code quality by preserving some of the code quality features we mention via the following benefits:


Testability: It is easy to define test cases for a class with a single responsibility.

Loose coupling: A class implementing SRP has few dependencies because it only encapsulates a single functionality.

Readability: This principle ensures that classes adhering to it are small, well-organized, and easy to understand.

Separation of Concerns: This principle preserves the concept of separation of concerns.

Maintainability: The ability to update the software is high.

Extensibility: Adding new features is easy.


4. Example of SRP in Java

To explain how a class complies with SRP or not, we create an illustrative class ‘Calculator’ with a few methods that we named ‘add’, ‘div’, ‘isMultipleOf’, and ‘removeWSpace’.

We defined the first three methods for arithmetic operations and followed strong logic to put them together (strong cohesion). 

The fourth operation removes white spaces from a variable of type string. That means it has a weak cohesion with the ‘add’, ‘div’, and ‘isMultipleOf’. 


Consider one responsibility according to our requirements, which is achieving mathematical operations. Therefore, this example violates the SRP because it does not encapsulate this sole responsibility. This could result in the loss of the benefits of the SRP mentioned above.


public class Calculator {

    public Calculator() { }

    public static int add(int x, int y) {

        return x + y;

    }

    public static int div(int x, int y) {

        return x / y;

    }

    public static boolean isMultipleOf(int x, int y) {

        if (x % y == 0) {

            return true;

        } else {

            return false;

        }

    }

    public static String removeWSpace(String inputStr) {

        return inputStr.replaceAll("\\s+","");

    }

}


5. Example of SRP in Python

Consider a class in Python that violates the SRP according to our requirements. We define a class named ‘ListExtremum’ which encapsulates four methods ‘find_max’, ‘find_min’, ‘find_max_dict’, and ‘find_min_dict’. 

The ‘find_max’ and ‘find_min’ methods look for the max and the min values respectively in a list. The ‘find_max_dict’ and ‘find_min_dict’ methods return the max and the min values respectively in a dictionary.

This example violates the SRP because it fulfills more than one responsibility. The first responsibility consists of calculating the extrema (minimum and maximum) of a list. The second one is to calculate the extrema of a dictionary.


class ListExtremum:

    def __init__(self, num_list, dict):

        self.num_list = num_list

        self.dict = dict


    def find_max(self):

        try:

            maximum = self.num_list[0]

            for number in self.num_list:

                if number > maximum:

                    maximum = number

            return maximum

        except TypeError:

            print("Method find_max - Error empty list")

        except IndexError:

            print("Method find_max - Error empty list")


    def find_min(self):

        try:

            minimum = self.num_list[0]

            for number in self.num_list:

                if number < minimum:

                    minimum = number

            return minimum

        except TypeError:

            print("Method find_min - Error empty list")

        except IndexError:

            print("Method find_min - Error empty list")


    def find_max_dict(self):

        try:

            max_key = max(self.dict, key=self.dict.get)

            max_value = self.dics[max_key]

            return max_value

        except AttributeError:

            print("Method find_max_dict - Error empty dictionary")

        except ValueError:

            print("Method find_max_dict - Error empty dictionary")


    def find_min_dict(self):

        try:

            min_key = min(self.dics, key=self.dics.get)

            min_value = self.dics[min_key]

            return min_value

        except AttributeError:

            print("Method find_min_dict - Error empty dictionary")

        except ValueError:

            print("Method find_min_dict - Error empty dictionary")




We explain what each method does in this code.
Constructor ‘__init__’: This code defines a constructor function (__init__) for the ‘ListExtremum’ class. It takes two arguments (‘num_list’ and ‘dict’) and assigns them to instance variables (‘self.num_list’ and ‘self.dict’).
Method ‘find_max’: this method searches for the maximum number in ‘self.num_list’. It initializes the maximum variable with the first element of ‘self.num_list’. Then, it iterates over the list and updates the maximum variable when it finds a number greater than the current maximum. It handles ‘TypeError’ and ‘IndexError’ exceptions and prints an error message if the list is None or empty.
Method ‘find_min’: This method returns the minimum value in ‘self.num_list’. It initializes the minimum variable with the element of ‘self.num_list’ at the first index. After that, it iterates over the list and updates the minimum variable when it finds a number smaller than the current minimum. It handles potential errors for an empty list by catching ‘TypeError’ and ‘IndexError’ exceptions and printing an error message.
Method ‘find_max_dict’: This method finds the key-value pair with the highest value in a dictionary. It uses the max function with the key parameter set to ‘self.dict.get’ to find the key with the highest value. If the dictionary is empty, it catches the ‘AttributeError’ and ‘ValueError’ exceptions and prints an error message.
Method ‘find_min_dict’: This method aims to find the key-value pair with the minimum value in a dictionary. It uses the min function to find the minimum key based on the values. Then, it retrieves the corresponding value from the dictionary. Like ‘find_max_dict’, this method catches the ‘AttributeError’ and ‘ValueError’ exceptions and prints an error message, if the dictionary is empty.
We should split this code into parts to make this class adhere to the SRP. The first part will implement operations applied to a list. The second part contains the operations of dictionaries. In this case, we get two classes called ‘ListExtremum’ and ‘DictExtremum’. 
The ‘ListExtremum’ class contains the ‘find_max’ and ‘find_min’ methods to calculate the max and min value of a list. On the other hand, the ‘DictExtremum’ class contains the ‘find_max_dict’ and ‘find_min_dict’ methods to get the max and min values of a dictionary.
class ListExtremum:
    def __init__(self, num_list):
        self.num_list = num_list
    def find_max(self):
        try:
            maximum = self.num_list[0]
            for number in self.num_list:
                if number > maximum:
                    maximum = number
            return maximum
        except TypeError:
            print("Method find_max - Error empty list")
        except IndexError:
            print("Method find_max - Error empty list")
    def find_min(self):
        try:
            minimum = self.num_list[0]
            for number in self.num_list:
                if number < minimum:
                    minimum = number
            return minimum
        except TypeError:
            print("Method find_min - Error empty list")
        except IndexError:
            print("Method find_min - Error empty list")

class DictExtremum:
    def __init__(self,  dict):
        self.dict = dict
    def find_max_dict(self):
        try:
            max_key = max(self.dict, key=self.dict.get)
            max_value = self.dict[max_key]
            return max_value
        except AttributeError:
            print("Method find_max_dict - Error empty dictionary")
        except ValueError:
            print("Method find_max_dict - Error empty dictionary")
    def find_min_dict(self):
        try:
            max_key = min(self.dics, key=self.dics.get)
            max_value = self.dics[max_key]
            return max_value
        except AttributeError:
            print("Method find_min_dict - Error empty dictionary")
        except ValueError:
            print("Method find_min_dict - Error empty dictionary")

We can call this code using some examples as follows:
print("**********************************************")
print("Calculate max and min values in None list and None dictionary")
print("")
list_extremum_calc = ListExtremum(None)
print("Max value in the list: ", list_extremum_calc.find_max())
print("Min value in the list: ", list_extremum_calc.find_min())
dict_extremum_calc = DictExtremum(None)
print("Max value in the dictionary: ", dict_extremum_calc.find_max_dict())
print("Min value in the dictionary: ", dict_extremum_calc.find_min_dict())
print("")
print("**********************************************")
print("Calculate max and min values in empty list and empty dictionary")
list_extremum_calc = ListExtremum([])
print("Max value in the list: ", list_extremum_calc.find_max())
print("Min value in the list: ", list_extremum_calc.find_min())
dict_extremum_calc = DictExtremum({})
print("Max value in the dictionary: ", dict_extremum_calc.find_max_dict())
print("Min value in the dictionary: ", dict_extremum_calc.find_min_dict())
print("")
print("**********************************************")
num_list = [18, 20, 25, -16, 27]
temp_dict = {'Vienna': 10, 'Paris': 11, 'Rome': 15, 'Algiers': 16}
print("num_list: ", num_list)
print("temp_dict: ", temp_dict)
print("Calculate max and min values in num_list and temp_dict")
print("")
list_extremum_calc = ListExtremum(num_list)
print("Max value in the list: ", list_extremum_calc.find_max())
print("Min value in the list: ", list_extremum_calc.find_min())
dict_extremum_calc = DictExtremum(temp_dict)
print("Max value in the dictionary: ", dict_extremum_calc.find_max_dict())
print("Min value in the dictionary: ", dict_extremum_calc.find_min_dict())

The execution of this code gives us the following result:
**********************************************
Calculate max and min values in None list and None dictionary
Method find_max - Error empty list
Max value in the list:  None
Method find_min - Error empty list
Min value in the list:  None
Method find_max_dict - Error empty dictionary
Max value in the dictionary:  None
Method find_min_dict - Error empty dictionary
Min value in the dictionary:  None
**********************************************
Calculate max and min values in empty list and empty dictionary
Method find_max - Error empty list
Max value in the list:  None
Method find_min - Error empty list
Min value in the list:  None
Method find_max_dict - Error empty dictionary
Max value in the dictionary:  None
Method find_min_dict - Error empty dictionary
Min value in the dictionary:  None
**********************************************
num_list:  [18, 20, 25, -16, 27]
temp_dict:  {'Vienna': 10, 'Paris': 11, 'Rome': 15, 'Algiers': 16}
Calculate max and min values in num_list and temp_dict
Max value in the list:  27
Min value in the list:  -16
Max value in the dictionary:  16
Method find_min_dict - Error empty dictionary
Min value in the dictionary:  None

Blog Posts

Enhancing Performance of Java-Web Applications

Applications built with a Java back-end, a relational database (such as Oracle or MySQL), and a JavaScript-based front-end form a common and...