Immutability

When a client changes the state of an object, the state changes for every client that has access to the object. This is no problem when there is only a single client, which is the most ordinary case. When multiple clients will share access to an object, the easiest and most common way to keep clients from affecting one another is to restrict clients from introducing any state changes in the shared object. You can achieve this by making an object immutable so that once created, the object cannot change. The most common immutable objects in Java are instances of the String class. Once you create a string, neither you nor any client with access to the string can change its characters.

CHALLENGE 13.2Either justify why the creators ...

Get Design Patterns Java™ Workbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.