一、什么是Builder模式?
builder模式,就好比一栋大楼,这栋大楼有很多个组件组成,但是这栋大楼只是一个各个组件结合起来的框架,每一栋大楼都有各自的特色,这些特色由它们实际情况决定。对于程序而言,这栋大楼就好比一个Builder抽象类,里面定义了很多组件方法,一个完整的功能离不开里面的每一个方法,具体实现依赖子类,只要继承这个抽象类,就可以根据组件方法组建起自己的“大楼”。
二、Builder模式思想
Builder类:负责生成实例的接口,里面定义了用于生成实例的方法;
ConcreteBuilder类:负责实现Builder抽象类,此外还定义了获取最终结果(大楼)的方法;
Director类:负责使用Builder抽象类生成实例,它并不依赖Builder的具体子类,所以它只会调用Builder里面定义的方法。
三、应用实例
设计一个Builder,用于编写文档,可以是Text文档,也可以是HTML文档。
1、Builder类
package com.cjs.builder; public abstract class Builder { public abstract void makeTitle(String title); public abstract void makeString(String str); public abstract void makeItem(String[] items); public abstract void close(); }
2、Director类
package com.cjs.builder; public class Director { Builder builder; public Director(Builder builder) { this.builder = builder; } public void construct() { builder.makeTitle("Greeting"); builder.makeString("从早上至下午"); builder.makeItem(new String[]{"Good Morning", "Good Afternoon"}); builder.makeString("晚上"); builder.makeItem(new String[]{"Good Night","Bye-Bye"}); builder.close(); } }
3、TextBuilder类
package com.cjs.builder; public class TextBuilder extends Builder { private StringBuilder buffer = new StringBuilder(); @Override public void makeTitle(String title) { buffer.append("=========================\n"); buffer.append("[" + title + "]\n"); buffer.append("\n"); } @Override public void makeString(String str) { buffer.append("■" + str + "\n"); buffer.append("\n"); } @Override public void makeItem(String[] items) { for (int i = 0; i < items.length; i++) { buffer.append(" ·" + items[i] + "\n"); buffer.append("\n"); } } @Override public void close() { buffer.append("=========================\n"); } public String getResult() { return buffer.toString(); } }
4、HTMLBuilder类
package com.cjs.builder; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class HTMLBuilder extends Builder { private String fileName; private PrintWriter writer; @Override 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>"); } @Override public void makeString(String str) { writer.println("<p>"+str+"</p>"); } @Override public void makeItem(String[] items) { writer.println("<ul>"); for (int i = 0; i < items.length; i++) { writer.println("<li>" + items[i] + "</li>"); } writer.println("</ul>"); } @Override public void close() { writer.println("</body></head"); writer.close(); } public String getResult() { return fileName; } }
5、Main类
package com.cjs.builder; public class Main { public static void main(String[] args) { if (args.length != 1) { usage(); System.exit(0); } if (args[0].equals("plain")) { TextBuilder textBuilder = new TextBuilder(); Director director = new Director(textBuilder); director.construct(); String result = textBuilder.getResult(); System.out.println(result); } else if (args[0].equals("html")) { HTMLBuilder htmlBuilder = new HTMLBuilder(); Director director = new Director(htmlBuilder); director.construct(); System.out.println(htmlBuilder.getResult()+"文件编写完成"); } else { usage(); System.exit(0); } } public static void usage() { System.out.println("Usage: java main plain 编写文本文档"); System.out.println("Usage: java main html 编写HTML文档"); } }
对于Director类,它并不关心实际编写文档是采用纯文本,还是HTML,所以在Builder抽象类里必须声明足够多的方法,同时,Builder类的实现类可以拥有自己独特的方法,以实现各自对实际情况的逻辑需求。
原文地址:https://www.cnblogs.com/SysoCjs/p/10327199.html
时间: 2024-11-06 07:19:04