A Funny Java Flavoured Look at the World

Friday, July 14, 2006

Where do you put the class creating code?

I don't know what you call it but I had an interesting programming dilema

I was writing some code which read in a text file and in this file it had six sets of data, with a name, date and then an int array. I decided to create a POJO (plain old java object) to hold the data.

The next question I had was where to put the code that split up the data. I had got it to a point where I had read in the file and put it into a String, then Tokenized the string into an array of strings, each block had the data to create the data bean.

I had created a DataBeanFactory in case I need to do something like this in the future but I was wondering if I should put the code which takes a String object and converts it into the correct variables either

1. In the DataBeanFactory
2. Or in the DataBean

Did I want to keep the DataBean purely as a data object and don't add in any creation code. I was thinking about the coding advice

"only give an object one reason to change"

Also Reusablability comes into play, if I were to put creation code in the DataBean it might reduce it reusability because it was totally dependent on getting this one set of data.

The other side of argument I felt was where better to put the creation of the DataBean but inside of the DataBean thus allowing the object to know how to create itself. It would also reduce dependency and coupling for the DataBean which I suppose would make it more reusable. It would allow the DataBeanFactory to just concentrate on working out which DataBean people want created.

In the end I put the code inside of the DataBean as it seemed the neatest place for it. Putting it inside the Factory class would have made it too messy. It struck me that there wasn't any guidelines on such things or I will correct myself any guidelines or articles that I have read. I suppose this is because you have to weigh up the setting of the bean etc and how you are going to initialize it, some times you might slowly fill up a POJO other times it might be in the constructor.

I wonder if anyone had any rules on the subject, when should you put self creating code inside a DataBean.

4 Comments:

  • Try a dymamic language like Lisp or Ruby someday. They encourage you to break the traditional rules, so you can do what *you* think is good, instead of some "experts" who don't even know what the problem you're trying to solve is.

    For example, this Ruby code would probably solve your problem:


    class Item < Struct.new(:name, :date, :ints)
    def initialize(name, date, ints)
    super(name, Date.parse(date), ints.split(' ').map{|i| i.to_i})
    end
    end

    result = File.open('data.txt', 'r').read.split("\n\n").map{|item|
    Item.new(*item.split("\n").map{|attr| attr[6..-1]})}

    result contains this array:

    [#(struct Item
    name="foo",
    date=#(Date: 4889107/2,0,2299161),
    ints=[4, 3, 2, 14, 5, 4]),
    #(struct Item
    name="bar",
    date=#(Date: 4908549/2,0,2299161),
    ints=[5, 4, 2, 23])]

    If you feed it this file:

    name: foo
    date: 10-11-1980
    ints: 4 3 2 14 5 4

    name: bar
    date: 23-06-2007
    ints: 5 4 2 23

    By Anonymous Anonymous, at Fri Jul 14, 06:45:00 pm 2006  

  • Can you show me the way you put the code to create DataBean inside DataBean? Did you use static factory method?

    By Blogger t800t8, at Sat Jul 22, 02:18:00 pm 2006  

  • I basically just made a method inside of the DataBean which converted a String into a couple of dates and an Int array.

    I had a constructor which took one String variable and then the constructor calls the method that converts the String into the variables needed for the DataBean.

    The code isn't really that impressive it doesn't do that much but I was wondering where I should put the code that created the variables for the DataBean class.

    By Blogger The Hosk, at Sun Jul 23, 12:31:00 am 2006  

  • Okie, now I see the way you create a DataBean. But what's the guideline here? I can't see it. You can pass any string into the constructor (ofcourse, you can throw an exception if the string is not match) But you want to parse a text file, why don't give creating DataBean for a factory class (or at least, a static method)?

    In factory class, I will parse text file and return a collection of DataBean. In DataBean, I only have constructors with "true" parameters.

    class DataBean {

    private Date date;
    private int x;
    private int y;

    DataBean(Date date, int x, int y) {
    this.date = date;
    this.x = x;
    this.y = y;
    }

    }

    class DataBeanFactory {

    static ArrayList@lt;DataBean@gt; getBeans(String filename) {
    // ...
    }

    }

    (Sorry for my English, it's not good)

    By Blogger t800t8, at Sun Jul 23, 08:06:00 am 2006  

Post a Comment

<< Home