Questions tagged [observer-pattern]

A design pattern in which an object, called the subject, maintains a list of its dependents, called observers and notifies them automatically of any state changes, usually by calling one of their methods. It is one of the Gang of Four's behavioral design patterns. When using this tag on implementation heavy questions - tag the code language the implementation is written in.

The Observer pattern (a subset of the publish/subscribe pattern) is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems.

This is one of the Gang of Four's behavioral , first published in Gamma et al.'s book "Design Patterns: Elements of Reusable Object-Oriented Software".

References:

1815 questions
0
votes
1 answer

Normal observer pattern vs template observer pattern

I want to know if the observer pattern configered with a template observer is more convenient than the general observer pattern. The concept of a template observer is a observable class which has a specific observer type configered as a template. As…
Marc
  • 379
  • 4
  • 8
0
votes
2 answers

Encapsulation & Message Objects

When developing an internal message system such as through the Mediator or Observer pattern, what's the best way to encapsulate the message objects that are passed around? Consider the following Message Object which tells some service to run a new…
0
votes
2 answers

Relationship in Observer Pattern

Does the observer pattern model a one-to-many relationship or a many-to-many? I've found many resources where the relationship modeled by the observer pattern is one-to-many. This makes perfectly sense, but isn't it possible to also model a…
Bastian
  • 1,553
  • 13
  • 33
0
votes
2 answers

Python dependencies: class method to access another instance's variable

How can I update an instance variable based on another instance? Is there a way to get a many-to-many dependency between objects in Python? Deptest is a class which can be either ONLINE or OFFLINE, and it has a dictionary instance variable called…
Metalgearmaycry
  • 271
  • 1
  • 2
  • 7
0
votes
1 answer

Event handling and Observer pattern (GoF)

These days, I am learning Gof design patterns in C# and I think event-handling might have something to do with Observer pattern. My question is while solving problems with events and their handlers, why we still need observer pattern?
0
votes
4 answers

Using threads with Observer Pattern

I have this code: public class classExample implements Observer Runnable { public classExample() { //... } @Override public void run() { while (true) { //wait for EVENT; //do something; } …
Maffe
  • 430
  • 6
  • 14
0
votes
1 answer

ReactiveX in REST API without UI

I'm starting to study ReactiveX and couldn't understand the advantages in a REST API, or any api that doesn't have a UI associated. For instance, I'm building a Spring Boot REST API that is consumed by a Angular webapp (and eventually other apps)…
Ivo Udelsmann
  • 142
  • 1
  • 13
0
votes
1 answer

Implementing observer pattern without unsubscribe method

When I implement observer pattern before, I always used to hold a reference to the owner inside of listener. And in listener's ctor I used register and in dtor I used to unregister. But this time around I don't want to hold a reference for keeping…
Kadir Erdem Demir
  • 3,531
  • 3
  • 28
  • 39
0
votes
1 answer

JS observer pattern

I try the observer pattern (by these two Urls: https://davidwalsh.name/pubsub-javascript, http://www.dofactory.com/javascript/observer-design-pattern) but listeners array is empty when I call the publish function. main.pagination.event= (function…
Sándor Hatvani
  • 435
  • 1
  • 7
  • 21
0
votes
1 answer

Mithriljs inter component communication - blocking

I have designed a app using mithriljs(0.2.5) with components and Observer pattern for inter-component communication. However I do have a requirement of blocking an action of component based on another one. Say, I have 2 components ItemList &…
chifer
  • 683
  • 5
  • 19
0
votes
1 answer

CoreData addObserver NSKeyValueChangeInsertion

Is there a way to get the object that has been added using addObserver:forKeyPath:options:context: with option NSKeyValueChangeInsertion on an NSManagedObject?
sharvey
  • 7,635
  • 7
  • 48
  • 66
0
votes
1 answer

Swift - How to manage turn order management?

I'm working on a local game which relies on turn order. Rules; There are a number of phases in the game (ie: Buy, Sell) During each phase, a player takes a single turn Each phase is not considered complete until every player (in turn order) has…
zardon
  • 1,601
  • 4
  • 23
  • 46
0
votes
1 answer

Observing Asynchronous Requests

I have three separate calls to APIs. When all three calls are complete, I am going to aggregate the data and use it to form a separate model object. I figured I would use property observers to accomplish this but I have no idea how it would be…
Tyler Travis
  • 117
  • 1
  • 5
0
votes
1 answer

How can I update an assignment in Swift?

I want to know how I can do the following: var x = Observed("hello") label.text = x Somewhere else x is updated: x.value = "World" Then label.text is automatically updated. Here is what I have for Observed so far class Observed { …
mergesort
  • 5,087
  • 13
  • 38
  • 63
0
votes
1 answer

Trigger an event for all listeners except the trigger itself

I have a couple of links bound to a custom event. The event is triggered by hovering any of those links. However I don't want it to be triggered on the link that I actually hovered. I can think of a couple of possible solutions I put an "if" in the…
Flanders
  • 23
  • 4