Example of the Builder pattern : Builder Pattern « Design Pattern « Java Tutorial






import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

class Office extends ArrayList<OfficeUnit> {
}

class Word extends Office {
}

class PPT extends Office {
}

class Excel extends Office {
}

class OfficeUnit {
  private String s;

  public OfficeUnit(String s) {
    this.s = s;
  }

  public String toString() {
    return s;
  }
}

class DocumentPage extends OfficeUnit {
  public DocumentPage(String s) {
    super(s);
  }
}

class PPTSlide extends OfficeUnit {
  public PPTSlide(String s) {
    super(s);
  }
}

class ExcelWorkbook extends OfficeUnit {
  public ExcelWorkbook(String s) {
    super(s);
  }
}

class OfficeBuilder {
  public void buildBase() {
  }

  public void addOfficeUnit(OfficeUnit item) {
  }

  public Office getFinishedMedia() {
    return null;
  }
}

class WordBuilder extends OfficeBuilder {
  private Word b;

  public void buildBase() {
    System.out.println("Building word framework");
    b = new Word();
  }

  public void addOfficeUnit(OfficeUnit chapter) {
    System.out.println("Adding word page " + chapter);
    b.add(chapter);
  }

  public Office getFinishedMedia() {
    return b;
  }
}

class PPTBuilder extends OfficeBuilder {
  private PPT m;

  public void buildBase() {
    System.out.println("Building PPT");
    m = new PPT();
  }

  public void addOfficeUnit(OfficeUnit article) {
    System.out.println("Adding slide" + article);
    m.add(article);
  }

  public Office getFinishedMedia() {
    return m;
  }
}

class ExcelBuilder extends OfficeBuilder {
  private Excel w;

  public void buildBase() {
    System.out.println("Building Excel");
    w = new Excel();
  }

  public void addOfficeUnit(OfficeUnit webItem) {
    System.out.println("Adding workbook" + webItem);
    w.add(webItem);
  }

  public Office getFinishedMedia() {
    return w;
  }
}

class OfficeDirector {
  private OfficeBuilder mb;

  public OfficeDirector(OfficeBuilder mb) {
    this.mb = mb; 
  }

  public Office produceMedia(List input) {
    mb.buildBase();
    for (Iterator it = input.iterator(); it.hasNext();)
      mb.addOfficeUnit((OfficeUnit) it.next());
    return mb.getFinishedMedia();
  }
};

public class BuildOfficeDemo {

  public static void main(String[] args) {
    List input = Arrays.asList(new OfficeUnit[] { new OfficeUnit("item1"), new OfficeUnit("item2"),
        new OfficeUnit("item3"), new OfficeUnit("item4"), });

    OfficeDirector buildBook = new OfficeDirector(new WordBuilder());
    Office word = buildBook.produceMedia(input);
    String result = "word: " + word;
    System.out.println(result);

    OfficeDirector buildMagazine = new OfficeDirector(new PPTBuilder());
    Office ppt = buildMagazine.produceMedia(input);
    result = "PPT: " + ppt;
    System.out.println(result);

    OfficeDirector buildWebSite = new OfficeDirector(new ExcelBuilder());
    Office excel = buildWebSite.produceMedia(input);
    result = "Excel: " + excel;
    System.out.println(result);
  }
}








34.6.Builder Pattern
34.6.1.Example of the Builder pattern
34.6.2.Builder pattern with interface
34.6.3.Builder pattern: build a house