views:

58

answers:

2

I need to build a 'MyThingie' using 'A'. MyThingie is in the model package and currently no code in model accesses the DB. My question is which of the following patterns should I use? The top or bottom? Or something completely different.

package com.model;

public class MyThingie {
    private String foo = "";
    private String bar = "";
    private X x = null;
    private Y y = null;
    private Z z = null;

    public MyThingie() {
    }

    public MyThingie(A a, X x, Y y, Z z) {
        this.foo = a.getFoo();
        this.bar = a.getBar();
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public static MyThingie createFromDb(A a) { 
        X x = fetchXFromDB(a.getBlah()); 
        Y y = fetchYFromDB(a.getFlah()); 
        Z z = fetchZFromDb(a.getZlah()); 

        return new MyThingie(a, x, y, z); 
    }

    // getters and setters
}

// ----------- OR----------------

package com.model;

public class MyThingie {
    private String foo = "";
    private String bar = "";
    private X x = null;
    private Y y = null;
    private Z z = null;

    public MyThingie() {
    }

    // getters and setters
}

package com.builder;

public class MyThingieBuilder {
    public MyThingieBuilder() {
    }

    public static MyThingie createFromDb(A a) { 
        MyThingie m = new MyThingie();

        m.setFoo(a.getFoo());
        m.setBar(a.getBar());

        X x = fetchXFromDB(a.getBlah()); 
        Y y = fetchYFromDB(a.getFlah()); 
        Z z = fetchZFromDb(a.getZlah()); 

        m.setX(x);
        m.setY(y);
        m.setZ(z);

        return m;
    }
}
+1  A: 

Both solutions are Ok. But there is a 'builder' methodology/pattern that you might find useful.

Following my posted comment, i present an example of the Builder as an inner class

Widget x = new Widget.Builder("1", 1.0).
                 model("1").build();
Widget y = new Widget.Builder("2", 2.0).
                 model("2").manufacturer("222").
                 serialNumber("12345").build();
Widget z = new Widget.Builder("3", 4.0).  
                 manufacturer("333").
                 serialNumber("54321").build();

The basic idea behind the pattern is to limit the number of constructor parameters and avoid the use of setter methods. Constructors with too many parameters, especially optional ones, are ugly and hard to use. Multiple constructors for different modes are confusing. Setter methods add clutter and force an object to be mutable. Here is an class skeleton of the pattern -

public class Widget {
 public static class Builder {
 public Builder(String name, double price) { ... }
 public Widget build() { ... }
 public Builder manufacturer(String value) { ... }
 public Builder serialNumber(String value) { ... }
 public Builder model(String value) { ... }
}

private Widget(Builder builder) { ... }

}

andreas
+1  A: 

Do not sprinkle Data Access logic in your models. Instead consider using a DAO pattern. Let your business object( typically a service class) use this DAO object to obtain or modify your model. Have a look at this Sample.

chedine