How do you reproduce the behavior of const-pointers in Java?

124 Views Asked by At

I have a singleton class containing a bunch of control data that needs to be kept synchronized with the rest of my application. As a result, there are many times which I want another class to be able to read the information but not modify it. Currently, this singleton class has many public variables. I don't want to use getter and setter functions because they are wordy and annoying. Also, there are a lot of variables.

If my singleton class is called ControlData, I could create a second create a second class called ImmutableControlData, where it has all the same members, but they are declared final. Then, when retrieving my singleton, I would return an ImmutableControlData, rather than a ControlData object. However, this means that I need to constantly maintain the ImmutableControlData class as well as the ControlData class (annoying...)

If I had const-pointers, I would just return a const-pointer to my ControlData object. What can I do in Java, instead?

3

There are 3 best solutions below

1
On

Java does not have const correctness like C++.

You could make an interface that declares the methods to read the data, but not the methods to modify the data. Make the class that holds the data implement this interface. Methods elsewhere in your program that should only read the data, should accept the interface, not the class, as the parameter type. For example:

public interface ReadablePerson {
    String getName();
}

public class Person implements ReadablePerson {
    private String name;

    @Override
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

// Elsewhere...
public void someMethod(ReadablePerson p) {
    System.out.println(p.getName());
}

Ofcourse, in someMethod you could still subvert this by casting p to Person, but at least it requires some conscious effort (adding the cast), which should alert the programmer that (s)he is doing something (s)he shouldn't do.

An advantage of this solution is that you don't have to make a defensive copy of the data.

0
On

First: If your Class has so many members, you should try to split the class into littler ones. Maybe you can summerize some variables eg.

ControlflowVariables StateVariables

If you want to restrict the access to the variables you have to use getter. IDE can create getters and setters for you. The access to variables are the same:

singletonClass.variable is not worst then singletonClass.getVariable()

If you want to restrict the access only at some points in your code then create a final copy of tha variable

final int variable = singletonClass.getInstance().variable;
0
On

Personally, I would not try to control access in this way. There is nothing you can to do to prevent bad programmers from misusing your class. Even in C++, they could use a simple const_cast to remove your "protection" and modify the singleton any way they like.

Instead, I would restructure the code to make it easy for others to do the right thing and hard for them to get it wrong. Segregate the interface for ControlData into two separate interfaces: one for reading the object and one for updating it. Then simply provide the two interfaces where they're needed.