Skip persistent class as argument and save it

pseudo-users code

The provided fragments should be taken as psuedo-code. I am open if there is another solution that is the standard way to solve this problem.

About expected use:

Some explanations:

  • One and only one configuration will be used for each application. It will not be changed at runtime.
  • Main.java does not allow @Override .
  • Configuration.java cannot be Interface because default values ​​must be assigned to fields that are not overridden.
  • Configuration.java will grow substantially from its two current fields. Providing the builder pattern is very erratic to work with.

Configuration.java

 public class Configuration { public static int getFoo () { return 1; } public static int getBar () { return 2; } } 

UserDefinedConfiguration.java

 public class UserDefinedConfiguration extends Configuration { @Override public static int getFoo () { return 3; } } 

Main.java

 public final class Main { private final Configuration config; // default configuration public Main () { this (Configuration.class); } // user-defined configuration public Main (Class<? extends Configuration> config) { this.config = config; } // dummy-test public void printFoo () { System.out.println(config.getFoo()); } } 

Now, to the main question, how to do this? If not (or Configuration ), then getFoo() should return 1 if UserDefinedConfiguration is passed, then 3 .

One way to achieve this is to save an instance of Configuration . However, it feels superfluous when all the getters are static . It doesn't make sense to not have them as static .

Note: This is taken into account.

+7
java
source share
2 answers

If you play with a dirty reflection, I'm afraid you will have to work with instances instead of classes. From @JonSkeet :

Singleton allows you to access one created instance - this instance (or rather, a link to this instance) can be passed as a parameter to other methods and is treated as a regular object.

A static class allows only static methods.

This is exactly what you are trying to do: passing the configuration as a parameter.


I would create an abstract class defining default values:

 public abstract class Configuration { public int getFoo() { return 1; } public int getBar() { return 2; } } 

Then one singleton for a specific configuration:

 public final class DefaultConfiguration extends Configuration { public static final Configuration INSTANCE = new DefaultConfiguration(); private DefaultConfiguration() {} // nothing to override, use the default values } public final class UserDefinedConfiguration extends Configuration { public static final Configuration INSTANCE = new UserDefinedConfiguration(); private UserDefinedConfiguration() {} @Override public int getFoo() { return 3; } // specific `foo` value } 

Finally, in Main :

 public class Main { private final Configuration config; public Main() { this(DefaultConfiguration.INSTANCE); } public Main(Configuration config) { this.config = config; } } 

Also, note that Java 8 allows you to implement default methods in interfaces; Configuration can be an interface:

 public interface Configuration { default int getFoo() { return 1; } default int getBar() { return 2; } } 
+1
source share

Essentially, you need a type polymorphism, not an instance. In Java, this is usually done using generic types:

 class GenericMain<T extends Configuration> { private final T config; } 

and since Java does not allow common default arguments, you must define a different class to specify the default value:

 class DefaultMain extends GenericMain<Configuration> { } 

They map to each other with your Main () and Main (Class<? extends Configuration> config) constructors.


Alternatively, you can save an instance of Configuration and do something like this:

 public class Configuration { private final int foo = 1; private final int bar = 2; public final int getFoo () { return foo; } public final int getBar () { return bar; } public Configuration () {} protected Configuration (int foo) { this.foo = foo; } } public class UserDefinedConfiguration extends Configuration { public UserDefinedConfiguration() { super(3); } } 
0
source share

All Articles