好处:

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

  能保证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. Java接口和抽象类的理解

    接口和抽象类的相同之处就是 都会有抽象方法 抽象方法就是一个没有方法体 等待继承的子类完成的方法 然而接口比较严格 它的方法必须是抽象方法且是公开的 抽象类 可以有自己的属性 和 实体方法 首相用面向 ...

  2. bxslider使用教程

    bxSlider下载+参数说明 "bxSlider"就是一款响应式的幻灯片js插件 bxSlider特性 充分响应各种设备,适应各种屏幕: 支持多种滑动模式,水平.垂直以及淡入淡出 ...

  3. python_为被装饰的函数保留元数据

    案例: 在函数对象中保存着一些函数的元数据,如: f.__name__           函数名 f.__doc__              函数文档 f.__moudle__       函数所 ...

  4. 流API--提取流+组合流

    提取子流和组合流 1,limit(n)会返回一个包含n个元素的新流,如果原始流的长度小于n,则会返回原始的流.这个方法可用来裁剪指定长度的流. 2,skip(n)正好相反,它会丢弃掉前面的n个元素. ...

  5. 比较Maven和Ant

    从今天开始,整理maven一系列. Maven 它是什么? 如何回答这个问题要看你怎么看这个问题. 绝大部分Maven用户都称Maven是一个"构建工具":一个用来把源代码构建成可 ...

  6. 在Tomcat中配置单点登录

    单点登录:Single Sign-On .概述 一旦你设置了realm和验证的方法,你就需要进行实际的用户登录处理.一般说来,对用户而言登录系统是一件很麻烦的事情,你必须尽量减少用户登录验证的次数.作 ...

  7. test for python thread

    #!/usr/bin/python # -*- coding: UTF-8 -*- import thread import time # 为线程定义一个函数 def print_time(threa ...

  8. nodejs爬虫笔记(一)---request与cheerio等模块的应用

    目标:爬取慕课网里面一个教程的视频信息,并将其存入mysql数据库.以http://www.imooc.com/learn/857为例. 一.工具 1.安装nodejs:(操作系统环境:WiN 7 6 ...

  9. [PHP]接口请求校验的原理

    具体的校验步骤可以自定义,下面是比较直观的一种形式: 1. 客户端:请求参数带上时间,进行首字母排序,连接私钥后,取得加密结果: 客户端请求时带上这个加密结果作为sign参数. 2. 服务端:对sig ...

  10. yii 缓存之apc

    首先yii CApcCache 实现了一个针对APC的缓存应用组件,常见的缓存操作方法get,set,add,delete,flush... 下面说说配置: 1. 在config/main.php c ...