设计模式——建造者模式(Builder)

本文介绍了一种使用建造者设计模式构建书籍的方法,通过代码展示了文章类、纸张类、书籍类和书籍生产导演类的实现,实现了书籍的构建过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码:GitHub

建造者设计模式

0. UML结构图

建造者模式UML结构图

1. 构建一本书籍,Code展示

1.1 文章类

public class Article {

    /** 文章类型 */
    private String type;

    /** 文章内容 */
    private String content;

    public Article(String type, String content) {
        this.type = type;
        this.content = content;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
    
}

1.2 纸张类

public class Paper {

    /** 纸张质量 */
    private int quality;

    /** 纸张能容纳的文字数 */
    private int size;

    /** 纸张上的文字内容 */
    private String content;

    public Paper(int quality, int size) {
        this.quality = quality;
        this.size = size;
    }

    public int getQuality() {
        return quality;
    }

    public void setQuality(int quality) {
        this.quality = quality;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }


    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

}

1.3 书籍类

import java.util.ArrayList;

/**
 * 书 - 建造者模式
 *
 * @author ALion
 * @version 2019/1/27 18:26
 */
public class Book {

    private String name;

    /** 作者 */
    private String author;

    /** 出版社 */
    private String publisher;

    /** 内容页 */
    private ArrayList<Paper> paperList;


    Book(String name, String author, String publisher, ArrayList<Paper> paperList) {
        this.name = name;
        this.author = author;
        this.publisher = publisher;
        this.paperList = paperList;
    }

    /**
     * 展示书籍
     */
    public void show() {
        System.out.println("---------------------------------------");
        System.out.println("name = " + name);
        System.out.println("author = " + author);
        System.out.println("publisher = " + publisher);
        System.out.println("paperContent = " + paperList.get(0).getContent());
        System.out.println("paperContent = " + paperList.get(1).getContent());
        System.out.println("paperContent = " + "……………………………………");
        System.out.println("paperContent = " + paperList.get(paperList.size() - 1).getContent());
        int price = paperList.size() * 10;
        System.out.println("price = " + price + "$");
        System.out.println("---------------------------------------");
    }

    /**
     * 建造者类 - 供外部调用
     */
    public static class Builder {

        private Paper paper;

        private Article article;

        private String author;

        private String publisher;

        private String name;

        /**
         * 设置纸张属性
         * @param paper 纸张
         * @return Book.Builder
         */
        public Builder paper(Paper paper) {
        	// 此处可以检查传参的合法性,例如paper是否为null
            this.paper = paper;
            return this;
        }

        /**
         * 设置文章
         * @param article 文章
         * @return Book.Builder
         */
        public Builder article(Article article) {
            this.article = article;
            return this;
        }

        /**
         * 设置作者
         * @param author 作者
         * @return Book.Builder
         */
        public Builder author(String author) {
            this.author = author;
            return this;
        }

        public Builder name(String name) {
            this.name = name;
            return this;
        }

        /**
         * 设置出版社
         * @param publisher 出版社
         * @return Book.Builder
         */
        public Builder publisher(String publisher) {
            this.publisher = publisher;
            return this;
        }

        public Book build() {
            // 此处使用导演类,按某个流程生成书籍
            return BookDirector.create(name, author, publisher, paper, article);
        }

    }

}

1.4 书籍生产导演类

import java.util.ArrayList;

/**
 * 生产书籍的导演类
 * <p>
 *     这个类决定了生产书籍的流程。
 *     除了已有的打印流程,你还可以添加其他详细流程,例如:
 *     1.联系一家印刷厂
 *     2.建立一个修订的方法,对书的内容进行预处理(例如将内容排版改为由右至左),再打印
 *     3.根据纸张、作者、内容、人力等,计算生产书籍的成本
 * </p>
 *
 * @author ALion
 * @version 2019/1/27 20:04
 */
public class BookDirector {

    /**
     * 根据参数,按流程创建书籍
     * @param name 书名
     * @param author 作者
     * @param publisher 出版社
     * @param paper 纸张
     * @param article 文章
     * @return 书籍
     */
    static Book create(String name, String author, String publisher, Paper paper, Article article) {
        ArrayList<Paper> paperList = print(paper, article);
        return new Book(name, author, publisher, paperList);
    }

    /**
     * 印刷书籍内容部分
     *
     * @param paper   纸张
     * @param article 文章
     * @return 纸张页集合
     */
    private static ArrayList<Paper> print(Paper paper, Article article) {
        System.out.println("Builder.print => 准备打印书籍内容页");

        int quality = paper.getQuality();
        int size = paper.getSize();
        String content = article.getContent();

        ArrayList<Paper> paperList = new ArrayList<>();
        int length = content.length();
        for (int i = 0; i < length; i += size) {
            Paper p = new Paper(quality, size);
            int e = i + p.getSize() < length ? i + p.getSize() : length;
            p.setContent(content.substring(i, e));
            paperList.add(p);
            System.out.println("Builder.print => 完成第" + paperList.size() + "页打印");
        }

        System.out.println("Builder.print => 完成书籍内容页打印");
        return paperList;
    }

}

2. 书籍建造者Test

public class BuilderTest {

    public static void main(String[] args) {
        // 构建纸张属性
        Paper paper = new Paper(100, 50);

        // 构建文章
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < 100; i++) {
            builder.append("Romeo and Julie ").append(i);
        }
        Article article = new Article("literature", builder.toString());

        // 开始构建书籍
        Book book = new Book.Builder()
                .name("Romeo and Juliet")
                .author("William Shakespeare")
                .publisher("People's Literature Press")
                .paper(paper)
                .article(article)
                .build();

        // 展示数据
        book.show();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值