Is it bad practice to use Consumers as setters and Suppliers as getters in Java?
<p>I have a Java class that has some private variable that I don't intend to create setters and getters for; I want these variables to remain inaccessible. But there is one class that needs access to these variables. This class is a visitor in a different package (and I'd prefer to keep it in a different package). Is it bad practice to allow this class to provide the visitor with Consumers and Suppliers, that act as setters and getters, so that the visitor could read and modify these variables? If yes, please state the reasons.</p>

<p>Example:</p>

<p>A.java</p>

<pre><code>public class A {
    private int x;
    private Consumer&lt;Integer&gt; setter;
    private Supplier&lt;Integer&gt; getter;
    public A(int v) {
        x = v;
        setter = new Consumer&lt;Integer&gt;() {
            @Override
            public void accept(Integer t) {
                x = t;
            }
        };

        getter = new Supplier&lt;Integer&gt;() {
            @Override
            public Integer get() {
                return x;
            }
        };
    }

    public void accept(SomeVisitor visitor) {
        visitor.setSetter(setter);
        visitor.setGetter(getter);
        visitor.visit(this);
    }
}
</code></pre>

<p>SomeVisitor.java</p>

<pre><code>public class SomeVisitor extends ParentVisitor {
    private Consumer&lt;Integer&gt; setter;
    private Supplier&lt;Integer&gt; getter;

    public SomeVisitor() {
        setter = null;
        getter = null;
    }

    public void setSetter(Consumer&lt;Integer&gt; setter) {
        this.setter = setter;
    }

    public void setGetter(Supplier&lt;Integer&gt; getter) {
        this.getter = getter;
    }

    @Override
    public void visit(A a) {
        // Code that will, possibly, read and modify A.x
        ...
    }
}
</code></pre>

<p>This way the variable A.x remains inaccessible to every class except the visitor.</p>

<hr>

<h2>More Details:</h2>

<p>I have some classes that will make use of the visitors. These classes have private variables that are dependent on one another. If these variables had setters, inconsistencies could arise as users change these variables, that should be dependent on one another, without respecting these dependecies.</p>

<p>Some of these variables will have getters, others won't as they will only be used internally and shouldn't be accessed elsewhere. The reason the visitors are an exception and should get read/write access to these variables is that the functionality the visitors are intended to implement were meant to be implemented within methods in these classes. But I thought it will be cleaner if I used visitors. And these functionalities do need read/write access to these variables.</p>

<p>The intention behind this approach was to emulate the friend feature in C++. I could place the visitors within the same package as these classes (which I would do if I didn't find a neat solution to this problem); But I think the package will look messy if it had the visitors as well (and there will be many visitors).</p>

<hr>

<p>The functionality the visitors will implement will also have something to do with these classes relations to one another.</p>