Encapsulating Java.util.Properties by accessor method is considered a bad practice?

I am using java.util.Properties to deal with my ongoing project. I have a class that uses Properties instances to manage .properties files, named PropertiesManager. This type of management is loaded from the CD And save the .properties file.

Now, because I want to make sure that only valid properties can be accessed, and as long as the properties are not in the file, the default value will be used, I add for each property of the file Getter and setter.

The problem is to make the class PropertiesManager very large. Separate getter and setter (with comments/blank lines) are 300 lines of code. So even if I transfer the load/save to another class (Inheritance etc.), it is still big.

This is not the actual code, but it gives you the idea:

import java.util. Properties;

public class PropertiesManager {
private Properties properties;

public PropertiesManager() {
// constructor
}

private void save() {
// save in .properties file
}

private void load() {
// load from. properties file
}

public String getPropertyName1() {
return properties.getProperty("PropertyName1", "Property1DefaultValue");
}

// 28 more getters here

public String getPropertyName30() {
re turn properties.getProperty("PropertyName30", "Property30DefaultValue");
}

public void setPropertyName1(String value) {
properties.setProperty("PropertyName1", value);
}

// 28 more more setters here

public void setPropertyName30(String value) {
properties.setProperty("PropertyName30", value) ;
}
}

Is it considered bad practice to encapsulate access to Properties instances in this way? Should I use the Properties instance directly instead of using the accessor method? Are there other solutions?

I just changed it to a single getter/setter using enum:

public class PropertiesManager {
...
public String getProperty(EnumProp enumProp) {
return properties.getProperty(enumProp.getKey(), enumProp.getDefaultValue());
}

public void setProperty(EnumProp enumProp, String value) {
properties.setProperty(enumProp.getKey(), value);
}
}

With this enumeration:

public enum EnumProp {
PROP_1("key", " defaultValue"),
...

EnumProp(String key, String defaultValue){
this.key = key;
this.defaultValue = defaultValue;
}
}

I am using java.util.Properties to handle my ongoing project. I have a project that uses the Properties instance to manage the .properties file Class, named PropertiesManager. This type of management loads and saves .properties files from the CD.

Now, because I want to make sure that only valid properties can be accessed, and as long as the properties are not in the file, it will Using the default values, I added getters and setters for each property of the file.

The problem is to make the class PropertiesManager very large. Separately The getter and setter (with comments/blank lines) are 300 lines of code. So even if I transfer the load/save to another class (inheritance etc.), it is still big.

This is not actual The code, but it gives you the idea:

import java.util.Properties;

public class PropertiesManager {
private Properties properties ;

public PropertiesManager() {
// constructor
}

private void save() {
// save in .properties file
}

private void load() {
// load from .properties file
}

public String getPropertyName1() {
return properties.getProperty("PropertyName1", "Property1DefaultValue");
}

// 28 more getters here

public String getPropertyName30() {
return properties.getProperty("PropertyName30", "Property30DefaultValue");
}

public void setPropertyName1(String value) {
properties.setProperty("PropertyName1 ", value);
}

// 28 more more setters here
public void setPropertyName30(String value) {
properties.setProperty("PropertyName30", value);
}
}

Encapsulate properties in this way Is access to the instance considered bad practice? Should I use the Properties instance directly instead of using the accessor method? Are there other solutions?

I just changed it to a single getter/setter using enum:

 public class PropertiesManager {
...
public String getProperty(EnumProp enumProp) {
return properties.getProperty(enumProp.getKey(), enumProp.getDefaultValue());
}

public void setProperty(EnumProp enumProp, String value) {
properties.setProperty(enumProp.getKey(), value);
}
}

With this enumeration:

public enum EnumProp {
PROP_1("key", "defaultValue"),
...< br />
EnumProp(String key, String defaultValue){
this.key = key;
this.defaultValue = defaultValue;
}
}

Leave a Comment

Your email address will not be published.