The way kozura suggested means that you create a new instance with all the
code for every possibility every time (as well as adding extra code for the
switch).

What have you got against creating a command with just the code that that
menu item needs? How does that 'blow up the code in an unnecessary way'?

Kozura's way is like going into a restaurant and, whatever you order, they
prepare and bring you everything that there is on the menu but you are only
allowed to eat what you ordered. All the rest is thrown away (but takes up
space all the time you are there).

Ian

http://examples.roughian.com


On 30 May 2010 11:21, Magnus <alpineblas...@googlemail.com> wrote:

> Thanks a log!
>
> It seems that its the style of java programming to instantiate new
> classes all the time. In this case, the normal way seems to be to
> declare a new class for every menu item and to instantiate each of
> these classes a single time, just to connect a menu item to a piece of
> code. Ist this the normal way in java? Doesn't this blow up the code
> in an unnecessary way??
>
> Magnus
>
> On May 29, 8:25 pm, kozura <koz...@gmail.com> wrote:
> > No, but you can do something close:
> >
> > m.addItem("Login", new MenuCmd(1));
> > m.addItem("Logout", new MenuCmd(2));
> >
> > class MenuCmd implements Cmd
> > {
> >     private int whichCmd;
> >     public MenuCmd(int which) {whichCmd = which;}
> >     public execute()
> >     {
> >         switch(whichCmd)...
> >     }
> >
> > }
> >
> > On May 29, 8:08 am, Magnus <alpineblas...@googlemail.com> wrote:
> >
> > > Hi,
> >
> > > I would like to give all my menu items the same command object:
> >
> > >   m.addItem ("Login", cmd);
> > >   m.addItem ("Logout", cmd);
> > >   m.addItem ("Register",cmd);
> > >  ...
> >
> > > Is it possible to retrieve the selected menu item within the command
> > > object's method "execute"?
> >
> > > The reason is that I want to keep my code compact, i. e. using a
> > > single method that reacts on menu item selections and distinguishes
> > > the items with a select statement...
> >
> > > Many thanks
> > > Magnus
>
> --
> You received this message because you are subscribed to the Google Groups
> "Google Web Toolkit" group.
> To post to this group, send email to google-web-tool...@googlegroups.com.
> To unsubscribe from this group, send email to
> google-web-toolkit+unsubscr...@googlegroups.com<google-web-toolkit%2bunsubscr...@googlegroups.com>
> .
> 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 google-web-tool...@googlegroups.com.
To unsubscribe from this group, send email to 
google-web-toolkit+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-web-toolkit?hl=en.

Reply via email to