Hi,
Davis' solution is in my opinion the most elegant / simple solution to
this problem.
That being said, another solution I've used before for similar
problems, involved the used of the adapter pattern:
(note: typing this out of my head, I hope it compiles, but the main
idea is here)
------
Create an interface:
public interface HasVisibility {
void setVisible(boolean visible);
}
Create an adapter:
public class HasVisibilityAdapter implements HasVisibility {
private final Widget widget;
public HasVisibilityAdapter(Widget widget) {
// Out of scope here, but these 2 lines could be adequately
replaced with
// something like google-collection's
Preconditions.checkNotNull() but using an "assert" statement instead
// you would then have: "this.widget =
Assertions.assertNotNull(widget);"
assert widget != null;
this.widget = widget;
}
@Override
public void setVisible(boolean visible) {
widget.setVisible(visible);
}
}
Then, in your Display interface:
public interface MyDisplay {
// ...
HasVisibility getOkButton();
}
and the implementation:
public class MyDisplayImpl {
// ...
private Button okButton;
// ...
public HasVisibility getOkButton() {
return new HasVisibilityAdapter(this.okButton);
}
}
Pros:
- Uses composition instead of inheritance: instead of having to
subclass all of your widgets to make them implement the HasVisibility
interface, you only have to create one adapter, and this adapter will
work for every Widget subclasses.
- Hides the "Widget" from your presenter. Your presenter and display
only know about the nice "HasVisibility" interface. You may test and
mock them easily.
Cons:
- It adds more complexity than Davis' solution (must create an
interface + an adapter). Simplicity should often be preferred.
- Performance might potentially be a problem if you call the getter
very often, due to creating an adapter object each time. IMO, this is
not a big problem in such a case, and the GWT compiler might even be
able to optimize this out...
That being said, in more complex use cases, using such an adapter
could be interesting.
I have in mind cases where you would want to expose an interface with
many different methods (instead of just one like you do here), in many
different presenters. Using such an adapter would remove a lot of code
duplication in such a case.
/end of wall of text ;)
- Etienne
On Jan 15, 4:46 am, Davis Ford <[email protected]> wrote:
> The nice thing about doing it this way is that you can still unit test it
> 100% in JUnit.
>
> I used EasyMock to mock out my Display interface and any other classes my
> presenter happened to depend on. This way I could always mock return and
> assert that the presenter did the right thing.
>
> I punted on testing that my Display implementation actually did what it said
> it would do via the interrace contract, but typically the code was so
> minimal that I didn't care.
>
> On Thu, Jan 14, 2010 at 10:31 PM, Dominik Steiner <
>
>
>
> [email protected]> wrote:
> > Hi Davis,
>
> > thanks for the fast reply.
>
> > That is actually a good idea, I was always looking for getters to implement
> > on the Display, never occured to me that simple methods that go the other
> > way might be as efficient too.
>
> > Thanks again for your help.
>
> > Dominik
>
> > Hi Dominik, why not have a display interface like this?
>
> > interface Display {
> > void toggleVisible(boolean toggle);
> > }
>
> > If you need the presenter to toggle specific widgets on the display create
> > an enum:
>
> > interface Display {
>
> > enum WidgetType {
> > BUTTON,
> > TEXTBOX
> > };
>
> > void toggleVisible(boolean toggle, WidgetType type);
> > }
>
> > On Thu, Jan 14, 2010 at 8:45 PM, Dominik Steiner <
> > [email protected]> wrote:
>
> >> Hi there,
>
> >> I'm wondering how you best handle the situation that in your Presenter
> >> you need to call setVisible() on some UI Object returned via the
> >> Display interface?
>
> >> I could of course just write something like this in the Presenter
>
> >> interface Display{
> >> Widget getOkButton();
> >> }
>
> >> but that would made the class not JUnit testable. So I'm wondering why
> >> there are interfaces in GWT like HasText in order to set text on a
> >> Label or something but no interface in order to pass that to the
> >> Presenter for setting the visiblity of an UI element?
>
> >> Am I missing something?
>
> >> Dominik
>
> >> P.S. of course I could just extend the Widget class with my own custom
> >> ones and let this implement a custom Interface that defines setVisible
> >> () in order to pass that to the Presenter to make it testable, but
> >> wondering if others have run into the same problem?
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "Google Web Toolkit" group.
> >> To post to this group, send email to [email protected].
> >> To unsubscribe from this group, send email to
> >> [email protected]<google-web-toolkit%[email protected]>
> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/google-web-toolkit?hl=en.
>
> > --
> > Zeno Consulting, Inc.
> > home:http://www.zenoconsulting.biz
> > blog:http://zenoconsulting.wikidot.com
> > p: 248.894.4922
> > f: 313.884.2977
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Google Web Toolkit" group.
> > 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.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Google Web Toolkit" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected]<google-web-toolkit%[email protected]>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/google-web-toolkit?hl=en.
>
> --
> Zeno Consulting, Inc.
> home:http://www.zenoconsulting.biz
> blog:http://zenoconsulting.wikidot.com
> p: 248.894.4922
> f: 313.884.2977
--
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
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.