7 Reducing complexity of state

 

This chapter covers

    • Making entities partially immutable
    • Using entity state objects
    • Looking at entities through entity snapshots
    • Modeling changes as a relay of entities

    If mutable state isn’t handled properly, bad things happen. For example, a flight taking off with a bag in the hold belonging to a passenger who never showed up for boarding might be a security risk. But keeping the state of entities controlled becomes hard when entities become complex, especially when there are lots of states with complex transitions between them. We need patterns to deal with this complexity, to reduce it in a manageable way.

    On top of the problems with complex mutable states, entities are also hard to code. This is because they represent data with a long lifespan, during which they are changed. The problem occurs if two different agents try to change the same entity at the same time. Technically, this boils down to two threads trying to change the same object simultaneously. The patterns you use to control complexity need to handle this situation as well. We’ll look more closely at this to distinguish between single-threaded environments, such as the inside of an EJB container, and multithreaded environments.

    7.1 Partially immutable entities

    7.2 Entity state objects

    7.2.1 Upholding entity state rules

    7.2.2 Implementing entity state as a separate object

    7.3 Entity snapshots

    7.3.1 Entities represented with immutable objects

    7.3.2 Changing the state of the underlying entity

    7.3.3 When to use snapshots

    7.4 Entity relay

    7.4.1 Splitting the state graph into phases

    7.4.2 When to form an entity relay

    Summary