Questions tagged [observer-pattern]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
15
votes
1answer
10k views

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 ...
10
votes
4answers
24k views

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 ...
10
votes
3answers
7k views

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 ...
9
votes
5answers
21k views

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 ...
9
votes
4answers
5k views

Was .NET's IObserver<T> 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'...
9
votes
2answers
3k views

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 ...
9
votes
2answers
2k views

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 ...
8
votes
1answer
9k views

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 ...
7
votes
2answers
2k views

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 ...
6
votes
3answers
2k views

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 ...
6
votes
2answers
2k views

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 ...
6
votes
4answers
697 views

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 ...
5
votes
3answers
464 views

Observer Pattern and Circular References

I was checking this example of Observer Pattern. https://www.tutorialspoint.com/design_pattern/observer_pattern.htm It seems that there is a circular reference when Observer Pattern is used. ...
5
votes
1answer
4k views

Design patterns for multi-threaded messaging server

I'm designing an instant messaging server as a personal exercise to improve my understanding and application of multi-threading and design patterns in Java. I'm still designing, there's no code yet. ...
5
votes
4answers
2k views

Design Patterns for Coordinating Change Event Listeners

I've been working with the Observer pattern in JavaScript using various popular libraries for a number of years (YUI & jQuery). It's often that I need to observe a set of property value changes (e....
4
votes
4answers
704 views

Are Compiler Directives an Antipattern?

I'm working on a legacy system that has a helper class that is symbolically linked into many different .Net projects within a solution. The logic is riddled with compiler directives that change it's ...
4
votes
1answer
975 views

I'm always reimplementing observer/subject code in Java. Is there a better option?

I'm always writing observer/subject interfaces in a particular Java project, e.g.: /** * Registers the receiver to the dispatcher. * When data arrives that the receiver can process, * it will be ...
4
votes
2answers
3k views

Publish/subscribe, observer pattern implementation question

I'm developing desktop application in java/swing. I have a component that is registered as a subscriber to many panels and gets messages (my custom event) from them. That component is something like ...
4
votes
1answer
353 views

Would this be an effective use of the Subject/Observer pattern, or is there a better way?

TBH: I've never used an observer pattern. But it looks pretty useful for this situation: I'm developing a very simple strategy game. It involves two combatants who simultaneously bludgeon each ...
4
votes
1answer
2k views

In MVC, who is in charge of handling observers?

(I'm writing a large MATLAB GUI that makes significant use of MVC, so my example is written in MATLAB. However, I believe this question is language-agnostic. Please correct me if I'm wrong.) I have a ...
4
votes
2answers
2k views

Is there a design pattern for chained observers?

Several times, I've found myself in a situation where I want to add functionality to an existing Observer-Observable relationship. For example, let's say I have an Observable class called PriceFeed,...
4
votes
1answer
606 views

Mutual observer pattern in Java

I want to improve my multi-threading and design pattern skills. As such I'm designing an Instant Messaging server. I'm writing the Server first. My plan so far is to have Client "Proxy" Classes to ...
3
votes
5answers
676 views

Using Observer Pattern to selectively act on events

I'm currently in the process of architecting a small RPG-style dungeon crawl (in Unity), and am a little stuck on how to update various objects when things change, while not updating unrelated objects....
3
votes
4answers
211 views

Using ObserverPattern, but need to stop some observers displaying changes immediately

In a GUI application (to organize images), I use the Observer Pattern (or anything similar to model-view-(view)controller) to inform other components about changes. Assuming three components A, B, C ...
3
votes
4answers
204 views

Two user inputs that alter each other

I have a user interface with two numeric input boxes, send amount and receive amount. The values are in different currencies and are related by an exchange rate. That is, receive amount should always ...
3
votes
3answers
2k views

How does Observer create loosely-coupled design?

I completely understand the Observer design pattern, what it is for and how to implement it. I also understand what the term 'loosely-coupled' means. But I'm not sure how Observer makes designs more ...
3
votes
2answers
148 views

Preventive measures for stopping developers from forgetting to update cached values

I'm at the point where results need to be cached to make the application more responsive. From experience in a previous project, countless (to say the least) bugs occurred because there was lots of ...
3
votes
1answer
493 views

Is Content Observer an implementation of Observer Pattern?

Observer Pattern is defined by the 'Gang of Four' Design Patterns book as a "one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated ...
3
votes
1answer
179 views

State propagation(bottom-up) in multi-walk tree

Using the Tree representation(mentioned below), typedef enum {Running, Warning, Critical}Status; struct TreeNode; typedef struct List{ int childCount; struct treeNode **childList; }List; ...
3
votes
2answers
942 views

UML class diagram: how to annotate observer-subject relationship?

How can I annotate in a UML class diagram that an instance of ClassA is registered as an observer of an instance of ClassB? (Both implement the appropriate interfaces). This doesn't really belong in ...
3
votes
1answer
963 views

Observer pattern for rest services with UI client

I am developing restful services using resteasy for an application. This application has UI which will consume these rest services. Now I am having a case where I have to do a server push to UI(...
3
votes
1answer
3k views

Observer for two independent events in the observable class

Observer pattern aim: the observer class responds to the changes of the observable class state. Problem. There are different processes inside observable class that should be observed. Those events ...
3
votes
1answer
211 views

Testing: Should I wrap system notifications and send my own?

In the current Cocoa app I'm working on, I've got an object, RecordScheduler, which responds to two types of notifications, "day did pass" and "quicksaving interval did pass". In both cases, the ...
3
votes
3answers
356 views

Optimal communication pattern to update subscribers

What is the optimal way to update the subscriber's local model on changes C on a central model M? ( M + C -> M_c) The update can be done by the following methods: Publish the updated model M_c to ...
3
votes
0answers
525 views

Observing multiple observables at once / Network of chained observers

Quick example: HM observes CM SM observes CM HSM observes HM and SM, but wants to be updated only after both have their new value derived from CM. More detail: (this is still a made-up example, but ...
2
votes
2answers
2k views

Any OOP design pattern that is somewhat representative of all of the SOLID OOP design principles at play?

I'm trying to teach an object oriented design principles course (on SOLID) at a training institute. I also want to teach the students a few OOP design patterns such as factory, singleton and one other....
2
votes
2answers
336 views

Is observer pattern abandoned by the industry now (2019)? [closed]

While working on a refactoring task of a legacy app, I came across a scenario where a series of cause->effect type events are happening and currently everything is being handled inline. To give you ...
2
votes
3answers
133 views

Is the observable pattern the right tool for this scenario?

Suppose I have a Player that wants to build a Tower in the Board of a tabletop game. My first approach to this would have been something like: function onConfirm() { player.consumeResources(); ...
2
votes
2answers
985 views

observer class as instance

Sometimes I read in observer-pattern descriptions, to make the constructor of a observer base class protected so the class will be abstract. but by making the constructor public (if even one is ...
2
votes
1answer
618 views

Does key-value observing between model and view violate MVC?

In MVC the Controller is the go-between for the View and the Model. So the Controller should be the one that observes or receives changes from the Model and View and updates the other accordingly. I ...
2
votes
3answers
3k views

Automatically observing changes in database tables

currently I'm working on a project with the following setting: There is a proprietary application which performs transactions on a Microsoft Access database (which is actually stored as an .mdb file) ...
2
votes
1answer
555 views

Connect observers to observable automatically using dependency injection container

I have several services that implement an observer interface and one service that implements the corresponding observable interface. I'm currently using unity together with Prism so I would prefer to ...
2
votes
1answer
2k views

What are the advantages of Observables over an iterable of futures?

I recently came across the ReactiveX pattern for asynchronous data-flows. I studied the information provided there and also watched this talk by a Netflix engineer on how they used Observables to ...
2
votes
2answers
1k views

What preconditions should be checked while adding/removing a listener?

Imagine the following interface: interface Service { addListener(Listener l) removeListener(Listener l) } Should I check for null values while add/remove? Is it a good idea on remove to ...
2
votes
3answers
565 views

Is the Observer pattern adequate for this kind of scenario?

I'm creating a simple game development framework with Ruby. There is a node system. A node is a game entity, and it has position. It can have children nodes (and one parent node). Children are always ...
2
votes
1answer
743 views

Implementation of observer pattern with one observer/multiple publishers and multiple events?

I'm in a bit of a tricky situation where I need to use the Observer pattern but I don't really know the best way to go about it. Here's a quick briefing on my application: I'm implementing a GUI ...
2
votes
1answer
561 views

Observer Design Pattern with complicated objects

I am trying to currently get a good grasp on the Observer design pattern, but I am stuck wondering about one specific issue. In the examples I have been able to find of implementation, they are all ...
2
votes
2answers
725 views

Is there a way to not use the Observer pattern when creating GUI systems?

In Java, the Swing library implements the Observer/Subscriber-Publisher design pattern to design the way the program responds to events on the GUI (button clicks, etc). The programmer registers ...
1
vote
2answers
913 views

Observer pattern without state change?

My question is whether there is a similar pattern to Observer that does not imply a state change in the observed object. The following is a concrete example: I have a ConnectionFactory class which is ...
1
vote
3answers
1k views

Is this Observer variant an improvement?

In a C++ code base I've been working on, they have a bunch of instances of the observer pattern, but it's a little different from the classical pattern. In the classic, the Observer is a specific ...