被观察者
/**
* @Author: chuan.bai
* @Description 邮件发送人
* @Date: Created on 15:03 2021/4/1
* @Modified By:
*/
public interface EmailSender {
/**
* 新增需要发送邮件的人
* @param emailReceiver
*/
void addUser(EmailReceiver emailReceiver);
/**
* 删除需要发送邮件的人
* @param emailReceiver
*/
void removeUser(EmailReceiver emailReceiver);
/**
* 通知收件人接收邮件
* @param msg
*/
void sendMsgToUser(String msg);
}
观察者抽象
/**
* @Author: chuan.bai
* @Description 邮件收件人
* @Date: Created on 15:03 2021/4/1
* @Modified By:
*/
public interface EmailReceiver {
/**
* 接收邮件
* @param msg
*/
void receiveEmailMsg(String name,String msg);
}
观察者的实现类
/**
* @Author: chuan.bai
* @Description 收件人
* @Date: Created on 15:20 2021/4/1
* @Modified By:
*/
public class ReceiverUser implements EmailReceiver{
/**
* 收件人姓名
*/
private String name;
public ReceiverUser(String name) {
this.name = name;
}
@Override
public void receiveEmailMsg(String name, String msg) {
System.out.println("用户" + this.name + "收到"+name+"的邮件内容为:" + msg);
}
}
被观察者的实现
/**
* @Author: chuan.bai
* @Description
* @Date: Created on 15:15 2021/4/1
* @Modified By:
*/
public class SenderUser implements EmailSender{
/**
* 声明收件人列表
*/
private List<EmailReceiver> receiverList;
/**
* 发件人姓名
*/
private String name;
public SenderUser(String name) {
this.receiverList = new ArrayList<>();
this.name = name;
}
@Override
public void addUser(EmailReceiver emailReceiver) {
receiverList.add(emailReceiver);
}
@Override
public void removeUser(EmailReceiver emailReceiver) {
receiverList.remove(emailReceiver);
}
@Override
public void sendMsgToUser(String msg) {
//遍历收件人列表 ,向他们群发邮件
for (EmailReceiver emailReceiver : receiverList) {
emailReceiver.receiveEmailMsg(this.name,msg);
}
}
}
客户端
/**
* @Author: chuan.bai
* @Description 邮件客户端
* @Date: Created on 15:26 2021/4/1
* @Modified By:
*/
public class EmailClient {
public static void main(String[] args) {
SenderUser senderUser = new SenderUser("系统用户");
ReceiverUser receiverUser1 = new ReceiverUser("jack");
senderUser.addUser(receiverUser1);
senderUser.sendMsgToUser("发的第一邮件");
ReceiverUser receiverUser2 = new ReceiverUser("lucy");
//加入收件人2 也作为收件人列表
senderUser.addUser(receiverUser2);
senderUser.sendMsgToUser("发的第二封邮件");
}
}
结果
用户jack收到系统邮箱的邮件内容为:发的第一邮件
用户jack收到系统邮箱的邮件内容为:发的第二封邮件
用户lucy收到系统邮箱的邮件内容为:发的第二封邮件
同时是观察者和被观察者
/**
* @Author: chuan.bai
* @Description 邮件收发人 (既能收邮件又能发邮件)
* @Date: Created on 15:36 2021/4/1
* @Modified By:
*/
public class ReceiverAndSender implements EmailSender,EmailReceiver{
private List<EmailReceiver> receiverUserList;
private String name;
public ReceiverAndSender(String name) {
this.receiverUserList = new ArrayList<>();
this.name = name;
}
@Override
public void receiveEmailMsg(String name, String msg) {
System.out.println("用户" + this.name + "收到"+name+"的邮件内容为:" + msg);
}
@Override
public void addUser(EmailReceiver emailReceiver) {
receiverUserList.add(emailReceiver);
System.out.println("收件人增加");
}
@Override
public void removeUser(EmailReceiver emailReceiver) {
receiverUserList.remove(emailReceiver);
System.out.println("收件人剔除");
}
@Override
public void sendMsgToUser(String msg) {
//遍历收件人列表 ,向他们群发邮件
for (EmailReceiver emailReceiver : receiverUserList) {
emailReceiver.receiveEmailMsg(this.name,msg);
}
}
}
客户端
/**
* @Author: chuan.bai
* @Description
* @Date: Created on 15:50 2021/4/1
* @Modified By:
*/
public class EmailClient2 {
public static void main(String[] args) {
ReceiverAndSender jack = new ReceiverAndSender("jack");
ReceiverAndSender bill = new ReceiverAndSender("bill");
ReceiverAndSender lucy = new ReceiverAndSender("lucy");
jack.addUser(bill);
jack.addUser(lucy);
jack.sendMsgToUser("aaa");
bill.addUser(jack);
bill.addUser(lucy);
bill.sendMsgToUser("cc");
lucy.addUser(bill);
lucy.addUser(jack);
lucy.sendMsgToUser("dd");
}
}
执行结果
收件人增加
收件人增加
用户bill收到jack的邮件内容为:aaa
用户lucy收到jack的邮件内容为:aaa
收件人增加
收件人增加
用户jack收到bill的邮件内容为:cc
用户lucy收到bill的邮件内容为:cc
收件人增加
收件人增加
用户bill收到lucy的邮件内容为:dd
用户jack收到lucy的邮件内容为:dd