wood burning stoves
The moose likes Struts and the fly likes Jakarta struts books question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Jakarta struts books question" Watch "Jakarta struts books question" New topic

Jakarta struts books question

Prannav Santhosh

Joined: May 09, 2009
Posts: 29

I am new to the struts and i was going through "Jakarta struts book" and i have a question. In this book they have described a sample Action class as below.

Public class LoginAction extends Action {

public ActionForward execute (ActionMapping map , ActionForm form ,
HttpServletRequest req , HttpServletResponse res) throws Exception{
// Line 1
IAutenticate service = new SecurityService();

For the Line 1 , they gave the below description.
"The LoginAction" delegates the handling of business logic to another service. In this case , it relies on the SecurityService component. The SecurityService component may be a EJB or just a wrapper around JDBC code. In either way this is helpful because even if the implementation changes drastically , no code will have to change as long as the IAutenticate interface remains unchanged and is implemented by the service."

My question would be what if the Line one is replaced with the below code
SecurityService service = new SecurityService(); and not implementing an interface.


Costi Ciudatu
Ranch Hand

Joined: Oct 24, 2006
Posts: 74
This is a little confusing (if what you quoted is exact), since the comment states a best practice that's actually violated by the actual code.
What they mean is that the struts controller should not rely on a specific implementation of the IAuthenticate interface, it should simply get an implementation from somewhere (via dependency lookup / injection or some factory) so that if you replace the SecurityService with some other implementation you won't need to touch the LoginAction.
For the code to respect this principle, that assignment should be written as something like this:


In other words, to ensure the low coupling, you should never import the concrete SecurityService class in your struts action, therefore you cannot use 'new' here.

Hope that helps.
David Newton

Joined: Sep 29, 2008
Posts: 12617

You also have to consider how long ago that book was written.
I agree. Here's the link: http://aspose.com/file-tools
subject: Jakarta struts books question
It's not a secret anymore!