I have some closed component which is working over some XML object. I need to expose this object outside of this component, but I don't want to allow changes on it outside of my component. How can I complete this?
Ensure your object has a copy constructor that allows you to make a deeply-cloned copy of your class.
Then call this when you return the object, e.g.
SomeClass instance = // ....
public SomeClass getInstance() {
return new SomeClass(instance);
}
This won't make the returned object immutable. But it doesn't need to be - you just don't want the external code making changes to your copy of the data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With