-->

Monday, March 23, 2015

Java Beans and Serialization

A java bean encapsulates the scripting for setting and getting information from a database. You make them Serializable (aka, savable). They help you manage rows of data to update and delete as you adapt your management class to various data structures. Beans are rather simple to correct and modify.

Videos that demonstrate the concept can be shown here.

<iframe width="560" height="315" src="https://www.youtube.com/embed/eMMLaneVkjc" frameborder="0" allowfullscreen></iframe>

JavaBeans are among the prime features of frameworks such as Apache Spring or Java Database Connectivity (JDBC).  An extension of Factory Patterns, they help free developers from relying on too many class dependencies by encapsulating the code that creates objects. This greatly assists maintenance, as per the Dependency Inversion Principle.



Dependency Inversion Principle - depend on abstractions, not concrete classes.

To speak more of Serialization, the concept here is to safe the instance variables (aka the "state") of your class objects to a FileOutputStream (foo for short). Such methods are risky, so you may want to put them into a try-catch block.
If there is an instance variable you don't want saved, mark it as transient.  Among the conveniences of Serialization is that the entire object graph is Serialized. That means any object values referenced by the class is also saved.



For example, say you have a Car Class and it references classes for Engines and Wheels. What you have with these various pointers within the Car Class is a graph, and Serialize will save all this for you.



Serialization can be more convenient then other paradigms for saving information (such as a Comma Separated Value document), but Serialization is limited to Java Applications only. If such information must be cross compatible among programming languages, you wish need to use a FileWriter to save a String that contains the information you wish to save.


~Code Crunch Corner~

No comments:

Post a Comment