Chapter 7. From birth to death: object lifecycle
This chapter covers:
- Notifying objects of significant events
- Understanding domain-specific lifecycle
- Initializing lazy and eager singletons
- Customizing lifecycle with multicasting
“I agree with everything you say, but I would attack to the death your right to say it.”
Tom Stoppard
Whether or not lifecycle is a part of dependency injection is a divisive issue. However, like scoping, it can be a powerful tool when used correctly, and it fits closely with dependency injection. In this chapter we’ll look at the basic form of lifecycle offered by the language runtime—constructors. We’ll also look at managed lifecycle as offered by more elaborate frameworks like servlets and EJBs, to illustrate how lifecycle is domain specific. On the way we’ll examine the pitfalls of relying on onesize-fits-all lifecycle models.
Finally, we’ll look at how to design and implement a custom lifecycle strategy and design classes that are simple and easy to test. First, let’s start by looking at what lifecycle is: events in the life of an application that an object is notified about.