当前位置:   article > 正文

Java 观察者模式(Observer Pattern)详解_观察者模式java

观察者模式java

说明:

观察者模式(Observer Pattern)是一种行为型设计模式,它定义了对象之间的一对多依赖关系,使得当一个对象的状态发生变化时,所有依赖它的对象都会得到通知并自动更新。

在观察者模式中,有两个主要角色:

Subject(主题):它是被观察的对象,也被称为主题或可观察者。它维护了一个观察者列表,并提供了添加、删除和通知观察者的方法。当主题的状态发生变化时,会通知所有注册的观察者。 Observer(观察者):它是接收主题通知的对象。观察者定义了一个更新方法,当接收到主题通知时,会调用该方法进行相应的更新操作。多个观察者可以同时观察同一个主题。

下面是观察者模式的主要特点和应用场景:

特点:

松耦合:主题和观察者之间通过接口进行通信,彼此之间不直接依赖,从而实现松耦合。 动态添加和移除观察者:可以在运行时动态添加和移除观察者,灵活地管理观察者对象。 支持一对多的依赖关系:一个主题可以有多个观察者,当主题状态变化时,所有观察者都会收到通知。

应用场景:

当一个对象的状态变化需要通知其他多个对象,并且这些对象之间的依赖关系动态变化时,可以使用观察者模式。

当系统中存在多个对象之间的一对多依赖关系,并且希望避免耦合性,降低对象之间的直接依赖关系时,可以使用观察者模式。

当一个抽象模型有两个方面,其中一个方面依赖于另一个方面,且需要独立地改变它们的对象时,可以使用观察者模式。

翻译:

The Observer Pattern is a behavioral design pattern that defines a one-to-many dependency between objects. When the state of one object changes, all its dependents are notified and updated automatically.

In the Observer Pattern, there are two main roles:

Subject: It is the object being observed, also known as the subject or the observable. It maintains a list of observers and provides methods to add, remove, and notify observers. When the state of the subject changes, it notifies all registered observers.

Observer: It is the object that receives notifications from the subject. The observer defines an update method that is called when it receives a notification from the subject. Multiple observers can observe the same subject.

Here are the main characteristics and application scenarios of the Observer Pattern:

Characteristics:

Loose coupling: The subject and observers communicate through interfaces, avoiding direct dependencies and achieving loose coupling.
Dynamic addition and removal of observers: Observers can be dynamically added or removed at runtime, providing flexibility in managing observer objects.
Support for one-to-many dependency: A subject can have multiple observers, and when the subject's state changes, all observers are notified.
Application scenarios:

When an object's state changes and needs to notify multiple other objects, and the dependencies between these objects are dynamic, the Observer Pattern can be used.
When there are one-to-many dependencies between objects in a system, and you want to avoid tight coupling and reduce direct dependencies between objects, the Observer Pattern can be used.
When an abstract model has two aspects, where one aspect depends on the other and they need to change independently, the Observer Pattern can be used.

I hope this provides a comprehensive explanation of the Observer Pattern.

下面是一个示例代码,展示了观察者模式的实现:

  1. // 观察者接口
  2. interface Observer {
  3. void update(int state);
  4. }
  5. // 具体观察者类
  6. class ConcreteObserver implements Observer {
  7. private String name;
  8. public ConcreteObserver(String name) {
  9. this.name = name;
  10. }
  11. public void update(int state) {
  12. System.out.println("Observer " + name + " received update. New state: " + state);
  13. }
  14. }
  15. public class ObserverPatternExample {
  16. public static void main(String[] args) {
  17. // 创建具体主题对象
  18. ConcreteSubject subject = new ConcreteSubject();
  19. // 创建具体观察者对象
  20. Observer observer1 = new ConcreteObserver("Observer 1");
  21. Observer observer2 = new ConcreteObserver("Observer 2");
  22. Observer observer3 = new ConcreteObserver("Observer 3");
  23. // 注册观察者
  24. subject.registerObserver(observer1);
  25. subject.registerObserver(observer2);
  26. subject.registerObserver(observer3);
  27. // 设置主题状态,并通知观察者
  28. subject.setState(1);
  29. // 移除一个观察者
  30. subject.removeObserver(observer2);
  31. // 设置主题状态,并通知观察者
  32. subject.setState(2);
  33. }
  34. }

在上述示例中,我们创建了一个具体主题类 ConcreteSubject,它实现了 Subject 接口,提供了注册、移除和通知观察者的方法。它还维护了一个观察者列表和一个状态值。

我们还定义了一个观察者接口 Observer,它声明了一个 update 方法。具体观察者类 ConcreteObserver 实现了 Observer 接口,并在 update 方法中定义了观察者的具体行为。

main 方法中,我们创建了具体主题对象和具体观察者对象,并进行注册。然后我们设置主题的状态,并通知观察者。在通知观察者时,每个观察者都会收到更新并执行相应的操作。

注意,在具体主题类中的 setState 方法中调用了 notifyObservers 方法,它会遍历观察者列表,依次调用每个观察者的 update 方法。这样就实现了当主题的状态发生变化时,所有注册的观察者都会收到通知并执行相应的更新操作。

运行上述代码,你会看到输出结果类似于:

  1. Observer Observer 1 received update. New state: 1
  2. Observer Observer 2 received update. New state: 1
  3. Observer Observer 3 received update. New state: 1
  4. Observer Observer 1 received update. New state: 2
  5. Observer Observer 3 received update. New state: 2

这说明观察者模式成功地实现了主题与观察者之间的解耦,并实现了一对多的依赖关系。当主题的状态发生变化时,所有观察者都能够及时收到通知并进行相应的更新操作。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/677513
推荐阅读
相关标签
  

闽ICP备14008679号