This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Using Different Packages based on input Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using Different Packages based on input" Watch "Using Different Packages based on input" New topic
Author

Using Different Packages based on input

Rudra Kumar
Greenhorn

Joined: Feb 15, 2002
Posts: 4
Hey all,
I have an application which needs to use two different sets of packages, based on the version of the input data. Both packages have files with same names and same function names, only the code inside the functions are different. Each package is required for parsing one version of input data.
How do I do this?
Thanks
Rudra
Sayed Ibrahim Hashimi
Ranch Hand

Joined: May 17, 2001
Posts: 148
I'm not sure how to exactly do this but it sounds like you are going to have to use the java.lang.reflect package. You should look into that.


SCJP 1.4<br /><a href="http://www.cise.ufl.edu/~sih" target="_blank" rel="nofollow">www.cise.ufl.edu/~sih</a>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

You can easily avoid the need for reflection (why use it if you don't have to?) by defining an interface that defines the common characteristics that both (or all) of those classes will implement.
Then once you decide which class you need to instantiate based upon your run-time criteria, you can use Class.forName().newInstance() to create the instance and assign it to a variable declared as the interface.
The code referencing this variable won't care which class it's actually dealing with since they will all obey the contract of the interface.
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rudra Kumar
Greenhorn

Joined: Feb 15, 2002
Posts: 4
Hi Bear,
Thanks for the reply.
Both the packages I am referring to, have more than 40 classes in each of them & they are generated by an automated tool based on the format of the user data.
The latest version of data has more functionality, hence more classes are there with more functions than the old classes. So, these packages have some classes in common with some functions inside them common(not the whole class is common).
How do i go about dealing with this?
Thanks
Rudra.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Hard to tell without knowing more about the project, but you should probably take a look at the different creational Design Patterns (at http://c2.com/cgi/wiki?CreationalPatterns for example).


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

Rudra, regardless of the scale, the general solution to the problem of having multiple classes represent the same set of functionality is through the uses of interfaces.
Taking Ilja's advice on researching creation patterns would be a great thing for you to do as you might find descriptions of solutions that match the needs of your project.
hth,
bear
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Different Packages based on input
 
Similar Threads
Best practice for persistence unit
wsconsume - how to split wsdl into packages
Transactions and Threading
multiple jar creation for multiple directories
Design single method with different input type and different data type