Super-simple example of C# observer/observable with delegates

The observer pattern is usually implemented with events. Here’s an example: using System; class Observable { public event EventHandler SomethingHappened; public void DoSomething() => SomethingHappened?.Invoke(this, EventArgs.Empty); } class Observer { public void HandleEvent(object sender, EventArgs args) { Console.WriteLine(“Something happened to ” + sender); } } class Test { static void Main() { Observable observable = … Read more

Observer Design Pattern vs “Listeners”

Whether the term “listener” refers to the Observer pattern or not will depend upon the context. For example, Java Swing’s “Event Listeners” are part of an Observer pattern implementation while .Net “Trace Listeners” are not. It isn’t uncommon for framework authors to assign different names to components participating in a given pattern implementation, but the … Read more

Observer is deprecated in Java 9. What should we use instead of it?

Why is that? Does it mean that we shouldn’t implement observer pattern anymore? Answering the latter part first – YES, it does mean you shouldn’t implement Observer and Obervables anymore. Why were they deprecated – They didn’t provide a rich enough event model for applications. For example, they could support only the notion that something … Read more

Difference between Observer, Pub/Sub, and Data Binding

There are two major differences between Observer/Observable and Publisher/Subscriber patterns: Observer/Observable pattern is mostly implemented in a synchronous way, i.e. the observable calls the appropriate method of all its observers when some event occurs. The Publisher/Subscriber pattern is mostly implemented in an asynchronous way (using message queue). In the Observer/Observable pattern, the observers are aware … Read more

Determine what attributes were changed in Rails after_save callback?

Rails 5.1+ Use saved_change_to_published?: class SomeModel < ActiveRecord::Base after_update :send_notification_after_change def send_notification_after_change Notification.send(…) if (saved_change_to_published? && self.published == true) end end Or if you prefer, saved_change_to_attribute?(:published). Rails 3–5.1 Warning This approach works through Rails 5.1 (but is deprecated in 5.1 and has breaking changes in 5.2). You can read about the change in this pull … Read more

Delegation: EventEmitter or Observable in Angular

Update 2016-06-27: instead of using Observables, use either a BehaviorSubject, as recommended by @Abdulrahman in a comment, or a ReplaySubject, as recommended by @Jason Goemaat in a comment A Subject is both an Observable (so we can subscribe() to it) and an Observer (so we can call next() on it to emit a new value). … Read more