好处:

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

  能保证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. html input验证只能输入数字,不能输入其他

    html input验证只能输入数字,不能输入其他 此方法为借鉴别人的,在此只做记录. <input type="text" onkeyup="if(!/^\d+$ ...

  2. SpringAOP简单入门

    注解形式 步骤一.定义一个interface public interface ArithmeticCalculator { double plus(int i, int j); double sub ...

  3. Mysql基本命令一

    一.清除mysql表中数据 delete from 表名;truncate table 表名;不带where参数的delete语句可以删除mysql表中所有内容,使用truncate table也可以 ...

  4. 【转】python入门指引

    http://matrix.42qu.com/10757179 前言 其实我也不知道python怎么入门,由我来写这个真的不是很合适.我学python是直接找了dive into python来看.然 ...

  5. 32位系统装4G以上的内存

    1.操作系统在32位平台上最大寻址空间是4GB,如果要使用4GB以上的内存,就必须使用intel的PAE(物理地址扩展)模式,在windows NT平台实现PAE只需对boot.ini加上/pae即可 ...

  6. sql集锦

    1. emp表中取出1981年入职的员工信息--sql select * from emp where extract(year from emp.hiredate)='1981'; ...陆续添加

  7. encodeURI()和encodeURIComponent()

    encodeURI() 返回值 URIstring 的副本,其中的某些字符将被十六进制的转义序列进行替换. 说明 该方法会替换所有的字符,但不包括以下字符,即使它们具有适当的UTF-8转义序列: 保留 ...

  8. echarts中视觉映射器(visualMap)与时间轴(timeline)混用的实现方法

    1.简述 echarts中的 timeline 组件,提供了在多个 ECharts option 间进行切换.播放等操作的功能. 与其他组件些不同,它需要操作『多个option』. 所以除了基准的ba ...

  9. 洛谷 [P2486] 染色

    树剖+线段树维护连续相同区间个数 注意什么时候长度要减一 #include <iostream> #include <cstdio> #include <cstdlib& ...

  10. HDU3488 Tour [有向环覆盖 费用流]

    Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...