mardi 8 décembre 2015

How to separate the construction in its presentation in builder pattern?

I have this program that I think it represents a builder pattern, but I have this concern that I don't understand the separate construction in its representation from the GofF.

And here is the codes,

public class IDMaker {
public static void main(String[] args) {
   ...
    IDDirector director = new IDDirector();
    IDBuilder idB;
    switch(role){
        case 'a': {
                    idB = new StudentIDBuilder(idnumber, fname, lname);
                    break;
                }
        case 'b': {
                    idB = new FacultyIDBuilder(idnumber, fname, lname);
                    break;
                }
        default: {
                    idB = new StudentIDBuilder(idnumber, fname, lname);
                    break;
                }
    }
    director.MakeID(idB);
    ID id = idB.getID();
    ...
  }
}

This is the Director class of the program.

public class IDDirector {
public void MakeID(IDBuilder idB){
    idB.addIDNumber();
    idB.addName();
    idB.addWidth();
    idB.addHeight();
}
 }

This is the abstract builder of the program.

abstract class IDBuilder{
    public abstract void addIDNumber();
    public abstract void addName();
    public abstract void addWidth();
    public abstract void addHeight();
    public abstract ID getID();
}

This is the one of the concrete builder of the program.

class StudentIDBuilder extends IDBuilder{
    String idnumber;
    String fname;
    String lname;
    int width = 180;
    int height = 300;
    StudentIDBuilder(String idnumber, String fname,String lname){
        this.idnumber=idnumber;
        this.fname=fname;
        this.lname=lname;
    }

    private ID id = new ID();
    public void addIDNumber(){id.idnumber = this.idnumber;}
    public void addName(){id.fname = this.fname;id.lname = this.lname;}
    public void addWidth(){id.width = this.width;}
    public void addHeight(){id.height = this.height;}
    public ID getID(){return id;}
}

And this is the product of the program.

class ID{
    public String idnumber;
    public String fname;
    public String lname;
    public int width;
    public int height;
    public String getIDNumber(){return this.idnumber;}
    public String getName(){return this.lname + ", " + fname;}
    public String getSize(){return width + "x" + height;}
}

Is the builder pattern used correctly in this program?

Aucun commentaire:

Enregistrer un commentaire