4 Replies Latest reply: Feb 7, 2011 1:26 PM by jschellSomeoneStoleMyAlias RSS

    Builder pattern


      I am new to design patterns, but willing to learn. So I came here for an advice, as this would be my first attempt in implementing the builder pattern.
      The project: Create a java application that will generate and send via email 6 exports (Excel files) of different types, having their data filled from a database. The names of the exports, as well as other filters, will be read from an XML.
      My solution was to use the builder pattern, as it follows:

      public abstract class BuilderExport {
           protected Expor export;
           public Export getExport(){
                return export;
           public void createExport(){
                export = new Export();
           public abstract void createAntet();
           public abstract void createTableHead();
           public abstract void createTable();
           public abstract void setPath();
      public class BuilderExportImplementationA extends BuilderExport {
           public void createAntet() {
                // TODO Auto-generated method stub
                      *export.setSheet() and export.setWorkbook()*
           public void createTableHead() {
                // TODO Auto-generated method stub
                      *export.setSheet() and export.setWorkbook()*
           public void createTable() {
                // TODO Auto-generated method stub
                      *export.setSheet() and export.setWorkbook()*
           public void setPath() {
                // TODO Auto-generated method stub
      public class DirectorExport {
           private BuilderExport builder;
           public DirectorExport (BuilderExport b){
                this.builder= b;
           public Export getExport(){
                return builder.getExport();
           public void createExport(){
      public class Export {
           private Workbook workbook;
           private Sheet sheet;
              private String path;
              public Export(){
                       workbook = new HSSFWorkbook();
           public void setPath(String path) {
                this.path = path;
           public String getPath() {
                return path;
           public Workbook getWorkbook() {
                return workbook;
           public void setWorkbook(Workbook workbook) {
                this.workbook = workbook;
           public Sheet getSheet() {
                if (sheet == null)
                     sheet = workbook.createSheet();
                return sheet;
           public void setSheet(Sheet sheet) {
                this.sheet = sheet;
              public static void main(String[] args) {
                BuilderExport builder = null;
                List<ExportFilters> listFilters;
                DirectorExport director;
                for (ExportFilters fe : listFilters) {
                     if (fe.getType().equals("A")) {
                          builder = new BuilderExportImplementationA ();
                     } else if (fe.getType().equals("B")) {
                     director= new DirectorExport (builder );
      Can someone please tell me if this design is well thought? I would appreciate any help you can give me.

      An other question: Keeping in mind that the XML with the requested filters is received as a param in the command line, how can I have access to the filters in the ConcreteBuilder classes? it is a good design to pass it as an argument to the constructor, like
      new BuilderExportImplementationA (ExportFilters fe)
      Also, the methods saveExport(String path) and sendExport(..) to which object will they belong?


      ps: Please excuse my grammar, as english is not my native language.

      Edited by: user13806367 on Jan 21, 2011 2:19 AM
        • 1. Re: Builder pattern
          The Builder pattern exists to allow a user (code that uses the pattern) to select from a palette of pieces and use those to construct a whole. There should be an expected variation in how the pieces are used.

          You are not doing that because you are not varying the usage.

          From your description the only real pattern might be Factory although I suspect that is a stretch.

          As for the 'filters' you don't explain what they do. But presuming they are used to query the data from the database then there is a way to build expressions but I wouldn't suggest using it unless the expressions are complicated. Doing so for simple expressions makes it difficult to debug.
          • 2. Re: Builder pattern
            As Jschell has mentioned, builder patterns are used to segregate the responsibilities in construction of an object when it turns out to have enormous number of parameters.
            Say, you would have an employee object having Address as one of the components, phone numbers as another component and like wise. (Builder Pattern is recommended whenever you have Object composition in your class)
            Then, in such a case, builder would ease building these objects individually and finally merging it to one.
            • 3. Re: Builder pattern
              Thank you both for your answers.

              Let me see if I understood well how this pattern works. So, if for example my Export class would be
              public class Export {
                   private Antet antet;
                   private TableBody body;
                   private String location;
              and I would use the builders only to construct Antet, TableBody and location, but the actual construction of the excel file would be done in the Export class, then the use of the builder pattern would be justified? This is just a scenario.
              As for the 'filters', i'm sorry for not explaining what they're all about, but as Jschell well presumed they are used to query the data from database. The problem here is that for each export I would have to construct a query from different tables, using different ordering. There are 5 or 6 tables that are always present in the query, but there are other 4 or 5 tables that may vary. The filters are read from an xml file, which will look something like that:
                   <export type="km">
              Could you tell me what is the best way to construct those querys?

              Thanks again.
              Best regards.
              • 4. Re: Builder pattern
                but there are other 4 or 5 tables that may vary.
                You can't just throw a random set of tables into a query. One of the following would need to be true.
                1.There is a static relationship between all of the tables
                2. The filter applies to a specific part of an exist report. Thus the relationships are coded, the only variation is the data.
                3. The report is built in pieces where is section represents on table.
                4. Your configuration xml is not complete. It must specify complex relationships, in some form, which can be consumed to create the actual DDL relationships.

                If 2 then the filter providers an identifier. That maps to the specific SQL. Then the data is just plugged in to the SQL statement.