• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

lots of ifs and elses what's wrong here?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
suppose we try to provide a menu like thing
for user inputs.
Now in my code i obviosuly have lots of ifs
and elses
if (input == a)
do this
else if (input == b)
do this
is there a oo way to handle this kind of a situation?
w'd command pattern help here?. If so how?.
in a servlet centric design we normally have a controller which does a look up on the busines object and forwards the request to the appropriate business obj. it does not have a if else kind of stuff.
thanks
prav.

 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From Desing Patterns in the Motivation section for the Command pattern:


The application configures each MenuItem with an instance of a concrete Command subclass. When the user selects a MenuItem, the MenuItem calls Execute on its comment, and Execute carries out the operation.


In other words, yes.
Warning: I don't no much at all about Swing
To see this applied, look in you JDK installation under the demo/jfc folder. You could look at the folder demo/jfc/notepad for example. Stylepad is a bit more advanced (it extends Notepad). You'll see that a javax.swing.Action is a Command. Be sure to check out Stylepad.properties and Notepad.properties under the resources folder. You'll notice references to Actions like "copy-to-clipboard" which aren't defined by either of these programs -- they're being reused from Swing.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic