How to manage/maintain the constant data in your applications?

Prabhu Kumar
2 min readNov 29, 2020

--

Objects in an application usually make use of different types of data in offering the required business functionality. That data can be either variable data or constant data.

Instead of keeping the constant data in different objects the Constant Data Manager pattern recommends all such data to be kept in a separate object and accessed by other objects in the application. This type of separation provides an easy to maintain, centralized repository for the constant data in an application.

Let us consider a Customer Data Management application that makes use of three types of objects — Account, Address and CreditCard — to represent different parts of the customer data. Each of these objects makes use of different items of constant data as part of offering the services it is designed for as shown below.

public class Account {
public static final String ACCOUNT_DATA_FILE = “ACCOUNT.TXT”;
public static final int VALID_MIN_LNAME_LEN = 2;
public void save() {
}
}

public class Address {
public static final String ADDRESS_DATA_FILE = “ADDRESS.TXT”;
public static final int VALID_ST_LEN = 2;
public static final String VALID_ZIP_CHARS = “0123456789”;
public static final String DEFAULT_COUNTRY = “USA”;
public void save() {
}
}

public class CreditCard {
public static final String CC_DATA_FILE = “CC.TXT”;
public static final String VALID_CC_CHARS = “0123456789”;
public static final String MASTER = “MASTER”;
public static final String VISA = “VISA”;
public static final String DISCOVER = “DISCOVER”;
public void save() {
}
}

Create a constant data manager class and move all the constants from the above classes into this class as shown below. I have explicitly made this class as final and constructor as private so that callers will never create a ConstantDataManager object.

public final class ConstantDataManager {
public static final String ACCOUNT_DATA_FILE = “ACCOUNT.TXT”;
public static final int VALID_MIN_LNAME_LEN = 2;
public static final String ADDRESS_DATA_FILE = “ADDRESS.TXT”;
public static final int VALID_ST_LEN = 2;
public static final String VALID_ZIP_CHARS = “0123456789”;
public static final String DEFAULT_COUNTRY = “USA”;
public static final String CC_DATA_FILE = “CC.TXT”;
public static final String VALID_CC_CHARS = “0123456789”;
public static final String MASTER = “MASTER”;
public static final String VISA = “VISA”;
public static final String DISCOVER = “DISCOVER”;
private ConstantDataManager() {
}
}

Now the — Account, Address and CreditCard — classes looks like as shown below.

public class Account {
public void save() {
}
}
public class Address {
public void save() {
}
}
public class CreditCard {
public void save() {
}
}

In the above classes, from the save() methods, you can refer the constants from ConstantDataManager using the static references such as ConstantDataManager.ACCOUNT_DATA_FILE
ConstantDataManager.DEFAULT_COUNTRY and so on.

--

--