Chapter 4. Manipulating objects
This chapter covers
- Making a distinction between mutable and immutable objects
- Using modifier methods to change state or create modified copies
- Comparing objects
- Protecting against invalid state changes
- Using events to track changes in mutable objects
As you’ve learned in the previous chapters, services should be designed to be immutable. This means that once a service object has been created, it can never be modified. The biggest advantage is that its behavior will be predictable, and it can be reused to perform the same task using different input.
So we know that services should be immutable objects, but what about the other types of objects: entities, value objects, and data transfer objects?
Entities are the application’s core objects. They represent important concepts from the business domain, like a reservation, an order, an invoice, a product, a customer, etc. They model knowledge that developers have gained about that business domain. An entity holds the relevant data, it may offer ways to manipulate that data, and it may expose some useful information based on that data. An example of an entity is the following SalesInvoice class.