美文网首页Java
图解设计模式Builder模式

图解设计模式Builder模式

作者: MWY | 来源:发表于2017-01-18 17:49 被阅读35次
    UML
    Paste_Image.png
    Builder
    //建造者
    public abstract class Builder {
        public abstract void makeTitle(String title);
        public abstract void makString(String str);
        public abstract void makeItem(String[] items);
        public abstract void close();
    }
    
    
    HTMLBuilder
    //具体的建造者
    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 makString(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></html>");
        }
    
        public String getResult() {
            return filename;
        }
    }
    
    
    TextBuilder
    //具体的建造者
    public class TextBuilder extends Builder {
        private StringBuffer buffer = new StringBuffer();
        @Override
        public void makeTitle(String title) {
            buffer.append("=============================\n");
            buffer.append("¥" + title + "¥\n");
            buffer.append("\n");
        }
    
        @Override
        public void makString(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();
        }
    }
    
    Director
    //Director使用Builder角色的接口生成实例
    public class Director {
        private Builder builder;
        public Director (Builder builder) {
            this.builder = builder;
        }
    
        public void construct() {
            builder.makeTitle("Greeting");
            builder.makString("从早上至下午");
            builder.makeItem(new String[] {
                    "早上好",
                    "下午好"
            });
    
            builder.makString("晚上");
            builder.makeItem(new String[]{
                    "晚上好。",
                    "晚安。",
                    "再见。"
            });
            builder.close();
        }
    }
    
    Main
    public class Main {
    
        public static void main(String[] args) {
        // write your code here
            TextBuilder textBuilder = new TextBuilder();
            Director director = new Director(textBuilder);
            director.construct();
            String result = textBuilder.getResult();
            System.out.println(result);
    
            HTMLBuilder htmlBuilder = new HTMLBuilder();
            Director director1 = new Director(htmlBuilder);
            director1.construct();
            String filename = htmlBuilder.getResult();
            System.out.println(filename + "文件编写完成。");
        }
    
    
    }
    
    

    相关文章

      网友评论

        本文标题:图解设计模式Builder模式

        本文链接:https://www.haomeiwen.com/subject/kjrbbttx.html