Menu
CoddyTech

Replaceable Collaborators

Lesson 38 of 48 in Coddy's Python for C# Developers course.

Delegate to a collaborator that can be replaced.

C# reference excerpt

Executable statements use C# top-level program style; type-only examples are declarations. System names assume the usual using System directive.

interface IRate { int Cost(int units); }
class Bill {
    public IRate Rate;
    public Bill(IRate rate) { Rate = rate; }
    public int Total(int units) => Rate.Cost(units);
}

Python

class Bill:
    def __init__(self, rate):
        self.rate = rate
    def total(self, units):
        return self.rate.cost(units)

What changes

C# commonly declares a collaborator interface. This Python class calls the supplied object by behavior without requiring an interface declaration. Store the collaborator and delegate at call time so replacement changes later results. The bill owns coordination; the rate owns its pricing calculation. Imports organize implementations without forcing a shared base class.

Transfer mistake

Do not copy the collaborator formula into Bill or cache its initial result. A different object with a cost method may implement a different formula and still satisfy the expected behavior.

Your task

Implement Rate(cents) with cost(units) returning cents * units. Implement UsageBill(rate) storing the collaborator as rate and total(units) delegating to its current cost method. The driver reads rate, units, replacement rate, and bonus as four nonnegative integers. It prints the original total, replaces the collaborator with a supplied object computing replacement_rate * units + bonus, then prints the new total.

challenge icon

Challenge

Beginner

Implement Rate(cents) with cost(units) returning cents * units. Implement UsageBill(rate) storing the collaborator as rate and total(units) delegating to its current cost method. The driver reads rate, units, replacement rate, and bonus as four nonnegative integers. It prints the original total, replaces the collaborator with a supplied object computing replacement_rate * units + bonus, then prints the new total.

Editable implementation files: rate.py, usage_bill.py. The locked driver.py is the supplied launcher or input/output harness and must remain unchanged.

Keep the supplied imports and exact API names. Return results or raise the required exceptions; do not read input or print from these implementation modules.

Try it yourself

from rate import Rate
from usage_bill import UsageBill

class BonusRate:
    def __init__(self, cents, bonus):
        self.cents = cents
        self.bonus = bonus

    def cost(self, units):
        return self.cents * units + self.bonus

cents, units, replacement, bonus = [int(input()) for _ in range(4)]
bill = UsageBill(Rate(cents))
print(bill.total(units))
bill.rate = BonusRate(replacement, bonus)
print(bill.total(units))
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Python for C# Developers

Practice on your own: Online Python compiler