Erdem Özkol

In Delegation pattern, we delegate an operation to a class instead of a method. Instead of we do operation inside a class, we just instantiate another class and set our property to that instance of the class we created.

Wikipedia: In software engineering, the delegation pattern is a design pattern in object-oriented programming where an object, instead of performing one of its stated tasks, delegates that task to an associated helper object.

Example:

Just think we are making a animation with different characters, and each character has different walking styles. So we have two class one is animation and other is character. To achieve this we create a character instace and we pass character instace to animation class. In the animation everything about character will be manage by character instace. Let's see example code bellow.

class Animation:
    def __init__(self, character, *args, **kwargs):
        # here we set character property of instance 
        self.character = character

    def play(self):
        self.character.walk()


class Character:

    def __init__(self, name, *args, **kwargs):
        self.name = name

    def walk(self):
        print "%s walking on the screen." % self.name


mike = Character(name="Mike")
animation = Animation(character=mike)
animation.play()

>>> "Mike walking on the screen."

john = Character(name="John")
animation = Animation(character=john)
animation.play()

>>> "John walking on the screen."

References: