File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is there a better way to do this?

 
Daniel Prene
Ranch Hand
Posts: 241
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the setup I have:


I'm trying to be able to execute a command by a String.. I shortened the code (took out exception catching) for the sake of simplicity.. Is there a more efficient way?

Any help would be apreciated... Thanks!
-D.P.
[ October 02, 2005: Message edited by: Daniel Prene ]
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
looks ok to me. not sure what the hashmap is doing though.
 
Norm Radder
Ranch Hand
Posts: 707
3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everytime create() is called a new HashMap is created and loaded. Shouldn't this be done just one time? Say in a static init?
 
Daniel Prene
Ranch Hand
Posts: 241
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
looks ok to me. not sure what the hashmap is doing though.

It's to avoid having to use reflection...

Everytime create() is called a new HashMap is created and loaded. Shouldn't this be done just one time? Say in a static init?

Doh! I was thinking that get returned a refrence... but that's foolish. Thanks
[ October 02, 2005: Message edited by: Daniel Prene ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like a suboptimal implementation of the Strategy Design Pattern to me. The correct alternative to switch/case and excessive if/else.
 
Daniel Prene
Ranch Hand
Posts: 241
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I plan to use it in the Command Controler stratagy for my webapp...

suboptimal

What code should I use instead?
 
Tony Morris
Ranch Hand
Posts: 1608
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got a bit carried away while I installed some Windows updates.
Only took 10 minutes anyway.







 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Command get(String name) throws NullPointerException, IllegalArgumentException;

Note that NullPointerException, IllegalArgumentException and also UnsupportedOperationException are unchecked exceptions, you don't need to declare with a throws clause that your methods throw them.
 
Tony Morris
Ranch Hand
Posts: 1608
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but you should so that that contractual information is available upon reflection. Or even better, do away with the language dependancy and use an annotation.

 
Daniel Prene
Ranch Hand
Posts: 241
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow... Thank you! I really apreciate it. I'll try it out this afternoon. Is this how <a href="http://java.sun.com/blueprints/corej2eepatterns/Patterns/FrontController.html"> they</a> are doing it in "Example 7.16 Command and Controller Strategy Sample Code" ?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use this design all the time, and I'm about to again today. I often load it from configuration. A "glue" module reads configuration and puts singleton command instances or class names into the map. The factory returns the singletons or Class.forName().new() instances but it has no knowledge of any command implementations.
 
Daniel Prene
Ranch Hand
Posts: 241
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh... What would be the disadvantage to using a bunch of if statements? It actually seems more effective/eficiant/simpler. What would I be sacroficing besides elegance?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a couple reasons to avoid big if-else structures.

It's a design goal (one prioritized by each of us among many) for a class to have exactly one reason to change. If a class embodies a core business policy I only want to change it if the policy changes. If I can plug in new strategies without touching the policy, I'm closer to one reason to change.

In big systems we watch dependencies very carefully. Some of those branches in the if-else will reference other classes. If I introduce the other classes through a factory I can introduce new strategy classes and even whole new packages without touching my policy class.

Plus, every time you open up that if-else structure to add a new choice you have to regression test the whole darned thing to make sure you didn't break it. Every change is an opportunity to fat finger something, so making classes that you never have to open in the editor is A Good Thing. In fact, I might want to shrink-wrap and sell my policy class and not invite customers to open it up and add choices.

Any of those ring true for you?
[ October 04, 2005: Message edited by: Stan James ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two major advantages.
One is that you can dynamically set the strategy; for example, declare it in an external resource, like an XML file. A change to a if/else requires a recompile (adding a new else branch to add a new strategy for example). The other advantage is that you can achieve O(1) lookup of the startegy, which can also be achieved with switch/case under certain conditions, but typically is O(n) as is the case for if/else. It is for these reasons that using switch/case should always be avoided - it offers no advantages ("saves typing" is superficial).

I once knew someone, about 8 years ago, who refused to use if/else on a C++ project.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic