使用java完成 :以发行商(Pbulisher)和不同类型读者(Reader)为例实现观察者(发布-订阅)模式
时间: 2024-05-15 11:13:28 浏览: 10
观察者模式也称为发布-订阅模式,是一种行为设计模式,用于将对象之间的一对多依赖关系解耦,当一个对象的状态发生改变时,所有依赖于它的对象都会得到通知并自动更新。
在本例中,我们以发行商和不同类型读者为例来实现观察者模式。发行商发布新书时,不同类型的读者可以订阅该书。当新书发布时,所有订阅该书的读者都会收到通知。
我们需要定义两个接口:`Publisher` 和 `Reader`。`Publisher` 接口定义了添加、删除和通知读者的方法。`Reader` 接口定义了更新方法。
```java
// 定义发布者接口
public interface Publisher {
void addReader(Reader reader); // 添加读者
void removeReader(Reader reader); // 删除读者
void notifyReaders(String bookName); // 通知所有读者
}
// 定义订阅者接口
public interface Reader {
void update(String bookName); // 更新方法
}
```
然后我们实现 `Publisher` 接口并维护一个读者列表,实现添加、删除和通知所有读者的方法。
```java
import java.util.ArrayList;
import java.util.List;
// 实现发布者接口
public class BookPublisher implements Publisher {
private List<Reader> readers = new ArrayList<>(); // 订阅该书的读者列表
@Override
public void addReader(Reader reader) {
readers.add(reader);
}
@Override
public void removeReader(Reader reader) {
readers.remove(reader);
}
@Override
public void notifyReaders(String bookName) {
for (Reader reader : readers) {
reader.update(bookName);
}
}
}
```
最后,我们实现 `Reader` 接口并在 `update` 方法中更新读者的状态。
```java
// 实现订阅者接口
public class NovelReader implements Reader {
private String name;
public NovelReader(String name) {
this.name = name;
}
@Override
public void update(String bookName) {
System.out.println(name + " received a new book: " + bookName);
}
}
```
现在可以创建不同类型的读者,并让他们订阅新书。当发布者通知读者时,所有订阅该书的读者都会收到通知并更新状态。
```java
public class ObserverPatternDemo {
public static void main(String[] args) {
BookPublisher publisher = new BookPublisher(); // 创建发行商
NovelReader reader1 = new NovelReader("Alice"); // 创建小说读者
ScienceReader reader2 = new ScienceReader("Bob"); // 创建科学读者
publisher.addReader(reader1); // 小说读者订阅新书
publisher.addReader(reader2); // 科学读者订阅新书
publisher.notifyReaders("The Lord of the Rings"); // 发布新书
}
}
```
输出结果:
```
Alice received a new book: The Lord of the Rings
Bob received a new book: The Lord of the Rings
```
这样就完成了以发行商和不同类型读者为例实现观察者(发布-订阅)模式的示例。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)