What is the best way to implement common functionality between all actions?
Hello all. My question might not be viewed as a Struts question, but more along the lines of an architecture question. But, on the other had, it is struts architecture, so I'm ok! ;-) This is the situation: some of our designers are creating subclasses of Action, while others are creating subclasses of DispatchAction. Now, there's some common functionality that needs to be executed by all subclasses of both Action and DispatchAction. The way that we've done it for Actions is by implementing execute in a base class and in it calling an abstract method that subclasses need to implement, like this: public class BaseAction extends Action { protected abstract ActionMapping internalExecute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ); public ActionMapping execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) { // ... Perform common work here return internalExecute( mapping, form, request, response ); } And, of course, for our base subclass of DispatchAction, we do something similar: public class BaseDispatchAction extends DispatchAction { public ActionMapping execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) { // ... Perform common pre-execution work here return super.execute( mapping, form, request, response ); } Plus, there are utility methods common to both BaseAction and BaseDispatchAction. Now, any half-decent coder's response to the phrase 'common functionality' is to think, is there a way that I can centralize this work, to reduce the repeated code? So, my question is this: what is the best way to add common functionality to subclasses of both Action and DispatchAction? As DispatchAction is a subclass of Action, the initial thought is to add it to Action, but that's un-necessarily messy. Another way that I've thought of is to create a 'common action functions' class, where I place implementations of all the functionality I might use, and then in my BaseAction and BaseDispatchAction, proxy/redirect to that CommonActionFunctions class. However, I try my best to write the best code possible, and this isn't as clean as I'd like it to be (I'm still duplicating method signatures, etc). What other ways have people found to deal with this situation? JDG -- Jay Glanville Web Developer [EMAIL PROTECTED] (613) 725-2030 x393 http://www.naturalconvergence.com - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: What is the best way to implement common functionality between all actions?
Hi I had this problem and i have done the following I defined a interface called CommonFuntionInterface which defines all the common methods for the funtions, and then a class CommonFuntion which implements this interface and do all the common logic in here then in your Action and DispatchAction Subclasses create instance of CommonFuntion class and call the methods in here, I guess this is called facade pattern ( i am not very good in patterns :-) I will give some code below as example public interface CommonFuntionInterface { public String doSomething(); } public class CommonFuntion implements CommonFuntionInterface { public String doSomething() { // to do logic } } public MyAction extend Action { private CommonFuntion common; public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { common = new CommonFunction(); return performTask(mapping, form, request, response); } public String doSomething() { return common.doSomething(); } } do this same for DispatchAction Hope this helps Ashish --- Glanville, Jay [EMAIL PROTECTED] wrote: Hello all. My question might not be viewed as a Struts question, but more along the lines of an architecture question. But, on the other had, it is struts architecture, so I'm ok! ;-) This is the situation: some of our designers are creating subclasses of Action, while others are creating subclasses of DispatchAction. Now, there's some common functionality that needs to be executed by all subclasses of both Action and DispatchAction. The way that we've done it for Actions is by implementing execute in a base class and in it calling an abstract method that subclasses need to implement, like this: public class BaseAction extends Action { protected abstract ActionMapping internalExecute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ); public ActionMapping execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) { // ... Perform common work here return internalExecute( mapping, form, request, response ); } And, of course, for our base subclass of DispatchAction, we do something similar: public class BaseDispatchAction extends DispatchAction { public ActionMapping execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) { // ... Perform common pre-execution work here return super.execute( mapping, form, request, response ); } Plus, there are utility methods common to both BaseAction and BaseDispatchAction. Now, any half-decent coder's response to the phrase 'common functionality' is to think, is there a way that I can centralize this work, to reduce the repeated code? So, my question is this: what is the best way to add common functionality to subclasses of both Action and DispatchAction? As DispatchAction is a subclass of Action, the initial thought is to add it to Action, but that's un-necessarily messy. Another way that I've thought of is to create a 'common action functions' class, where I place implementations of all the functionality I might use, and then in my BaseAction and BaseDispatchAction, proxy/redirect to that CommonActionFunctions class. However, I try my best to write the best code possible, and this isn't as clean as I'd like it to be (I'm still duplicating method signatures, etc). What other ways have people found to deal with this situation? JDG -- Jay Glanville Web Developer [EMAIL PROTECTED] (613) 725-2030 x393 http://www.naturalconvergence.com - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] __ Do you Yahoo!? Yahoo! Search - Find what youre looking for faster http://search.yahoo.com - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: What is the best way to implement common functionality between all actions?
Helpful hints.Have you done any deletion code in thsi common functionality-possibly using String arrays,... Thanks Ashish Kulkarni [EMAIL PROTECTED] wrote: Hi I had this problem and i have done the following I defined a interface called CommonFuntionInterface which defines all the common methods for the funtions, and then a class CommonFuntion which implements this interface and do all the common logic in here then in your Action and DispatchAction Subclasses create instance of CommonFuntion class and call the methods in here, I guess this is called facade pattern ( i am not very good in patterns :-) I will give some code below as example public interface CommonFuntionInterface { public String doSomething(); } public class CommonFuntion implements CommonFuntionInterface { public String doSomething() { // to do logic } } public MyAction extend Action { private CommonFuntion common; public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { common = new CommonFunction(); return performTask(mapping, form, request, response); } public String doSomething() { return common.doSomething(); } } do this same for DispatchAction Hope this helps Ashish --- Glanville, Jay wrote: Hello all. My question might not be viewed as a Struts question, but more along the lines of an architecture question. But, on the other had, it is struts architecture, so I'm ok! ;-) This is the situation: some of our designers are creating subclasses of Action, while others are creating subclasses of DispatchAction. Now, there's some common functionality that needs to be executed by all subclasses of both Action and DispatchAction. The way that we've done it for Actions is by implementing execute in a base class and in it calling an abstract method that subclasses need to implement, like this: public class BaseAction extends Action { protected abstract ActionMapping internalExecute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ); public ActionMapping execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) { // ... Perform common work here return internalExecute( mapping, form, request, response ); } And, of course, for our base subclass of DispatchAction, we do something similar: public class BaseDispatchAction extends DispatchAction { public ActionMapping execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) { // ... Perform common pre-execution work here return super.execute( mapping, form, request, response ); } Plus, there are utility methods common to both BaseAction and BaseDispatchAction. Now, any half-decent coder's response to the phrase 'common functionality' is to think, is there a way that I can centralize this work, to reduce the repeated code? So, my question is this: what is the best way to add common functionality to subclasses of both Action and DispatchAction? As DispatchAction is a subclass of Action, the initial thought is to add it to Action, but that's un-necessarily messy. Another way that I've thought of is to create a 'common action functions' class, where I place implementations of all the functionality I might use, and then in my BaseAction and BaseDispatchAction, proxy/redirect to that CommonActionFunctions class. However, I try my best to write the best code possible, and this isn't as clean as I'd like it to be (I'm still duplicating method signatures, etc). What other ways have people found to deal with this situation? JDG -- Jay Glanville Web Developer [EMAIL PROTECTED] (613) 725-2030 x393 http://www.naturalconvergence.com - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] __ Do you Yahoo!? Yahoo! Search - Find what youre looking for faster http://search.yahoo.com - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - Do you Yahoo!? Yahoo! Search - Find what youre looking for faster.