Files
Java/com/study/demo13/NotificationService.java
2026-01-21 14:35:47 +08:00

67 lines
1.7 KiB
Java
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.study.demo13;
interface Notifier {
void send(String message, int level);
String getChannel();
}
class EmailNotifier implements Notifier {
private String channel = "EMAIL";
@Override
public void send(String message, int level) {
System.out.println("[EMAIL] " + (level >= 3 ? "[紧急] " : "") + message);
}
@Override
public String getChannel() {
return channel;
}
}
class SMSNotifier implements Notifier {
private String channel = "SMS";
@Override
public void send(String message, int level) {
System.out.println("[SMS] " + (level >= 3 ? "[紧急] " : "") + message);
}
@Override
public String getChannel() {
return channel;
}
}
class PushNotifier implements Notifier {
private String channel = "PUSH";
@Override
public void send(String message, int level) {
System.out.println("[PUSH] " + (level >= 3 ? "[紧急] " : "") + message);
}
@Override
public String getChannel() {
return channel;
}
}
public class NotificationService {
public static void main(String[] args) {
Notifier email = new EmailNotifier();
Notifier sms = new SMSNotifier();
Notifier push = new PushNotifier();
email.send("您的订单已发货", 1);
sms.send("验证码: 123456", 2);
push.send("账户安全警告", 3);
email.send("重要通知:系统维护", 4);
System.out.println("\n通知渠道类型");
System.out.println("Email渠道: " + email.getChannel());
System.out.println("SMS渠道: " + sms.getChannel());
System.out.println("Push渠道: " + push.getChannel());
}
}