Delegation

noun

say it: “del-uh-GAY-shun” /ˌdɛləˈɡeɪʃən/

An object taking a job and handing it, or part of it, to another object it holds.

What is delegation?

Delegation is when an object takes a job and hands it, or part of it, to another object it holds. The caller talks to one object and never sees who does the work. A bike rental adds its own $2 helmet fee and hands the rest of its price to the bike it holds.

class Rental:
    def __init__(self, bike, hours):
        self.bike = bike
        self.hours = hours

    def price(self):
        return self.bike.price(self.hours) + 2  # the bike's part is handed on

In a sentence

  • “The rental uses delegation: it adds the helmet fee and asks its bike for the rest.”

Level: Apprentice · More OOP terms at this level

Updated