Builder Pattern - PowerPoint PPT Presentation

builder pattern n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Builder Pattern PowerPoint Presentation
Download Presentation
Builder Pattern

play fullscreen
1 / 21
Builder Pattern
234 Views
Download Presentation
kasimir-cross
Download Presentation

Builder Pattern

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Builder Pattern S. Yukita yukita@k.hosei.ac.jp

  2. Builder パターン使われ所 • 大きなインスタンスを組み上げる場合。 • インスタンスの組み上げにパターンがあるとき,パターンをディレクタに固定する。 • 組み上げパターンに従って実際の仕事をするのはBuilderインタフェース(抽象クラス)を実装(具象化)するクラス。 Builder Pattern

  3. uses 例題のクラス図 Builder Pattern

  4. Builder.java public abstract class Builder { public abstract void makeTitle(String title); public abstract void makeString(String str); public abstract void makeItems(String[] items); public abstract Object getResult(); } Builder Pattern

  5. TextBuilder.java (1) public class TextBuilder extends Builder { private StringBuffer buffer = new StringBuffer(); public void makeTitle(String title) { buffer.append("==============================\n"); buffer.append("『" + title + "』\n"); buffer.append("\n"); } Builder Pattern

  6. TextBuilder.java (2) public void makeString(String str) { buffer.append('■' + str + "\n"); buffer.append("\n"); } Builder Pattern

  7. TextBuilder.java (3) public void makeItems(String[] items) { for (int i = 0; i < items.length; i++) { buffer.append(" ・" + items[i] + "\n"); } buffer.append("\n"); } Builder Pattern

  8. TextBuilder.java (4) public Object getResult() { buffer.append("==============================\n"); return buffer.toString(); } } Builder Pattern

  9. HTMLBuilder.java (1) import java.io.*; public class HTMLBuilder extends Builder { private String filename; private PrintWriter writer; Builder Pattern

  10. HTMLBuilder.java (2) public void makeTitle(String title) { filename = title + ".html"; try { writer = new PrintWriter(new FileWriter(filename)); } catch (IOException e) { e.printStackTrace(); } writer.println( "<html><head><title>" + title + "</title></head><body>"); writer.println("<h1>" + title + "</h1>"); } Builder Pattern

  11. HTMLBuilder.java (3) public void makeString(String str) { writer.println("<p>" + str + "</p>"); } public void makeItems(String[] items) { writer.println("<ul>"); for (int i = 0; i < items.length; i++) { writer.println("<li>" + items[i] + "</li>"); } writer.println("</ul>"); } Builder Pattern

  12. HTMLBuilder.java (4) public Object getResult() { writer.println("</body></html>"); writer.close(); return filename; } } Builder Pattern

  13. Director.java (1) public class Director { private Builder builder; public Director(Builder builder) { this.builder = builder; } Builder Pattern

  14. public Object construct() { builder.makeTitle("Greeting"); builder.makeString("朝から昼にかけて"); builder.makeItems(new String[]{ "おはようございます。", "こんにちは。" }); builder.makeString("夜に"); builder.makeItems(new String[]{ "こんばんは。", "おやすみなさい。", "さようなら。" }); return builder.getResult(); } } Director.java (2) Builder Pattern

  15. Main.java (1) public class Main { public static void main(String[] args) { if (args.length != 1) { usage(); System.exit(0); } Builder Pattern

  16. if (args[0].equals("plain")) { Director director = new Director( new TextBuilder()); String result = (String)director.construct(); System.out.println(result); } else if (args[0].equals("html")) { Director director = new Director( new HTMLBuilder()); String filename = (String)director.construct(); System.out.println(filename + "が作成されました。"); } else { usage(); System.exit(0); } } Main.java (2) Builder Pattern

  17. Main.java (3) public static void usage() { System.out.println( "Usage: java Main plain プレーンテキストで文書作成"); System.out.println( "Usage: java Main html HTMLファイルで文書作成"); } } Builder Pattern

  18. 実行結果(plain) java Main plain ================================= [Greeting] ■朝から昼にかけて  ・おはようございます。  ・こんにちは。 ■夜に  ・こんばんは。  ・おやすみなさい。  ・さようなら。 ================================= Builder Pattern

  19. 実行結果(HTML) java Main html Greeting.htmlが作成されました。 <html><head><title>Greeting</title></head><body> <h1>Greeting</h1> <p>朝から昼にかけて</p> <ul> <li>おはようございます。</li> <li>こんにちは。</li> </ul> <p>夜にかけて</p> <ul> <li>こんばんは。</li> <li>おやすみなさい。</li> <li>さようなら。</li> </ul> </body></html> Builder Pattern

  20. uses パターン Builder Pattern

  21. Sequence 図 :Client :Director :ConcreteBuilder construct buildPart1 Builder Pattern