1

What could be the best practices of writing the following case:

We have a controller which based on what paths users take, take different actions. For example:

if user chooses the path /path1/hello it will say hello. If a user chooses /path1/bye?name="Philipp" it will invoke sayGoodBye() and etc.

I have written a switch statement inside the controller which is simple, however IMO not efficient. What are the best way to implement this, considering that paths are generally String.

private void takeAction()
    {
    switch (path[1])
    {
    case "hello":
        //sayHello();
        break;
    case "bye":
        //sayBye();
        break;
    case "case3":
        //Blah();
        break;
             ...
    }
}

UPDATE: In my context, the path is dynamically generated, meaning that there are not specific pages to be there. If the path is /Amir/ then Amir will be dynamically generated. Therefore, as Mark Leighton Fisher explained, it is rather difficult to put them in hashes.

FidEliO
  • 875
  • 3
  • 14
  • 24
  • Don't reinvent the wheel. Choose an MVC framework that does that for you (and much more). – JB Nizet Dec 15 '12 at 15:36
  • @JBNizet , it is one of the most prevailing banes of Java developers. You tend to use frameworks without understanding the underlaying concepts. I would say making a routing mechanism (especially if done for educational purposes) is a good idea. Later then you can pick the framework. But you will have a clear understanding of problems, which are solved by that exact part of said framework. – tereško Dec 15 '12 at 15:53
  • No problem if it's purely for educational purpose, and you throw away the code after that to use a well-tested, established framework. But don't fall into the NIH syndrome trap. – JB Nizet Dec 15 '12 at 16:00
  • @JBNizet , as a PHP developer I can say that sometimes you *have to* reinvent the wheel, because there are situations when all the "popular" wheels are not even remotely elliptical. – tereško Dec 15 '12 at 17:43

2 Answers2

1

Your controller has taken up additional responsibility: URL routing.

Instead this should be performed before the controller's method is executed. The as nothing to do with your controller. Most obvious pattern you could look at could be front controller.

As for the specific implementation, the simplest way would be to create a Router instance, which produces a Request object from user's input (both the URL itself and other parameters, like POST values). The you extract both the name of controller and the method from this Request object.

Further-more, it would be reasonable to provide said Request instance as parameter for the action, so that this action has a clear access to other values from user's input.

You might also find this beneficial: Spring Framework docs for DispatcherServlet

tereško
  • 58,060
  • 25
  • 98
  • 150
  • In case I am working with Java SE, I was wondering how can I implement the same features safely. I am woking with sun's built-in HttpServer. – FidEliO Dec 15 '12 at 16:42
  • Well. All you controllers would be in separate namespace/package. You just check, if the URL that user requested maps to [exiting class withing said namespace](http://stackoverflow.com/questions/5794227/how-to-check-if-class-exists-somewhere-in-package). As for checking, if action exists in controller, there is alread an [exception](http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/NoSuchMethodError.html). – tereško Dec 15 '12 at 17:09
  • Why not just directly put the code that e.g for example if value is login, then create the first controller, if it is user create second controller and ... ? – FidEliO Dec 15 '12 at 18:16
1

It is cleaner and faster to use hashes than Switch statements for the actual routing, especially when you have many routes and you are only testing for String equality.

Mark Leighton Fisher
  • 5,609
  • 2
  • 18
  • 29
  • How can you implement that? I have 3 controllers which implement a single interface. I want to invoke those classes based on the routing. I cannot see how hashes are related in this context – FidEliO Dec 15 '12 at 18:14