observer-pattern

Callback/Command vs EventListener/Observer Pattern

╄→尐↘猪︶ㄣ 提交于 2019-11-28 15:21:28
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{ private EventListener listener; public registerEventListener(EventListener listener){ this.listener

vector of pointer to object - how to avoid memory leak?

泄露秘密 提交于 2019-11-28 06:53:03
问题 How do we ususaly deal with a vector whose elements are pointers to object? My specific question is the comment at the end of the code supplied below. Thanks. class A { public: virtual int play() = 0 ; }; class B : public A { public: int play() {cout << "play in B " << endl;}; }; class C : public A { public: int play() {cout << "play in C " << endl;}; }; int main() { vector<A *> l; l.push_back(new B()); l.push_back(new C()); for(int i = 0 ; i < l.size();i++) { l[i]->play(); } //Do i have to

Android custom listener for an event

ⅰ亾dé卋堺 提交于 2019-11-28 06:31:06
问题 I'm trying to fire an event when an integer value is updated, but it's failing. Here's the code I'm using: Declaring The Custom Listener public class fieldactivity extends AppCompatActivity implements View.OnClickListener { OnModeUpdate modeupdate; //Create custom listener for mode update int mode = 1; Mode Update Code protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fieldsignals); Button button = (Button) findViewById(R.id.mode

iPhone : camera autofocus observer?

爱⌒轻易说出口 提交于 2019-11-28 05:03:21
I would like to know if it's possible to receive notification about autofocus inside an iPhone application? I.E, does it exist a way to be notified when autofocus starts, ends, if it has succeed or failed... ? If so, what is this notification name ? I find the solution for my case to find when autofocus starts / ends. It's simply dealing with KVO (Key-Value Observing). In my UIViewController: // callback - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if( [keyPath isEqualToString:@"adjustingFocus"] ){ BOOL

Text change notification for an NSTextField

微笑、不失礼 提交于 2019-11-28 04:50:29
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 notification in NSTextField to use instead of NSTextViewDidChangeSelectionNotification. Is there a

How to create custom event in symfony2

*爱你&永不变心* 提交于 2019-11-28 03:56:10
I want to create custom events called user_logged so that i can attach my listeners to those events. I want to execute few functions whenever user has logged in. Lusitanian Create a class which extends Symfony\Component\EventDispatcher\Event . Then, use the event dispatcher service to dispatch the event: $eventDispatcher = $container->get('event_dispatcher'); $eventDispatcher->dispatch('custom.event.identifier', $event); You can register your event listener service like so: tags: - { name: kernel.event_listener, event: custom.event.identifier, method: onCustomEvent } Vladimir Kovpak This

Observer Design Pattern vs “Listeners”

人走茶凉 提交于 2019-11-28 03:05:08
It seems to me that the Observer design pattern as described in GOF is really the same thing as Listeners found in various toolkits. Is there a difference between the concepts, or are Listeners and Observers really the same thing. (I'm not looking for any specific computer language implementation, I just want to understand the difference (if any) from a design point of view. Yes, I know there are several answers to similar questions on SOF, but they're rooted in specific questions about specific languages -- I'm looking for a design answer, not a language answer.) Whether the term "listener"

Difference between Observer, Pub/Sub, and Data Binding

走远了吗. 提交于 2019-11-28 02:32:37
What is the difference between the Observer Pattern , Publish/Subscribe , and Data Binding ? I searched around a bit on Stack Overflow and did not find any good answers. What I have come to believe is that data binding is a generic term and there are different ways of implementing it such as the Observer Pattern or the Pub/Sub pattern. With the Observer pattern, an Observable updates its Observers. With Pub/Sub, 0-many publishers can publish messages of certain classes and 0-many subscribers can subscribe to messages of certain classes. Are there other patterns of implementing "data binding"?

Getting error for generic interface: The interface Observer cannot be implemented more than once with different arguments:

点点圈 提交于 2019-11-28 01:55:17
I am getting this error in Eclipse while writing a GWT app The interface Observer cannot be implemented more than once with different arguments: Observer<CompositeListData > and Observer<DialogBoxAuthenticate> public class CompositeWordLists extends Composite implements Observer<DialogBoxAuthenticate>, Observer<CompositeListData> Here is the interface public interface Observer<T> { public void update(T o); } Is this right? How can I get around this problem without having to create a multitude of Observer classes for every possible event? Salman Paracha Because of type erasure you can't

Update: How to find event listeners on a DOM node in prototype?

淺唱寂寞╮ 提交于 2019-11-28 01:12:11
I'm looking for an updated answer to this question . It seems that Event.observers is no longer used (perhaps to avoid memory leaks) in Prototype 1.6+, so how do I track down now what event listeners are attached to an element? I know Firebug has a "break on next" button, but there are several mouse listeners on the body element that execute before I can get to the behavior that I want on another particular element, so is there some other way? Crescent Fresh I've update the answer you linked to with more comprehensive Prototype coverage accounting for changes in versions 1.6.0 to 1.6.1 . It