Final challenge: refactor a family
After this: Count, predict, complete and explain the refactor of a notifier family into a notifier that holds channels.
A shop sends alerts to its customers: "paid", "shipped". Some customers want email, some want SMS, some want both. The first version of the program has one subclass of Notifier for each choice.
- EmailOnly inherits from Notifier
- SmsOnly inherits from Notifier
- EmailAndSms inherits from Notifier
class Notifier: def alert(self, msg): pass
class EmailOnly(Notifier): def alert(self, msg): print("email: " + msg)
class SmsOnly(Notifier): def alert(self, msg): print("sms: " + msg)
class EmailAndSms(Notifier): def alert(self, msg): print("email: " + msg) print("sms: " + msg)
n = EmailAndSms()n.alert("paid")email: paid sms: paid
abstract class Notifier { abstract void alert(String msg);}
class EmailOnly extends Notifier { void alert(String msg) { System.out.println("email: " + msg); }}
class SmsOnly extends Notifier { void alert(String msg) { System.out.println("sms: " + msg); }}
class EmailAndSms extends Notifier { void alert(String msg) { System.out.println("email: " + msg); System.out.println("sms: " + msg); }}
public class Main { public static void main(String[] args) { Notifier n = new EmailAndSms(); n.alert("paid"); }}email: paid sms: paid
The refactor keeps one Notifier class. It holds a list of channels, and its alert method passes the message to every channel in the list. Each channel is a small class with a send method. In Java the channels share the interface Channel; in Python any object with a send method will do.
- Notifier has 0..* Channel (aggregation)
- Email implements Channel
- Sms implements Channel
- Push implements Channel
This challenge has five steps, and each step is scored on its own. You will count the subclasses the old design needs, predict what the new one prints, complete the line that passes the message on, count again for a fourth channel, and say what each design does when a customer changes their settings. Every step shows the code in Python and in Java; the answers are the same in both. You can try as many times as you like, and hints work as they do in the lessons.
A third channel, Push, arrives. The shop wants every mix of the three channels: any one, any two, or all three. With one subclass per mix, how many subclasses of Notifier are needed?
The refactored notifier. One alert is sent with one channel, then two channels are added and a second alert is sent. What does this print?
class Email: def send(self, msg): print("email: " + msg)
class Sms: def send(self, msg): print("sms: " + msg)
class Push: def send(self, msg): print("push: " + msg)
class Notifier: def __init__(self): self.channels = []
def add(self, channel): self.channels.append(channel)
def alert(self, msg): for channel in self.channels: channel.send(msg) return len(self.channels)
n = Notifier()n.add(Email())sent = n.alert("paid")n.add(Sms())n.add(Push())sent = sent + n.alert("shipped")print(sent)import java.util.ArrayList;import java.util.List;
interface Channel { void send(String msg);}
class Email implements Channel { public void send(String msg) { System.out.println("email: " + msg); }}
class Sms implements Channel { public void send(String msg) { System.out.println("sms: " + msg); }}
class Push implements Channel { public void send(String msg) { System.out.println("push: " + msg); }}
class Notifier { private List<Channel> channels = new ArrayList<>();
void add(Channel channel) { channels.add(channel); }
int alert(String msg) { for (Channel channel : channels) { channel.send(msg); } return channels.size(); }}
public class Main { public static void main(String[] args) { Notifier n = new Notifier(); n.add(new Email()); int sent = n.alert("paid"); n.add(new Sms()); n.add(new Push()); sent = sent + n.alert("shipped"); System.out.println(sent); }}Complete the one line inside the loop of alert, so that each channel in the list sends the message. The program should print the five lines from the last step.
class Email: def send(self, msg): print("email: " + msg)
class Sms: def send(self, msg): print("sms: " + msg)
class Push: def send(self, msg): print("push: " + msg)
class Notifier: def __init__(self): self.channels = []
def add(self, channel): self.channels.append(channel)
def alert(self, msg): for channel in self.channels: ________ # ← pass the message to this channel return len(self.channels)
n = Notifier()n.add(Email())sent = n.alert("paid")n.add(Sms())n.add(Push())sent = sent + n.alert("shipped")print(sent)import java.util.ArrayList;import java.util.List;
interface Channel { void send(String msg);}
class Email implements Channel { public void send(String msg) { System.out.println("email: " + msg); }}
class Sms implements Channel { public void send(String msg) { System.out.println("sms: " + msg); }}
class Push implements Channel { public void send(String msg) { System.out.println("push: " + msg); }}
class Notifier { private List<Channel> channels = new ArrayList<>();
void add(Channel channel) { channels.add(channel); }
int alert(String msg) { for (Channel channel : channels) { ________ // ← pass the message to this channel } return channels.size(); }}
public class Main { public static void main(String[] args) { Notifier n = new Notifier(); n.add(new Email()); int sent = n.alert("paid"); n.add(new Sms()); n.add(new Push()); sent = sent + n.alert("shipped"); System.out.println(sent); }}A fourth channel, WhatsApp, arrives, and the shop still wants every mix. How many new classes does each design need?
A customer with email alerts turns on SMS in the app's settings, while the program runs. What does each design have to do?
- Count the subclasses a family of mixes needs, predict and complete a notifier that holds channels, and say what each design does when a part must change.