Hi, I want to write 2 classes , one class say MyAction extends Action and the other class say MyDispatchAction extends DispatchAction. I want to have some code ( like setting some parameters or getting connection from pool) handled in both the classes, but i want to write it in such a way that i dont have to code all the common methods in both classes. This way i would be reducing my coding and future maintenance, since if there is any modification i can do it at only one place and will be applicable to both places Any suggestions are welcome
It sounds like you want some kind of helper class where you factor out the common functionality. The other class wouldn't be an action, maybe as a utility class (a non-object-oriented class that exists only to share implementation artifacts, eg. java.lang.Math). One caution: it would definitely be a bad thing to have methods in MyAction call methods in MyDispatchAction just for convenience (or vice versa). MVC really requires a strong discipline of keeping view implementations as independent as possible. Every presentation layer I've seen that violated that ended up being thrown out and re-written.
Reid - SCJP2 (April 2002)
Joined: Aug 15, 2002
Hi, When i was thinking about my problem, I think what i want to do in multiple inhereteance I want to define a class called CommonFunctions , this call will have methods which are common, (As name suggest) now i have to do some thing like public class MyAction extends Action, CommonFuntions and public class MyDisptachAction extends DispatchAction, CommonFuntions By doing this i will be able to access all the methods in the class which sybclasses MyAction or MyDispatchAction I know the code above is not possible to do, but i want to do some thing like that.. So what are the ways of achieving it
Reid M. Pinchback
Joined: Jan 25, 2002
Since inheritance = composition + language-specific sugar coating, that would be the solution to your problem. Either make CommonFunctions static, as I suggested previously, or compose a reference to an instance of it. The composition approach is better if you'll be subclassing CommonFunctions to change the behaviour of the methods - you could instantiate whichever version you want for the composition.