views:

199

answers:

6

Hey everyone, I'm a college senior with my first real job opportunity (exciting). I'm at the stage now where they need to see a programming example, and they gave me the task of implementing a random number generator service in Java, with two different implementations (one using the built-in stuff and another of my choice). The code is the easy part, but one part of the task is confusing me... here it is:

As the evaluator, I should be able to do the following: Compile my own project with the candidates jar file. Register my solution with the candidates executable jar. Run the candidates executable jar, somehow telling it to run my implementation.

Basically I'm making my code into an executable .jar, and

evaluators should be able to use the code and compiled classes developed by the candidate to plug in their own random number generator implementation without recompiling the candidate’s code.

What the heck does that mean? Maybe I'm just missing something obvious? I'm not sure how to allow them to just throw in their own implementation without having to recompile everything... hopefully it's not too big of a task, as I haven't heard of something like that (I think) at my University.

Any help/insight is really appreciated!

+5  A: 

They want you to load the implementation JAR with an URLClassLoader (see the docs) and then use reflection to instantiate the main class and call the correct method to invoke the random number generator.

Aaron Digulla
+10  A: 

I think it just means that you should provide a RandomNumberGenerationStrategy interface as part of your public API, that the evaluator can implement.

Then provide another hook whereby he can register his particular implementation of your interface, which you then invoke via callback.

Kevin Pauli
A simple vm argument like -DRandomNumberStrategy=my.own.random.impl.MyGenerator will provide this functionality. You can use the value in your own code to instantiate and execute it.
Robin
@Robin Even better... use commons-discovery to instantiate and execute it.
Tim R
Thanks for the ideas, I'll definitely look into them tonight! Quick question/clarification: what exactly do you mean by "register"? Simply finding a way so he can tell the main calling class to use his implementation?As for the commons-discovery part, they want me to use only tools that are bundled with the JRE by default, so an interface might be the way to go...
Eric Jackson
You would provide a method on your main class such as RandomNumberGenerationService.setStrategy(RandomNumberGenerationStrategy)
Kevin Pauli
I was able to use the Interface concept and everything's working like they want (I hope). Thanks again everyone.
Eric Jackson
A: 

Look into makefiles.. They'll allow you to rebuild only certain portions of your program. I think they're asking you to make it flexible enough to do this.

http://www.cs.swarthmore.edu/~newhall/unixhelp/howto_makefiles.html

Jreeter
first thing, using make with Java is bad advice, ant is the defacto standard in the Java world. second thing, building the code has nothing to do with the question or the answer.
fuzzy lollipop
+1  A: 

Use Spring and leverage ClassPathXmlApplicationContext to swap your implementations via the Spring configuration. Your Spring config should look similar to this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"&gt;

<beans>
    <bean id="randonNumberGenerator"  class="com.me.MyGenerator"/>

    <!--
    <bean id="randonNumberGenerator"  class="com.someoneelse.ADifferentGenerator"/>
    -->
</beans>

In your program, load your Spring context and look up the bean.

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
IGenerator generator = applicationContext.getBean("randonNumberGenerator");

// IGenerator in the interface which MyGenerator and ADifferentGenerator implement

Remember your Spring configuration file should exist on your classpath (not just inside your jar) so it can be changed at runtime without a recompile.

Vinnie
Hmm, interesting. He mentioned how they love using the Spring framework in one of the interviews, so maybe I'll check this out, thanks.
Eric Jackson
A: 

Have a look at the OSGi framework which Eclipse uses. As an example, Eclipse is able to load new plug-ins and actively insert them into the running environment for immediate usage (apply without restarting). You could do exactly the same.

Chris Dennett
Mmm... they are asking for a simple thing, not an enterprise environment for hot swapping of services. As Kevin Pauli and Aaron suggest, Reflection and strategy pattern will suffice.
helios
A: 

Well maybe to make this more simple as possible, didn't they provide you with a name of the class and the method prototypes that you should be implementing? I think this one of the most logical and straightforward way to this... Like when our professor in school give us some assignment, and he requires that our programs should be able to run with the driver classes he provided, he usually sets the name of the classes that we should implement together with the prototypes of the methods... just a thought...

ultrajohn
Nope, they just said "implement a random number generator service" using two ways, one using the built-in Java tools (Random, SecureRandom, etc.) and one using an algorithm of my choice, and also allow it for the evaluator to add their implementation, so no names were given unfortunately.
Eric Jackson