On Tuesday, May 15, 2012 6:22:02 PM UTC+2, Yaniv wrote:
>
> I think your original hunch was correct. I like using a presenter or 
> "delegate" pattern. Choose an activity to control your widget. If your 
> widget is part of a view, that view should have a corresponding 
> activity and that should be the thing that controls your widget and 
> fires events on its behalf. Since your widget is the one that has 
> special needs, it should define what it needs. In the widget class 
> define an interface 
>
> public interface MyWidgetDelegate { 
>   public void doMyThing(); 
> } 
>
> Add a method to your widget class: 
> public void setDelegate(MyWidgetDelegate delegate) { 
>  this.delegate = delegate; 
> } 
>
> Now your activity implements MyWidgetDelegate and the activity is 
> responsible for calling setDelegate(this); 
>
> Now whenever your widget needs it can call delegate.doMyThing() which 
> the activity implements and can be responsible for interacting with 
> the eventbus or doing any other smart thing. 
>
> I use this pattern all over the place and I think it's the cleanest 
> way to deal with this problem.


If all (or most) activities implement the interface the same, then you can 
cut duplicated code by having them *provide* the MyWidgetDelegate 
implementation to the view, so that you can have a single concrete class 
used everywhere. And in case an activity needs to do something special / 
different from the others, then it could still implement the interface and 
provide itself as the implementation to the view.
As for "providing the MyWidgetDelegate to the view", you can either add a 
getter to the presenter/activity (called by the view) or add a setter to 
the view (called by the presenter/activity). I prefer always pushing things 
to the other side, so I'd go with the setter, but YMMV.

-- 
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/google-web-toolkit/-/H5kUOmqzB3UJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-web-toolkit?hl=en.

Reply via email to