建造者设计模式
0. 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;
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;
public Builder paper ( Paper paper) {
this . paper = paper;
return this ;
}
public Builder article ( Article article) {
this . article = article;
return this ;
}
public Builder author ( String author) {
this . author = author;
return this ;
}
public Builder name ( String name) {
this . name = name;
return this ;
}
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;
public class BookDirector {
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) ;
}
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 ( ) ;
}
}