• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using serializable to write everything to a file at once: why is it a bad practice?  RSS feed

 
bob joe
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a class, I'm making a prison system program. The prison has cell blocks, each cell block has cells, each cell has bed(s), and each bed has prisoners. There are also visitors and visits. I thought, rather than write all of those to different files, I could just write the entire prison to a file and make all the classes under it implement serializable. The instructor said that is a bad practice and we shouldn't do it. So I'm thinking I'll make the prison non-serializable. But the prison has maps of cell blocks, inmates, visitors, and visits. I think I'll make all those serializable and I'll also make everything a cell block contains serializable as well. Then I can just read the cellblocks from the file. But is that too easy as well? Is that a bad practice? Basically I don't understand why it would be bad to write the whole prison to the file so it's hard for me to understand what is acceptable/good practice. Thanks.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think you need serialization at all? I've been writing Java apps for over a decade and I don't remember ever having to write code that did serialization/deserialization of Java objects. For a program such as what you describe, the usual persistence mechanism is a database of some sort.
 
bob joe
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I'm mainly using serialization because the instructor said that we should use it for our final project. It also seems less tedious than using scanners and formatters. We haven't learned about using databases with java yet though.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob joe wrote:For a class, I'm making a prison system program. The prison has cell blocks... I could just write the entire prison to a file and make all the classes under it implement serializable.

It sounds like you've fallen into a trap that people learning OO often fall into. Not everything in the real world has to be modeled as an object in the program. Conversely, not every object in your program needs to have a corresponding real-world object. You would be hard pressed to find a Bank object in a real-world banking system.

Another trap you appear to have fallen into is getting too caught up with implementation details (like serialization) before you've figured out what kind things and behaviors you want your program to have. Think of high-level behaviors and abstractions first. Class-Responsibility-Collaborator models are a good place to start.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob joe wrote:I guess I'm mainly using serialization because the instructor said that we should use it for our final project.

I would wait until the instructor explains exactly how and for what you're going to use serialization. If you haven't learned about databases yet, then I would not worry about persisting data yet. Just implement the main functionality first, worry about persistence of data later. Serialization/deserialization is a low-level operation that most programmers, even professional ones, should leave to be encapsulated in libraries and utilities. At least that's what I do and I'm happy to do it.
 
bob joe
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prison is just what I'm calling the class that does most of the set up for the program like making the Jframes and Jpanels, holding the containers of other things, reading from and writing to files etc. This is what I have so far.

http://creately.com/diagram/i1timf8k1/tlTcFfW9C6CS8qbEbNAT4djBoOc%3D

You think I should get rid of the prison class? I did draw an ERD and draw out a couple screens and use cases, though I suppose I could do more.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob joe wrote:Prison is just what I'm calling the class that does most of the set up for the program like making the Jframes and Jpanels, holding the containers of other things etc. This is what I have so far.

You think I should get rid of the prison class?

If that's what you have your Prison class doing, that's fine. I don't think you need to serialize it though. However, you said earlier that the Prison class contains cell blocks and other things so it sounds like besides setting up JFrames and JPanels, it's also serving as the top node of an object graph. I wouldn't do that. Presentation is one responsibility that should be kept separate from other responsibilities. I don't know how big your app is going to get and it may be small enough to get away with having the Prison class the way it sounds like you have it but for larger applications, separation of concerns and layering of the application are important design principles to consider.
 
bob joe
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
bob joe wrote:I guess I'm mainly using serialization because the instructor said that we should use it for our final project.

I would wait until the instructor explains exactly how and for what you're going to use serialization. If you haven't learned about databases yet, then I would not worry about persisting data yet. Just implement the main functionality first, worry about persistence of data later. Serialization/deserialization is a low-level operation that most programmers, even professional ones, should leave to be encapsulated in libraries and utilities. At least that's what I do and I'm happy to do it.


Yeah, I hadn't thought about it but I'm guessing there's some pre-existing java class similar to this one from my book?
http://cs.stmarys.ca/~porter/csc/465/code/deitel/examples/ch14/fig14_20_21/ReadSequentialFile.java2html

If that's what you have your Prison class doing, that's fine. I don't think you need to serialize it though. However, you said earlier that the Prison class contains cell blocks and other things so it sounds like besides setting up JFrames and JPanels, it's also serving as the top node of an object graph. I wouldn't do that. Presentation is one responsibility that should be kept separate from other responsibilities. I don't know how big your app is going to get and it may be small enough to get away with having the Prison class the way it sounds like you have it but for larger applications, separation of concerns and layering of the application are important design principles to consider.


Good point... I might make another class called GUI or something to handle the JFrames, JPanels, etc. and have theGUI as an attribute of thePrison
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking through the Java programs at the link you gave, I have to say that I think whoever wrote those programs is out of touch with how most of Java applications are written today, at least the projects I've ever worked on. The programs I see there remind of how we used to write our Pascal programs 20+ years ago in the late 80s, using random access and other low-level file operations. I don't know anybody who does that for real-world applications. It's just too tedious. That's why you have JDBC and other persistence technologies.
 
bob joe
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, we really should be using a database and JDBC for this but since we haven't learned about databases yet I'll probably just go with the outdated approach in the book. (That program is from our book). Thanks for the insight.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!