「Builder」という英単語は、「建築者」・「建築業者」を意味します。
全体を構成している各部分部分の処理を積上げていくのが「Builder パターン」です。
Builderパターンのクラス図
1. Client.java
public class Client { public static void main(String[] args){ String result = ""; Director director = new Director(new ConcreteBuilder()); result = (String)director.construct(); System.out.println(result); } }
2. Director.java
public class Director { private Builder builder; public Director(Builder builder){ this.builder = builder; } public Object construct(){ builder.part1("工程1"); builder.part2("工程2"); return builder.getResult(); } }
3. Builder.java
public abstract class Builder { public abstract void part1(String str); public abstract void part2(String str); public abstract Object getResult(); }
4. ConcreteBuilder.java
public class ConcreteBuilder extends Builder { private StringBuffer sb = new StringBuffer(); public void part1(String str){ sb.append("part1:" + str + System.getProperty("line.separator")); } public void part2(String str){ sb.append("part2:" + str + System.getProperty("line.separator")); } public Object getResult(){ return sb.toString(); } }
C:\sample\desin_pattern\builder>javac Client.java [Enter] C:\sample\desin_pattern\builder>java Client [Enter] part1:工程1 part2:工程2