In observer pattern normally there is a one-to-many relationship between the subject and observers (there are one subject and many observers).
But I have a problem where in my implementation, there are many subjects (eg: A, B, C) that are returning objects but only one observer (eg: O) is there to take outputs of those subjects into a queue and process them one after another.
What sort of design pattern / paradigm can I use for this?
答案 0 :(得分:1)
如果您需要使用队列,可以使用Producer / Consumer模式。
观察者模式的不同之处在于观察者通常知道可观察者。在生产者/消费者模式中,观察对象(对象,信息,事件的生产者)和观察者(消费者)完全分离。 无论有多少(甚至没有)生产者,消费者都不会受到影响。
另一个区别当然是,消费者必须主动轮询队列。
"挑战"使用此模式是在两端访问队列时的正确同步。但当然有一些库可以解决这个问题。