Hacker News new | ask | show | jobs
by vermasque 2911 days ago
Interfaces can also provide read-only references of a instance. Consider interface Foo with accessors only and a MutableFoo implementation with accessors and mutators. The creator of the MutableFoo instance can make all sorts of changes to the instance but then pass it around as a Foo that can't be modified. Another option is to simply put all the state in the constructor so that a Foo never needs to be mutated; however, that might not be elegant in some cases state is incrementally accrued.

I do acknowledge that XImpl is kinda ugly as name.

1 comments

> The creator of the MutableFoo instance can make all sorts of changes to the instance but then pass it around as a Foo that can't be modified.

Until you cast the object...

You should use truly immutable data types unless you specifically need otherwise, most modern languages and libraries encourage you to do so.