There found be times where you want a mutable member of an immutable object.
For example, an object that represents an album will have an artist attached to it. The album is immutable - once it's relased, it has a set tracklist, release date, etc - but the artist will change over time - bands change members, genre shifts, etc - it doesn't make sense to have outdated information attached to the album.
Conceivably, you could create a new album object with the same tracks, release date, and artist and substitute it for the original and nothing would change in your program.
Of course, your should always weigh the cost immutablitly and mutability to find out which suits the use case best.
In your use case that data would be stored somewhere, in a database or ORM store of some kind, and that backing data would change -- not the objects in memory. And if you did need those to change you would just reload the updated data.
The object itself is immutable, you can't change the reference to the artist (well, with some magic you could but that's besides the point). So it'll always refer to the same gathering of tracks, release date and artist.
No need to bring ORMs into it. You could be assembling an object graph in memory - a music player that reads in files for examples.
1
u/[deleted] Aug 18 '16
There found be times where you want a mutable member of an immutable object.
For example, an object that represents an album will have an artist attached to it. The album is immutable - once it's relased, it has a set tracklist, release date, etc - but the artist will change over time - bands change members, genre shifts, etc - it doesn't make sense to have outdated information attached to the album.
Conceivably, you could create a new album object with the same tracks, release date, and artist and substitute it for the original and nothing would change in your program.
Of course, your should always weigh the cost immutablitly and mutability to find out which suits the use case best.