• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

To FrontController or not to FrontController?

 
Ranch Hand
Posts: 100
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,

I'm trying to implement an interface from a mobile application and am wondering if the Front Controller pattern is the correct way to do this.

The interface I have to the mobile application is a single java method;

public String sendAndReceive(String methodName, String parameters)

...the parameters are passed in as a JSON object.

The goal is for me to take the method name and JSON parameters, translate them into a call to a back end system and translate the return into a JSON object for consumption by the mobile application.

There are roughly 60 different method calls that the mobile application could make. I need to figure out the best way to invoke the appropriate logic as was thinking the FrontController pattern would be best placed for this - using a dispatcher to do the mapping of methodName to classes which would manage the invocation of the method.

My concern is that I'm going to have 60 different classes (and a big dispatcher method with lots of if else constructs!) if I do this.

Any thoughts?

Bic
 
Ranch Hand
Posts: 531
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chris Bicnal wrote:Hey guys,

I'm trying to implement an interface from a mobile application and am wondering if the Front Controller pattern is the correct way to do this.

The interface I have to the mobile application is a single java method;

public String sendAndReceive(String methodName, String parameters)

...the parameters are passed in as a JSON object.

The goal is for me to take the method name and JSON parameters, translate them into a call to a back end system and translate the return into a JSON object for consumption by the mobile application.

There are roughly 60 different method calls that the mobile application could make. I need to figure out the best way to invoke the appropriate logic as was thinking the FrontController pattern would be best placed for this - using a dispatcher to do the mapping of methodName to classes which would manage the invocation of the method.

My concern is that I'm going to have 60 different classes (and a big dispatcher method with lots of if else constructs!) if I do this.

Any thoughts?

Bic


Hi, Chris,

FrontController is good when you wish to apply cross-cutting concerns at the controller. For example, a request comes in and you wish to apply security rules to it before it is passed any further within your app.

With best regards,

Anton.
 
Chris Bicnal
Ranch Hand
Posts: 100
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anton,

The centralisation did spring to mind when thinking about this - logging/auditing, authentication etc.. would be simplified.

I think my bigger question boils down to, what's the best way to implement an efficient mechanism that accepts string method names and parameters and translates them into actually method calls.

Bic
 
Anton Golovin
Ranch Hand
Posts: 531
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chris Bicnal wrote:Anton,

The centralisation did spring to mind when thinking about this - logging/auditing, authentication etc.. would be simplified.

I think my bigger question boils down to, what's the best way to implement an efficient mechanism that accepts string method names and parameters and translates them into actually method calls.

Bic



Hi, Chris,

Reflection?

With best regards,

Anton.
 
Anton Golovin
Ranch Hand
Posts: 531
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anton Golovin wrote:

Chris Bicnal wrote:Anton,

The centralisation did spring to mind when thinking about this - logging/auditing, authentication etc.. would be simplified.

I think my bigger question boils down to, what's the best way to implement an efficient mechanism that accepts string method names and parameters and translates them into actually method calls.

Bic



Hi, Chris,

Reflection?

With best regards,

Anton.



Additionally,

You can store instantiation objects with one method in a Map, retrieve them based on your method call, and pass other entries within the JSON as params.

That way you avoid reflection at the cost of some careful planning. It will be faster at the expense of more memory consumption out of the box. Plus, you will have to essentially deal with Singletons, so keep multi-threaded issues in mind.

With best regards,

Anton.
 
Chris Bicnal
Ranch Hand
Posts: 100
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anton,

How about we use both?

Store the method name > class mappings in a Map and then use reflection to instantiate. If they all inherit from the same parent my delegation logic is very short and quick - and thread safe.

I can populate the Map from a configuration file or data source once on initiation, but it lends itself to growth pretty well.

What do you think?

Chris
 
Anton Golovin
Ranch Hand
Posts: 531
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chris Bicnal wrote:Anton,

How about we use both?

Store the method name > class mappings in a Map and then use reflection to instantiate. If they all inherit from the same parent my delegation logic is very short and quick - and thread safe.

I can populate the Map from a configuration file or data source once on initiation, but it lends itself to growth pretty well.

What do you think?

Chris



Hi, Chris,

I think you're talking about using the Command design pattern and calling that method execute(params).

If you look at Struts, Spring MVC or ASP.NET MVC - pretty much same mechanism.

With best regards,

Anton.
 
Onion rings are vegetable donuts. Taste this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic