Java7新特性(四)并发 1 不可变对象及构造器

本文主要根据《Java程序员修炼之道》整理的代码笔记片段

构造器模式:

1.实现了构造器泛型接口的内部静态类

2.构建不可变类实例的私有构造方法

public interface IObjBuilder<T> {
	T build();
}
public class Update implements {
	private final Author author;
	private final String updateText;
	 
	private Update(Builder builder_){
		author = builder_.author;
		updateText = builder_.updateText;		
	}
	
	public Author getAuthor() {
		return author;
	}

	public String getUpdateText() {
		return updateText;
	}

	public static class Builder implements IObjBuilder<Update>{
		private Author author;
		private String updateText;		
		 
		public Builder author(Author author_){
			author = author_;
			return this;
		}
		
		public Builder updateText(String updateText_){
			updateText = updateText_;
			return this;
		}
				
		@Override
		public Update build() {			
			return new Update(this);
		}	
	}			 
}
使用

Update.Builder ub = new Update.Builder();
ub.author(a).updateText(text).build();



你可能感兴趣的:(并发,新特性,java7,构造器,不可变对象)