Design Pattern--Builder模式

阅读更多

微博:http://weibo.com/375061590

QQ :375061590

 

         当一个类有多个属性的时候,这时候要去实例化这个类,太多的属性会带来一些不便。

把广东工业大学图书馆的书看成一个实体:

 

package DesignPattern;
 
public class Book {
         /**
          * 书名
          */
         private String bookName;
         /**
          * 可借数量
          */
         private String leandableNum;
         /**
          * 索引号
          */
         private String reference;
         /**
          * 作者
          */
         private String writer;
         /**
          * 出版社
          */
         private String publisher;
         /**
          * 出版年
          */
         private String pubYear;
 
         public Book(String bookName, String leandableNum, String reference,
                            String writer, String publisher, String pubYear) {
                   this.bookName=bookName;
                   this.leandableNum=leandableNum;
                   this.reference=reference;
                   this.writer=leandableNum;
                   this.publisher=publisher;
                   this.pubYear=pubYear;
         }
 
}

实例化Book

Book book = new Book(“深入分析Java Web技术内幕 [专著]”, “0”, “TP312JA/X781”, “许令波”, “电子工业出版社”, “2012”);

 

过多的参数会使使用者混淆,不小心少了一个参数,或者颠倒了位置,会使实例化出错,或者实例出现数据错误,而这种错误编译器是不会报错的。

 

 

 

重叠构造器

重叠构造器解决了一般构造器多参数时候参数混乱的缺点

 

public  Book(String bookName) {
                   this( bookName,"");
         }
         public Book(String bookName, String leandableNum) {
                   this( bookName,leandableNum,"");
         }
         public Book(String bookName, String leandableNum, String reference) {
                   this( bookName,leandableNum,reference,"");
         }
         public Book(String bookName, String leandableNum, String reference,
                            String writer) {
                   this( bookName,leandableNum,reference,writer,"");
         }
         public Book(String bookName, String leandableNum, String reference,
                            String writer, String publisher) {
                   this( bookName,leandableNum,reference,writer,publisher,"");
         }
         public Book(String bookName, String leandableNum, String reference,
                            String writer, String publisher, String pubYear) {
                   this.bookName=bookName;
                   this.leandableNum=leandableNum;
                   this.reference=reference;
                   this.writer=leandableNum;
                   this.publisher=publisher;
                   this.pubYear=pubYear;
         }

 

这类构造通过构造器调用构造器,最后会调用到最后一个构造器,包含了所有参数,但是该方法在更多属性的时候,参数变得太多的话,会产生一大堆构造器,且难控制,代码就太冗长了。

 

 

JavaBeans的setter方法

该方法取代重叠构造器一大堆的构造方法,简化成只需要一个不带参数的构造方法:

 

public  Book() {       }
public void setBookName(String bookName) {
         this.bookName = bookName;
}
public void setLeandableNum(String leandableNum) {
         this.leandableNum = leandableNum;
}
public void setReference(String reference) {
         this.reference = reference;
}
public void setWriter(String writer) {
         this.writer = writer;
}
public void setPublisher(String publisher) {
         this.publisher = publisher;
}
public void setPubYear(String pubYear) {
         this.pubYear = pubYear;
}
public void setBook(Book book) {
         this.book = book;
}
 
Book book = new Book();
book. setBookName(“深入分析Java Web技术内幕 [专著]”);
book .setLeandableNum(“0”);
book .setReference(“TP312JA/X781”);
book . setWriter (“许令波”);
book . setPublisher (“电子工业出版社”);
book . setPubYear (“2012”);

 

       setter方法名称简单易懂,使得代码更加清晰,不会很容易混淆参数。

       但是如果该类是一个不可变类(像String类),setter方法就会破坏其不可变性,使得类是线程不安全的,这时候就要花心思对线程安全进行处理。

 

 

 

Builder模式

可以解决这个问题,既有很强可读性,又能保证不可变类的线程安全

 

package DesignPattern;
 
public class Book {
         /**
          * 书名
          */
         private String bookName;
         /**
          * 可借数量
          */
         private String leandableNum;
         /**
          * 索引号
          */
         private String reference;
         /**
          * 作者
          */
         private String writer;
         /**
          * 出版社
          */
         private String publisher;
         /**
          * 出版年
          */
         private String pubYear;
 
 
         public Book(Builder builder) {
                   this.bookName = builder.bookName;
                   this.leandableNum = builder.leandableNum;
                   this.reference = builder.reference;
                   this.writer = builder.writer;
                   this.publisher = builder.publisher;
                   this.pubYear = builder.pubYear;
         }
 
 
 
         public static class Builder {
                   /**
                    * 书名
                    */
                   protected String bookName;
                   /**
                    * 可借数量
                    */
                   protected String leandableNum;
                   /**
                    * 索引号
                    */
                   protected String reference;
                   /**
                    * 作者
                    */
                   protected String writer;
                   /**
                    * 出版社
                    */
                   protected String publisher;
                   /**
                    * 出版年
                    */
                   private String pubYear;
 
                   public Book build() {
                            return new Book(this);
 
                   }
 
                   public void setBookName(String bookName) {
                            this.bookName = bookName;
                   }
 
                   public void setLeandableNum(String leandableNum) {
                            this.leandableNum = leandableNum;
                   }
 
                   public void setReference(String reference) {
                            this.reference = reference;
                   }
 
                   public void setWriter(String writer) {
                            this.writer = writer;
                   }
 
                   public void setPublisher(String publisher) {
                            this.publisher = publisher;
                   }
 
                   public void setPubYear(String pubYear) {
                            this.pubYear = pubYear;
                   }
 
         }
 
}

 

Book.Builder builder = new Builder();
builder. setBookName(“深入分析Java Web技术内幕 [专著]”);
builder.setLeandableNum(“0”);
builder.setReference(“TP312JA/X781”);
builder. setWriter (“许令波”);
builder. setPublisher (“电子工业出版社”);
builder. setPubYear (“2012”);
Book book = builder.build();

 

 

 

 

 

你可能感兴趣的:(设计模式,builder,构建者模式)