File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes one design question, how do it do it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "one design question, how do it do it" Watch "one design question, how do it do it" New topic
Author

one design question, how do it do it

ashish kulkarni
Ranch Hand

Joined: Aug 15, 2002
Posts: 130
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


A$HI$H
Reid M. Pinchback
Ranch Hand

Joined: Jan 25, 2002
Posts: 775
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)
ashish kulkarni
Ranch Hand

Joined: Aug 15, 2002
Posts: 130
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
Ranch Hand

Joined: Jan 25, 2002
Posts: 775
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: one design question, how do it do it