Questions tagged [observer-pattern]

87 questions
17
votes
4 answers

Which design pattern is more suitable for logging?

I should log some events in a program but as far as I know it would be better to keep the logging code outside the program because it is not about the real functionality of the program. So may you tell me if I should keep it completely out of code…
user1892555
  • 311
  • 1
  • 2
  • 5
16
votes
3 answers

Is using Observer pattern a good idea while building a Chess Game?

I am trying to design a Chess Game using OOPs concepts that has a UI element to it. My idea is to show the number of squares / cells a piece can travel to when selected. Basically I want to show the paths / directions in which the it can travel /…
16
votes
1 answer

Difference between Consumer/Producer and Observer/Observable

I am working on the design of an application that consists of three parts: a single thread that watches for certain events happening (file creation, external requests etc.) N worker threads that respond to these events by processing them (each…
12
votes
1 answer

The Observer pattern using the pulling mechanism

I was wondering about the following implementation of public void update(Observable obs, Object arg) while I want to send to all of my observer and update using notifyObserver() I and passing a references to this the observer can use the getters…
USer22999299
  • 237
  • 1
  • 2
  • 10
10
votes
3 answers

What's the difference between Observer pattern and listeners?

I have used some kind of "listeners" where I have an interface implemented by classes that need to be notified of some event (e.g.: CurrencyListener, with a method currencyUpdated(Currency currency)) Then, the object that needs to send a…
10
votes
2 answers

Is the observer pattern suitable when the observers are not independent of each other?

I have a class Car which has 2 properties: int price and boolean inStock. It also holds a List of abstract class State (empty class). There are 2 states which can be applied on the car and each is represented by its own class: class Upgrade extends…
user1803551
  • 262
  • 3
  • 12
10
votes
5 answers

What are the advantages of the delegate pattern over the observer pattern?

In the delegate pattern, only one object can directly listen to another object's events. In the observer pattern, any number of objects can listen to a particular object's events. When designing a class that needs to notify other object(s) of…
JoJo
  • 1,475
  • 2
  • 14
  • 21
10
votes
3 answers

Observer pattern; knowing *what* changed?

I've created two abstract classes Subject and Observer that define a classic Observer pattern interface. I derive from them to implement the Observer pattern. An observer might look like this: void MyClass::Update(Subject *subject) { …
User
  • 1,541
  • 2
  • 16
  • 30
9
votes
4 answers

Was .NET's IObserver intended for subscribing to multiple IObservables?

There are IObservable and IObserver interfaces in .NET (also here and here). Interestingly, the concrete implementation of the IObserver does not hold a direct reference to the IObservable. It doesn't know who it's subscribed to. It can only…
Nick Alexeev
  • 2,484
  • 2
  • 17
  • 24
9
votes
2 answers

Observer pattern over HTTP and TCP/IP (Server-Client)

I have a server and many clients (around 50 clients) who connect to that server based on a web application, which is of course based on HTTP protocol, which in turn uses TCP/IP (correct me if I'm wrong, cause I'm not really good at networking). The…
Saeed Neamati
  • 18,142
  • 23
  • 87
  • 125
7
votes
4 answers

Is it true that for Observer Pattern to work, there probably is some polling mechanism underneath?

The short question is: is it true that for Observer Pattern to work, there probably is some polling mechanism underneath? (update: in a network situation) It was quite amazing that if on my Macbook Pro, I save a file Hello World.txt into the Dropbox…
nonopolarity
  • 1,827
  • 1
  • 16
  • 30
7
votes
2 answers

Designing web-based plugin systems correctly so they don't waste as many resources?

Many CMS systems which rely on third parties for much of their code often build "plugin" or "hooks" systems to make it easy for developers to modify the codebase's actions without editing the core files. This usually means an Observer or Event…
Xeoncross
  • 1,213
  • 1
  • 11
  • 24
6
votes
3 answers

Connotation difference between "subscribers" and "observers"

In the observer pattern, and similar, is there a meaningful difference between "subscribers" and "observers" in the wild, or in the lit? With RxJS and (functional) reactive programming, are the terms interchangeable?
Alexander Mills
  • 277
  • 2
  • 13
6
votes
2 answers

API Design - Observable vs Callback

We as a team are writing an C# SDK which communicates with a Server endpoint. All our API's till now have been Task based. Like Task DoOperationAsync() Recently we across a need for API which gets an intermediate update (file location) before the…
Shenron
  • 71
  • 1
  • 5
6
votes
3 answers

When building an observer pattern (event pattern) is it better to use classes or an enum for events?

When building an Observer pattern, you need to define your events that the are broadcast and observed. Is it better to define an abstract Event class with multiple subclasses for each event, or to define a single enum, where each event is listed in…
tir38
  • 203
  • 2
  • 6
1
2 3 4 5 6