Menu
CoddyTech

Recap: Pluggable Channels

Lesson 42 of 48 in Coddy's Python for Java Developers course.

challenge icon

Challenge

Beginner

Implement Email.send(text) and Sms.send(text), returning email: or sms: plus text. Dispatcher(channels) stores a shallow copy of the list; dispatch(text) returns each channel's send result in order. The driver reads email/sms/log names and a message, supplies log, clears the original list, then prints results or none if no channels. Repeated channels remain repeated. This is the Python counterpart of a Java interface-based dispatcher with a defensive ArrayList copy; no real messages are sent.

Editable files: email.py, sms.py, dispatcher.py. Keep the provided imports and the required API names. The locked driver.py supplies execution and checks; do not edit it.

Implement the named definitions in the editable modules without input or printing. The driver owns input/output and may call the APIs again on fresh data without producing extra output.

Try it yourself

from email import Email
from sms import Sms
from dispatcher import Dispatcher

class Log:
    def send(self, text):
        return "log:" + text

factories = {"email": Email, "sms": Sms, "log": Log}
channels = [factories[name]() for name in input().split()]
text = input()
dispatcher = Dispatcher(channels)
channels.clear()
results = dispatcher.dispatch(text)
if not results:
    print("none")
for result in results:
    print(result)

All lessons in Python for Java Developers

Practice on your own: Online Python compiler