1 /** 
   2  * BuilderPattern Example
   3  */
   4 
   5 import java.io.*;
   6 
   7 abstract class Builder {
   8     public abstract void makeTitle(String title);
   9     public abstract void makeString(String str);
  10     public abstract void makeItems(String[] items);
  11     public abstract Object getResult();
  12 }
  13 
  14 class Director {
  15     private Builder builder;
  16     public Director(Builder builder) {
  17         this.builder = builder;
  18     }
  19     public Object construct() {
  20         builder.makeTitle("Greeting");
  21         builder.makeString("In the morning and afternoon...");
  22         builder.makeItems(new String[] {
  23             "Good morning",
  24             "Good afternoon",
  25         });
  26         builder.makeString("In the evening...");
  27         builder.makeItems(new String[] {
  28             "Hello",
  29             "Good evening",
  30             "Good nignt",
  31         });
  32         return builder.getResult();
  33     }
  34 }
  35 
  36 class TextBuilder extends Builder {
  37     private StringBuffer buffer = new StringBuffer();
  38     public void makeTitle(String title) {
  39         buffer.append("=====================\n");
  40         buffer.append("["+title+"]");
  41         buffer.append("\n");
  42     }
  43     public void makeString(String str) {
  44         buffer.append(" * "+str+"\n");
  45         buffer.append("\n");
  46     }
  47     public void makeItems(String[] items) {
  48         for(int i=0; i < items.length; i++) {
  49             buffer.append(" . "+items[i]+"\n");
  50         }
  51         buffer.append("\n");
  52     }
  53     public Object getResult() {
  54         buffer.append("=======================\n");
  55         return buffer.toString();
  56     }
  57 }
  58 
  59 class HTMLBuilder extends Builder {
  60     private String filename;
  61     private PrintWriter writer;
  62     public void makeTitle(String title) {
  63         filename = title+".html";
  64         try {
  65             writer = new PrintWriter(new FileWriter(filename));
  66         } catch(IOException e) {
  67             e.printStackTrace();
  68         }
  69         writer.println("<html><head><title>"+title+"</title></head><body>");
  70         writer.println("<h1>"+title+"</h1>");
  71     }
  72     public void makeString(String str) {
  73         writer.println("<p>"+str+"</p>");
  74     }
  75     public void makeItems(String[] items) {
  76         writer.println("<ul>");
  77         for(int i=0; i < items.length; i++) {
  78             writer.println("<li>"+items[i]+"</li>");
  79         }
  80         writer.println("</ul>");
  81     }
  82     public Object getResult() {
  83         writer.println("</body></html>");
  84         writer.close();
  85         return filename;
  86     }
  87 }
  88 
  89 public class BuilderDoc {
  90     public static void main(String[] args) {
  91         Director d1 = new Director(new TextBuilder());
  92         String result = (String)d1.construct();
  93         System.out.println(result);
  94 
  95         Director d2 = new Director(new HTMLBuilder());
  96         String filename = (String)d2.construct();
  97         System.out.println(filename);
  98     }
  99 }
web biohackers.net