Thank you Brian and Brian (what a coincidence, I got two replies from
two different Brians).
The GUI example was just that, an example... and maybe not the best
one. Here is another one:
Let's say I want to build a menu composed of menu items and submenus.
Each menu item has a label and an associated action, etc. If you
externalize that information in YAML, this is how it could look like:
items:
- label: Menu item 1
activity: activity 1
- label: submenu A
items:
- label: subitem a1
activity: activity a1
- label: subitem a2
activity: activity a2
- label: Menu item 2
activity: activity 2
As I said before I am not an expert on Spring but I think that this
could be easily expressed as a Spring-based XML bean definition, and
this menu tree structure could then be instantiated on runtime through
a single line of code.
Is there an equivalent way to do this with Guice? (and yes, I know
Guice does not read XML but that is supposed to be an advantage, not a
limitation). In that respect, it seems to me that other DI frameworks
can also be used as flexible builders in order to instantiate
arbitrary object trees, whereas Guice is only able to create object
trees that explicitly match the Java code structure, so a given Java
class can only generate a single object tree which will always look
the same, except maybe for constant values for basic object types such
as int or String.
To put it another way, how can you use Guice to build something like
this?
class MenuElement {
@Inject String label;
}
class MenuItem extends menuElement {
@Inject Action action;
}
class MenuTree extends MenuElement {
@Inject List<MenuElement> items;
}
And then I want it to instantiate an arbitrary menu structure for my
application, for example the above YAML structure.
As far as I can tell Guice is not enough, and some builder has to be
developed around it. For example I could create a builder that
receives a Guice Injector instance and uses it to create the object
trees/graphs by reading a file or even by using an EDSL.
Please correct me if I am wrong... notice that reading from an
external file is not a requirement: the way Guice avoids XML is nice,
and I wonder if it can be useful for this example too.
Luis.
On Dec 22, 5:30 pm, Brian Pontarelli <[email protected]> wrote:
> There are patterns you can use to achieve this in Guice. I would warn
> you that using the method you describe above puts a lot of faith into
> the framework for constructing your UI exactly how you want. You might
> be better off using a factory or a builder to construct the UI. Then
> you can have multiple implementations of that.
>
> -bp
>
> On Dec 20, 2008, at 3:09 PM, Luis wrote:
>
>
>
> > Sorry if I am asking something too basic but I am totally new to
> > Guice.
>
> > What stops me for thinking that this is a great framework is that all
> > the examples I have seen use objects with a very fixed and predefined
> > object graphs. For example object A always contains objects B and C,
> > and so forth. But often one needs to create more complex structures,
> > where a given object contains a list of other objects, and this list
> > varies for each instance. A typical example is for GUIs: using an
> > imaginary windowing framework,
>
> > Window win = new Window();
> > win.add(new Label("Name: ");
> > win.add(new InputField());
> > win.add(new Button("OK));
> > win.add(new Button("Cancel");
> > // etc...
>
> > So even if I say:
>
> > class Window {
> > �...@inject public void add(Component c) {
> > // blabla...
> > }
> > }
>
> > I don't see how can I use Guice to build the above "win" instance...
> > and later use it to create another Window instance containing other
> > stuff, possibly nesting components such as tabbed panes or whatever.
>
> > Is that beyond the capabilities of Guice? I haven't used Spring
> > either, but from reading its documentation, it seems to accept
> > building beans containing lists, maps, and other arbitrary graphs.
> > If Guice does not intend to implement a full Builder Pattern, how do
> > you recommend to extend it or combine it with other framework?
>
> > Thanks in advance,
>
> > Luis.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"google-guice" 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-guice?hl=en
-~----------~----~----~----~------~----~------~--~---