开发者

Adding implementation without recompiling in Java?

开发者 https://www.devze.com 2022-12-21 23:56 出处:网络
Hey everyone, I\'m a college senior with my first real job opportunity (exciting). I\'m at the stage now where t开发者_如何学运维hey need to see a programming example, and they gave me the task of imp

Hey everyone, I'm a college senior with my first real job opportunity (exciting). I'm at the stage now where t开发者_如何学运维hey 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!


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.


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.


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">

<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.


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.


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...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号