The Tech World

Replace Method with Method Object | Drawbacks and Resolving Strategy

The Method to Replace By enclosing difficult methods within a different class, the object refactoring technique can be used to enhance the design and organization of these methods. This refactoring technique is especially helpful when a function is too complicated or huge, making it challenging to comprehend, maintain, or test. Replace Method with Method Object is the technique that makes the code cleaner and clearer.

Ways to Apply the Replace Method with Method Object Refactoring Technique?

The Replace Method with Method Object refactoring approach is explained in detail here:

Using the above method we can apply Replace Method with Method Object easily.

Problem

The local variables in your lengthy function are so intertwined that the extract technique won’t work.

Before Refactoring

class Order:
    def calculate_total(self, items):
        total = 0
        for item in items:
            if item['price'] > 100:
                total += item['price'] * 0.9
            else:
                total += item['price']
        return total

order = Order()
items = [{'name': 'Item 1', 'price': 50}, {'name': 'Item 2', 'price': 120}]
total = order.calculate_total(items)
print(f"Total: {total}")

Solution

To convert the local variables into class fields, create a separate class for the function. After that, the method can be split up into different methods that are members of the same class.

After Refactoring

class OrderCalculator:
    def __init__(self, items):
        self.items = items
        self.total = 0

    def calculate_total(self):
        for item in self.items:
            if item['price'] > 100:
                self.total += item['price'] * 0.9
            else:
                self.total += item['price']
        return self.total

class Order:
    def calculate_total(self, items):
        calculator = OrderCalculator(items)
        return calculator.calculate_total()

order = Order()
items = [{'name': 'Item 1', 'price': 50}, {'name': 'Item 2', 'price': 120}]
total = order.calculate_total(items)
print(f"Total: {total}")

In this example, the original code calculates the total price of a list of items based on certain conditions. The calculate_total method in the Order class handles this logic.

After applying the refactoring technique, the code is split into two classes: OrderCalculator and Order. The OrderCalculator class is responsible for calculating the total price, and the Order class serves as a wrapper that instantiates the calculator and delegates the calculation.

By encapsulating the calculation logic within the OrderCalculator class, the code becomes more modular and easier to understand. The calculate_total method in OrderCalculator operates on instance variables (items and total), and external dependencies are passed through the constructor. The Order class simply creates an instance of OrderCalculator and calls its calculate_total method.

Drawbacks of Replace Method with Method Object technique:

Here are some specific drawbacks associated with this Replace Method with Method Object refactoring technique:

Resolving Strategies

To mitigate the drawbacks of the Replace Method with Method Object refactoring technique, you can employ several resolving strategies:

You can view our other blogs on:

Exit mobile version