observer-pattern

Determine what attributes were changed in Rails after_save callback?

早过忘川 提交于 2019-11-27 17:03:55
I'm setting up an after_save callback in my model observer to send a notification only if the model's published attribute was changed from false to true. Since methods such as changed? are only useful before the model is saved, the way I'm currently (and unsuccessfully) trying to do so is as follows: def before_save(blog) @og_published = blog.published? end def after_save(blog) if @og_published == false and blog.published? == true Notification.send(...) end end Does anyone have any suggestions as to the best way to handle this, preferably using model observer callbacks (so as not to pollute my

Observing multiple observables while avoiding instanceof operator in java?

穿精又带淫゛_ 提交于 2019-11-27 15:13:09
问题 If I have an object that I want to be able to observe several other observable objects, not all of the same type. For example I want A to be able to observe B and C. B and C are totally un-related, except for the fact that they both implement Observable. The obvious solution is just to use "if instanceof" inside the update method but that quickly can become messy and as such I am wondering if there is another way? 回答1: Similar to previous suggestions you could change you update to. public

Observable in Java

*爱你&永不变心* 提交于 2019-11-27 13:49:57
问题 I'm trying to understand the Observer and the Observable. Here's an example that I'm trying to figure out: public class IntegerDataBag extends Observable implements Iterable<Integer> { private ArrayList<Integer> list= new ArrayList<Integer>(); public void add(Integer i){ list.add(i); setChanged(); notifyObservers(); } public Iterator<Integer> iterator(){ return list.iterator(); } public Integer remove (int index){ if (index< list.size()){ Integer i = list.remove(index); setChanged();

Observer Pattern vs Mediator Pattern

柔情痞子 提交于 2019-11-27 13:17:57
问题 I did some googling and yes I know that questions about the difference between these two has been asked before on stackoverflow and all over the web. But I mostly find worded answers, which can be confusing. My question is if anyone here can please provide two visual examples of both the mediator and observer patterns for me that can clearly demonstrate the difference between the two. In Javascript. Thank you! 回答1: Yes, they are distinct. I will explain by examples from real life, based on a

Observers vs. Callbacks

别等时光非礼了梦想. 提交于 2019-11-27 10:17:47
问题 i thought about using observers or callbacks. What and when you should use an observer? F.e. you could do following: # User-model class User << AR after_create :send_greeting! def send_greeting! UserNotifier.deliver_greeting_message(self) end end #observer class UserNotifier << AR def greeting_message(user) ... end end or you could create an observer and let it watch when users becomes created... What dou you recommened? 回答1: A callback is more short lived: You pass it into a function to be

Why should the observer pattern be deprecated?

家住魔仙堡 提交于 2019-11-27 09:16:01
问题 I've noticed that my dependency injected, observer-pattern-heavy code (using Guava's EventBus) is often significantly more difficult to debug than code I've written in the past without these features. Particularly when trying to determine when and why observer code is being called. Martin Oderski and friends wrote a lengthy paper with an especially alluring title, "Deprecating the Observer Pattern" and I have not yet made the time to read it. I'd like to know what is so wrong with the

Callback/Command vs EventListener/Observer Pattern

喜欢而已 提交于 2019-11-27 09:09:52
问题 I'm trying to design an async framework and wanted to know what people think are the pros/cons of the callback pattern vs the observer pattern. Callback pattern: //example callback public interface Callback{ public void notify(MethodResult result); } //example method public class Worker{ public void doAsyncWork(Callback callback){ //do work callback.notify(result); } } //example observer pattern public interface EventListener{ public void notify(MethodResult result); } public class Worker{

Observe a File or Folder in Objective-C

浪子不回头ぞ 提交于 2019-11-27 06:34:39
What is the best way to listen to a folder or file to see if it has been saved or if a new file has been added? The FSEvents API is ideal if you just want to watch directories but it doesn't handle the monitoring of individual files. Stu Connolly has a great Objective-C wrapper for the FSEvents C API, it's called SCEvents and you can get it here: http://stuconnolly.com/blog/scevents-011/ The nice thing about FSEvents is that you just need to watch one folder and you will be notified of any changes that occur anywhere in the subfolder hierarchy of that folder. If you need file-level

Text change notification for an NSTextField

人走茶凉 提交于 2019-11-27 05:27:51
问题 I would like to use the code from the answer to this question: How to observe the value of an NSTextField on an NSTextField in order to observe changes on the string stored in the NSTextField. [[NSNotificationCenter defaultCenter] addObserverForName:NSTextViewDidChangeSelectionNotification object:self.textView queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note){ NSLog(@"Text: %@", self.textView.textStorage.string); }]; The class used here is an NSTextView. I can't find a

Mediator Vs Observer Object-Oriented Design Patterns

筅森魡賤 提交于 2019-11-27 02:57:10
I have been reading the Gang Of Four , in order to solve some of my problems and came across the Mediator pattern. I had earlier used Observer in my projects for making some GUI application. I am a bit confused as I do not find great difference between the two. I browsed to find the difference but could not find any apt answer for my query. Could some one help me to differentiate between the two with some good example which clearly demarcates the two? The Observer pattern: Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified