You can move two implementations Animalinto two separate projects. Each project may have its own dependencies. Then you can package both projects independently of each other (including the corresponding dependencies), and only one implementation Animalwill be known at runtime.
You might end up with something like this:
base-animals
├ com.wang.animals.base.Animal
└ com.wang.animals.base.AnimalFactory
dog-application
├ com.wang.animals.dogs.Dog
└ application.properties
cat-application
├ com.wang.animals.cats.Cat
└ application.properties
dog-application cat-application base-animals , , , . dog-application.jar cat-application.jar, / .
, Animal , AnimalFactory, Animal - ( ):
public class AnimalFactory {
public static Animal createAnimal() throws Exception {
final String animalClassName = getThisValueFromApplicationProperties();
return Class.forName(animalClassName).getConstructor().newInstance();
}
}
, Cat Dog , base-animals application.properties dog-application cat-application. , Cat Dog.