好处:

  能保证重叠构造器模式的安全性;

  能保证JAVABeans模式的可读性;

package cn.lonecloud.builder;
/**
* 使用内部类构建器来对这个类进行构造
* @Title: MyConstractor.java
* @Package cn.lonecloud.builder
* @Description:
* @author lonecloud
* @webSite http://wwww.lonecloud.cn
* @date 2016年9月12日 上午10:17:11
*/
public class MyConstractor { //some field of class
private final int size; private final int servings; private final int fat; private final int sodium; private final int age;
//using getMethod
public int getSize() {
return size;
}
public int getServings() {
return servings;
}
public int getFat() {
return fat;
}
public int getSodium() {
return sodium;
}
public int getAge() {
return age;
} @Override
public String toString() {
return "MyConstractor [size=" + size + ", servings=" + servings
+ ", fat=" + fat + ", sodium=" + sodium + ", age=" + age + "]";
}
//using constractMethod init field
private MyConstractor(Builder builder){
this.size=builder.size;
this.servings=builder.servings;
this.fat=builder.fat;
this.sodium=builder.sodium;
this.age=builder.age;
}
//using static class to set this class
public static class Builder{
//some field of staticClass
private final int size; private final int servings;
//using method to set field
private int fat=0; private int sodium=0; private int age=0;
//using constractorMethod to init this final field
public Builder(int size, int servings) {
this.size = size;
this.servings = servings;
}
//write method to set field;
public Builder fat(int fat){
this.fat=fat;
return this;
}
public Builder sodium(int sodium){
this.sodium=sodium;
return this;
}
public Builder age(int age){
this.age=age;
return this;
}
//return MyConstractor
public MyConstractor build(){
return new MyConstractor(this);
} }
}

测试类:

package cn.lonecloud.constactor;

import org.junit.Test;

import cn.lonecloud.builder.MyConstractor;

public class MyTest {

	@Test
public void constractorTest(){
MyConstractor myConstractor = new MyConstractor.Builder(1,2).age(100).build();
System.out.println(myConstractor.toString());
}
}

Java采用内部构造器Builder模式进行对类进行构建的更多相关文章

  1. 疯狂的类构造器Builder模式,链式调用

    疯狂的类构造器 最近栈长在做 Code Review 时,发现一段创建对象的方法: Task task = new Task(112, "紧急任务", "处理一下这个任务 ...

  2. Builder模式的目的是解耦构建过程,为什么要用内部类?

    还没有看过Builder模式的作用,看过一篇介绍Builder模式的文章,这里是关于Builder模式的思考:思考是否有比新建一个内部类更好的方法,首先想到的是 package xyz.n490808 ...

  3. Java采用反射技术创建对象后对目标类的成员变量和成员方法进行访问

    实现: package com.ljy; import java.lang.reflect.Field; import java.lang.reflect.Method; /** * * @Class ...

  4. Builder模式在Java中的应用

    在设计模式中对Builder模式的定义是用于构建复杂对象的一种模式,所构建的对象往往需要多步初始化或赋值才能完成.那么,在实际的开发过程中,我们哪些地方适合用到Builder模式呢?其中使用Build ...

  5. Java设计模式(3)建造者模式(Builder模式)

    Builder模式定义:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. Builder模式是一步一步创建一个复杂的对象,它允许用户可以只通过指定复杂对象的类型和内容就可以构 ...

  6. Builder模式在Java中的应用(转)

    在设计模式中对Builder模式的定义是用于构建复杂对象的一种模式,所构建的对象往往需要多步初始化或赋值才能完成.那么,在实际的开发过程中,我们哪些地方适合用到Builder模式呢?其中使用Build ...

  7. Effective Java 第三版——2. 当构造方法参数过多时使用builder模式

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  8. Builder模式(建造者模式)

    在设计模式中对Builder模式的定义是用于构建复杂对象的一种模式,所构建的对象往往需要多步初始化或赋值才能完成.那么,在实际的开发过程中,我们哪些地方适合用到Builder模式呢?其中使用Build ...

  9. 当构造方法参数过多时使用builder模式

    静态工厂和构造方法都有一个限制:它们不能很好地扩展到很多可选参数的情景.请考虑一个代表包装食品上的营养成分标签的例子.这些标签有几个必需的属性——每次建议的摄入量,每罐的份量和每份卡路里 ,以及超过 ...

随机推荐

  1. LINUX文件操作命令

    body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...

  2. P1345 [USACO5.4]奶牛的电信Telecowmunication

    P1345 [USACO5.4]奶牛的电信Telecowmunication 题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮 ...

  3. mysql主从同步(4)-Slave延迟状态监控

    mysql主从同步(4)-Slave延迟状态监控  转自:http://www.cnblogs.com/kevingrace/p/5685511.html 之前部署了mysql主从同步环境(Mysql ...

  4. web、pc、wap、app的区别

    通常情况下web=pc,wap=app,前者指电脑用的程序,后者指手机用的程序. 更深层的区别是,pc电脑上软件,web电脑上的网页,wap手机上的网页,app手机用软件

  5. Springboot中使用AOP统一处理Web请求日志

    title: Springboot中使用AOP统一处理Web请求日志 date: 2017-04-26 16:30:48 tags: ['Spring Boot','AOP'] categories: ...

  6. webpack 介绍 & 安装 & 常用命令

    webpack 介绍 & 安装 & 常用命令 webpack系列目录 webpack 系列 一:模块系统的演进 webpack 系列 二:webpack 介绍&安装 webpa ...

  7. input标签(待填坑)

    input标签几种属性值 button:用作定义按钮 checkbox:定义复选框 file:供文件上传 hidden:定义隐藏的输入字段 image:图像形式的提交按钮 password:密码字段 ...

  8. 【转】shell学习笔记(七)——流程控制之while

    while do done, until do done (不定回圈) 当 condition 条件成立时,就进行回圈,直到 condition 的条件不成立才停止 while [condition] ...

  9. ehcache模糊批量移除缓存

    目录 前言 实现 总结 前言 众所周知,encache是现在最流行的java开源缓存框架,配置简单,结构清晰,功能强大.通过注解@Cacheable可以快速添加方法结果到缓存.通过@CacheEvic ...

  10. final修饰符,多态,抽象类,接口

    1:final关键字(掌握)    (1)是最终的意思,可以修饰类,方法,变量.    (2)特点:        A:它修饰的类,不能被继承.        B:它修饰的方法,不能被重写.      ...