Persistent Object Basics

 <  Day Day Up  >  

Recall that when an object is instantiated by an application, it lives only as long as the application itself. Thus, if you instantiate an Employee object that contains attributes such as name , ss# , and so on, that Employee object will cease to exist when the application terminates. Figure 11.1 illustrates the traditional object life cycle which is pretty straightforward. When an application creates an object, an object lives within the confines of that object. When the application ends, the object goes out of scope. For the object to live on, it usually must be written to some sort of persistent storage.

Figure 11.1. Object life cycle.

graphics/11fig01.gif

When the Employee object is instantiated and initialized , it has a specific state. Remember that the state of an object is defined by the value of it attributes. If we want to save the state of the Employee object, we must take some sort of action to save the state of this object. The concept of saving the state of an object so that it can be used later is called persistence . Thus, we used the term persistent object to define an object that can be restored and used independent of the application. Figure 11.2 illustrates the traditional object life cycle with persistence. In this figure, the object is created in application 1, which then writes the object out to a storage device, perhaps a database. Because the object is in persistent storage, other applications can access it. In this figure, application 2 can now instantiate an object and load the contents of the persistent object.

Figure 11.2. Object life cycle with persistence.

graphics/11fig02.gif

There are many ways to save the state of an object. Some of these are as follows :

  • Save to a flat file

  • Save to a relational database

  • Save to an object database

The easiest way to demonstrate how to save an object is to create code that will write the object to a flat file, as most people do not have access to an object database or an industrial strength relational database on their home computer.

 <  Day Day Up  >  


Object-Oriented Thought Process
Object-Oriented Thought Process, The (3rd Edition)
ISBN: 0672330164
EAN: 2147483647
Year: 2003
Pages: 164
Authors: Matt Weisfeld

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net