aspose file tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes Controller as a Singleton class in Front Controller Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Reply Bookmark "Controller as a Singleton class in Front Controller Pattern" Watch "Controller as a Singleton class in Front Controller Pattern" New topic
Author

Controller as a Singleton class in Front Controller Pattern

Kripal Singh
Ranch Hand

Joined: Jul 26, 2001
Posts: 254
Trying to develop a prototype for Front Controller Pattern. I am considering implementing Controller as a singleton class. Now the Controller will be instantiated only once in the application lifecycle (client being servlets and getinstance of singleton controller being called in init method). In this situation all of the request will go through singleton controller (controller will just delegate requests and will not have any state, property or instance variables). Other components will remain the same as explained in j2ee patterns.
Is there possible architecture flaw /threading issue in this case. Any idea or suggestion
Thanks


# Help an unprivileged kid.<br /> Whatever u do will make a difference...<br /> ...to a child's life & ur own #<br /><a href="http://www.cry.org/" target="_blank" rel="nofollow">www.cry.org/</a>
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6913
My main issue with this is that I don't see the need for a Singleton. Given this, and the way that Singleton is so commonly misused or misunderstood, I'd avoid it.
Can you explain why you feel you need a Singleton here, rather than just a regular object?
The traditional way of doing this in a servlet/JSP context is to instantiate your Controller object once when the web-app starts, and place a reference to it in the "application" context. Then any servlets and JSPs which need it just fetch it from the application context and use it. No Singleton required.
This, incidentally, is a common way of dealing with any resource shared between servlets and JSPs, and can also be used for JNDI handles, JDBC connection pools and so on.


A Convergent Visionary ~ Frank's Punchbarrel Blog ~ LinkedIn profile
Kripal Singh
Ranch Hand

Joined: Jul 26, 2001
Posts: 254
thanks it is a good idea to share application resources. i will not use the singleton approach . do you have any other idea or method for doing this .
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6913
OK, I'm glad you decided agains the Singleton, but I'm not entirely sure what you are asking now. Other idea or method for what, exactly ?
Kripal Singh
Ranch Hand

Joined: Jul 26, 2001
Posts: 254
The reason for making controller singleton was to create it once in servlet life cycle (not to create a new controller object with every request). The idea you suggested is to instantiate Controller object once when the web-app starts, and place a reference to it in the "application" context.
What i meant was that is there any other way of doing this (persisting it through the web-app life cycle)?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Kripal Singh:
The reason for making controller singleton was to create it once in servlet life cycle (not to create a new controller object with every request). The idea you suggested is to instantiate Controller object once when the web-app starts, and place a reference to it in the "application" context.
What i meant was that is there any other way of doing this (persisting it through the web-app life cycle)?

Why do you need another way? Is putting it into the application context inappropriate for your problem? If so, why?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6913
If you really want to, you can store it in a JNDI context, or make it available as a web service or whatever, but why bother. All modern servlet/JSP containers have an application context, and load-on-startup servlets in which you can create things like this. The web application lifecycle processing takes care of starting things once when the web-app starts, and shuts them down when it stops. It's what they are for, really.
Can you help me with some ideas of what might be an ideal solution for you, if this is not right, somehow?
Kripal Singh
Ranch Hand

Joined: Jul 26, 2001
Posts: 254
Application context will work fine for me. But in multi tier architecture if there is need for controller object in a backend layer we will have to get hold of application context first. Which means we have to keep passing the context through all layers. Correct me if i am wrong.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6913
It looks as though you are over-generalizing the pattern. The kind of controller you use in a servlet/JSP system is only responsible for controlling the interaction between HTTP requests, servlets and JSP.
I can't see any need to pass this controller around at all. If other layers use a similar pattern, they would need their own controllers for their own contexts.
Can you give us an example of another layer which might need access to a web-application controller?
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Controller as a Singleton class in Front Controller Pattern
 
Similar Threads
Using Servlet as a Dispatcher
Struts selection
How much struts is needed for passing SCWCD
StrutsConfig.xml dilemma
Lyons book cover Struts?