This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Is there a better way to do this? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Is there a better way to do this?" Watch "Is there a better way to do this?" New topic
Author

Is there a better way to do this?

Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
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

Joined: Oct 05, 2001
Posts: 1170

looks ok to me. not sure what the hashmap is doing though.
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
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

Joined: Jul 15, 2005
Posts: 241
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

Joined: Sep 24, 2003
Posts: 1608
Looks like a suboptimal implementation of the Strategy Design Pattern to me. The correct alternative to switch/case and excessive if/else.


Tony Morris
Java Q&A (FAQ, Trivia)
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
I plan to use it in the Command Controler stratagy for my webapp...

suboptimal

What code should I use instead?
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Got a bit carried away while I installed some Windows updates.
Only took 10 minutes anyway.







Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

> 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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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

Joined: Jul 15, 2005
Posts: 241
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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
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

Joined: Jan 29, 2003
Posts: 8791
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

Joined: Sep 24, 2003
Posts: 1608
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is there a better way to do this?
 
Similar Threads
Can Varible be used to create a Method ?
A question about Strategy Pattern
Restricting access to an object in the same package.
action Handlers: Do they have to be static??
Constants and switch statement