mirror of
https://github.com/Dichgrem/Java.git
synced 2026-02-05 02:51:57 -05:00
update:demo13
This commit is contained in:
66
com/study/demo13/NotificationService.java
Normal file
66
com/study/demo13/NotificationService.java
Normal file
@@ -0,0 +1,66 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user