Adding an implementation without recompiling in Java?

Hi everyone, I'm a high school student with my first real job opportunity (exciting). Now I am on the stage where they need to see an example of programming, and they gave me the task to implement the random number generator service in Java with two different implementations (one of which uses embedded material and the other of my choice), the code is the easy part, but one part of the task confuses me ... here it is:

As an evaluator, I should be able to do the following: Compile my own project using the candidates jar file. Register my decision with the help of a candidate executed by the bank. Launch an executable candidate bank, somehow telling her to carry out my implementation.

I basically make my code into an .jar executable and

evaluators should be able to use the code and compiled classes developed by the candidate to enable their own random number generation without recompiling the candidate code.

What does it mean? Maybe I just missed something obvious? I'm not sure how to let them just quit their own implementation without recompiling everything ... I hope this is not too big a task since I have not heard of anything like this (I think) at my university.

Any help / understanding is really appreciated!

+4
source share
5 answers

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

Then specify one more hook through which he will be able to register his specific implementation of your interface, which you then call through a callback.

+10
source

They want you to load the implementation JAR using the URLClassLoader ( see docs ), and then use reflection to create an instance of the main class and call the correct method to call the random number generator.

+5
source

Use Spring and use ClassPathXmlApplicationContext to replace your implementations through Spring configuration. Spring configuration should look something like 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 the Spring context and find 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 that your Spring configuration file must exist in your class path (not only inside your jar), so it can be changed at run time without recompiling.

+1
source

Take a look at the OSGi framework that Eclipse uses. As an example, Eclipse can download new plugins and actively insert them into the working environment for immediate use (apply without restarting). You can do the same.

0
source

Perhaps in order to make this as simple as possible, haven't they provided you with the name of the class and prototype methods that you should implement? I think that this is one of the most logical and simple ways for this ... For example, when our professor at school gives us some assignment, and he requires that our programs can work with the classes of drivers that he provided, he usually sets the name of the classes that we must implement with the prototype methods ... just a thought ...

0
source

Source: https://habr.com/ru/post/1303012/


All Articles