目前,我正在研究设计模式,我遇到了一个让我感到困惑的部分,那就是观察者模式是使用了push机制,还是使用了pull机制?
我读过不同的实现,但不能确定哪一个是正确的。
另外,我想知道push模型相对于pull模型的三个直接优势。我猜其中之一是推式模型比拉式模型耦合得更少?
发布于 2017-08-13 11:34:00
观察者模式的细节(重点放在提出的问题上)
观察者:观察者模式定义了对象之间的一对多依赖关系,因此当一个对象更改状态时,它的所有依赖项都会被通知并将automatically
1. _Observable object_ - The object being observed.
2. _Observer objects_ - The objects that observe the observable object
3. _Communication Mechanism_ - Pull or Push Mechanism
目前,我正在研究设计模式,我遇到了一个令人困惑的部分,那就是观察者模式是使用了push机制,还是使用了pull机制?
混淆可能是因为你是一个文学上的名字-拉或推。
请注意,在这两种机制中,通知所有订阅的观察者始终是可观察对象的责任,但不同之处在于Push->观察者获得它想要的确切数据还是Pull->它获得包装在某个对象(主要是可观察对象)中的数据&它必须从中提取所需的数据。
我读过不同的实现,不能确定哪一个是正确的。
这不是修正的问题,实际上两者在任何情况下都会工作得很好。如果我们看到下面这两种机制的细节,就可以很容易地分析出哪种机制最适合特定的场景/情况。
另外,我想知道push模型相对于pull模型的三个直接优点。
。我猜其中之一是推式模型比拉式模型耦合得更少?
我可能不能提供3个优点,但让我尝试一下,如果我可以通过使用用例示例来清楚地告诉你应该在哪里使用什么:
- This is purely the Observable's responsibility, Observer just need to make sure they have put required code in their update methods.
- Advantages
- The main advantage of the 'push' model is lower coupling between the observer and the subject.
- Observable & Observer both are interfaces/abstract classes which is actually a design principle - **Program to interface or supertypes**
- Disadvantage
- Less flexibility : As Observable needs to send the required data to the Observer and it would become messy if we have say 1000 observers and most of them require different types of data.
- Use-Case
- It should be used when there are max 2-3 different types of Observers (different types means observer require different data) or all observers require same type of data.
- Like token systems in a bank
- In this all observers (different LEDs) just need one notification the list of updated waiting token numbers, so may better be implemented in this way as compared to Pull Mechanism.
- In pull mechanism as well, it is the Observable's responsibility to notify all observer that something has been changed, but this time Observable shares the whole object which has the changes, some observers might not require the complete object, so Observers just need extract the required details from that complete project in their update methods.
- Advantages
- The advantage of this is more flexibility.
- Each observer can decide for itself what to query, without relying on the subject to send the correct (only required) information.
- Disadvantage
- The observers would have to know things about the subject in order to query the right information from the shared complete object.
- Use-Case
- It should be used when there are more than 2-3 different types of Observers (different types means observer require different data)
- Like publishing of FX Rates by any foreign exchange rates provider for different investment banks
- In this Some banks just deals with only INR, some others only GBP etc. so it should be implemented using Pull mechanism as compared to Push mechanism.
参考
的第一本书
发布于 2020-03-01 03:44:16
这是一个使用"PULL“模式的示例代码,如上所述,观察者可以获得不同类型的数据(在这种情况下没有实现)。
import java.io.*;
import java.util.*;
interface Observer{
public void update();
}
interface Observable {
public void notifyAll() throws Exception;
public void notify(Observer o) throws Exception;
}
class Suscriber implements Observer {
String id;
Subject subject;
boolean registered = false;
Double data = 0.0;
public Suscriber(String id,Subject sub){
this.id = id;
subject = sub;
subject.register(this);
registered = true;
}
public void update() {
if(registered){
data = subject.getData();
}
display();
}
void display(){
System.out.println("Suscriber:" + id + " updated");
System.out.println("Current DATA: " + data);
}
}
class Subject implements Observable{
private List<Observer> observers = new ArrayList<Observer>();
private Double data = 0.0;
public void register(Observer o){
observers.add(o);
}
public void unregister(Observer o){
int i = observers.indexOf(o);
observers.remove(i);
}
public void notify(Observer o) throws Exception{
o.update();
}
public void notifyAll() throws Exception {
for(Observer o:observers)
this.notify(o);
}
public void computeMetrics(){
try{
long bunch = System.currentTimeMillis()/2;
data = data + new Double(bunch);
this.notifyAll();
}catch(Exception ex){
ex.printStackTrace();
}
}
public Double getData() {
return this.data;
}
}
class ObserverTestDrive {
public static void main (String[] args) {
Subject subject = new Subject();
long transmission = 10000;
Suscriber norths = new Suscriber("NorthStation",subject);
Suscriber wests = new Suscriber("WestStation",subject);
Suscriber souths = new Suscriber("SouthStation",subject);
for(int i=0;i<transmission;i++)
subject.computeMetrics();
}
}发布于 2016-01-10 21:59:54
观察者模式使用推送,因为可观察对象向其订阅者推送通知。
Push vs Pull (主要在web上):Push -服务器向客户端发送(Push)通知,这意味着它需要跟踪他们的地址(URI),或者在更一般的情况下,他们的引用。
拉取-客户端负责从服务器请求新数据。
该模式不仅适用于Web,而且被广泛使用,例如在桌面应用程序中。
https://stackoverflow.com/questions/34706186
复制相似问题