File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes structuring common method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "structuring common method " Watch "structuring common method " New topic

structuring common method

D Preethi
Ranch Hand

Joined: May 24, 2008
Posts: 68
Hi All,

I have the following scenario:

There are classes "B","C","D" which are extending class "A". I do not have access to class "A" here. Now, I need to introduce a method "m" which can be commonly used by "B", "C" and "D". Can anyone suggest me some design pattern which can help me achieve this without having to duplicate the method across "B", "C " and "D" ?

Nitesh Kant

Joined: Feb 25, 2007
Posts: 1638

Have a look at the Visitor pattern.

apigee, a better way to API!
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Or create a class ExtendedA which implements that method and let B, C and D extend ExtendedA.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Rob Spoor

Joined: Oct 27, 2005
Posts: 19914

Or create an interface with method m and let those three classes implement that interface. You'll need to use references of that interface type to call the method.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: structuring common method