Sobes.tech
Junior — Middle

What design pattern does the decorator pattern implement?

sobes.tech AI

Answer from AI

The decorator pattern implements a design style based on dynamically extending the functionality of an object without changing its original code. This is achieved by wrapping the object in another decorator object that adds new capabilities.

In Python, decorators are often used to modify the behavior of functions or methods:

def decorator(func):
    def wrapper(*args, **kwargs):
        print("Before calling the function")
        result = func(*args, **kwargs)
        print("After calling the function")
        return result
    return wrapper

@decorator
def say_hello():
    print("Hello!")

say_hello()

Here, decorator extends the behavior of say_hello without changing its code.

What design pattern does the decorator pattern… - sobes.tech