Save User Data in a Text File or a Serialized Object

I need to save bookmarks for a program I am making. The bookmarks are Xposition, Yposition, and zoom. Should I just read and write all of the bookmarks to a text file, or should I put them into an Bookmarks object and Serialize it. I am interested in which one would be considered better OO design.

Answers


I'd create class Bookmark anyway. Then I'd create a interface BookmarkSerializer with to methods serialize() and deserialize(). Then I can create my first implementation of BookmarkSerializer. It for example may be based on native java serialization, plain text, csv, xml etc. So, I can choose one, implement it and then use it in my application.

When one implementation does not serve me enough I can add other. The point is that in this case I will not have to change anything in my application except initialization of the serializer itself.

I think that in your case you should start from the simplest CSV implementation and if it is not enough add as many implementations as you need.


i'll suggest saving to a text file with crossing platform. in fact it isn't an object oriented consideration since you can still programing in oo when you're using the text file approach, say, a data access object with text file source doesn't violate the oo concepts.


java serialization is fragile, so if your intention is to keep some config data for long time, which possibly can live across multiple versions of your program, then probably text file is your best bet.

format is really up to you, xml, json, csv

if you decided to use java serialization i would recommend to store your data into a HashMap

and wrap you accessor around the map

public class Config {

    private HashMap map; 
    Congif(HashMap map) { this.map = map; }

    <T> T get(Class<T> clazz, String key) {
        return (T) map.get(key);
    }

    void put(String key, Serializable value) {
        map.put(key, value);
    }
} 

Using Objects is usually the best OO design.

For ease of support, I use a text file as its easy to view (to check it right) and edit (to fix it)


This might answer your question.

I would recommend the text file to be XML or JSON format for easier cross system compatibility.

Serialization is great, but it gets tricky if you decide to ever change the object you're serializing.

If you decide on serializing make sure you define a SERIAL_VERSION_UID for your object. You can read about it in more detail here.


Need Your Help

WCF DataServices Astoria Version 2 not returning results when called from Code

c# wcf wcf-data-services

I have WCF DataService which works great when queried via directly through the browser. However, when run from Visual Studio 2010 where I'm trying to query and bind to a grid, it throws a

FairCom c-tree ODBC Connection - PHP

php odbc

I have a User DSN configured in Windows ODBC:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.