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

package xyz.n490808114.test;
public class BuilderTest{
String name;
int age;
int high;
int weight;
int speed; public BuilderTest name(String name){
this.name = name;
return this;
}
public BuilderTest age(int age){
this.age = age;
return this;
}
public BuilderTest high(int high){
this.high = high;
return this;
}
public BuilderTest weight(int weight){
this.weight = weight;
return this;
}
public BuilderTest speed(int speed){
this.speed = speed;
return this;
}
public void setName(String name){this.name = name;}
public void setAge(int age){this.age = age;}
public void setHigh(int high){this.high = high;}
public void setWeight(int weight){this.weight = weight;}
public void setSpeed(int speed){this.speed = speed;}
public String getName(){return this.name;}
public int getAge(){return this.age;}
public int getHigh(){return this.high;}
public int getWeight(){return this.weight;}
public int getSpeed(){return this.speed;}
public static void main(String[] args) {
BuilderTest t = new BuilderTest().age(20).speed(50);
System.out.println(t.name);
System.out.println(t.age);
System.out.println(t.high);
System.out.println(t.weight);
System.out.println(t.speed);
}
}

输出为

null
20
0
0
50

这样感觉跟set方法重复太多,整合一下,如下

package xyz.n490808114.test;
public class BuilderTest{
String name;
int age;
int high;
int weight;
int speed;
public BuilderTest setName(String name){this.name = name;return this;}
public BuilderTest setAge(int age){this.age = age;return this;}
public BuilderTest setHigh(int high){this.high = high;return this;}
public BuilderTest setWeight(int weight){this.weight = weight;return this;}
public BuilderTest setSpeed(int speed){this.speed = speed;return this;}
public String getName(){return this.name;}
public int getAge(){return this.age;}
public int getHigh(){return this.high;}
public int getWeight(){return this.weight;}
public int getSpeed(){return this.speed;}
public static void main(String[] args) {
BuilderTest t = new BuilderTest().setAge(20).setSpeed(50);
System.out.println(t.name);
System.out.println(t.age);
System.out.println(t.high);
System.out.println(t.weight);
System.out.println(t.speed);
}
}

这样只是精简代码,只是不知道这样的set方法能不能被spring认可,待测试

能够想到的是:

1,Builder模式用内部类的方法,可以用来修改现有代码,不改动,不新增类的方法,仅在创建时使用内部类;

2,猜测:如果不是频繁创建的话,对性能的影响不大;

3,如果不采用模式,仅使用set方法的话,代码量会多,但是代码会更清晰;

4,Builder模式使代码更易阅读。

Builder模式

package xyz.n490808114.test;
public class BuilderTest{
String name;
int age;
int high;
int weight;
int speed;
public BuilderTest(Builder builder){
this.name = builder.name;
this.age = builder.age;
this.high = builder.high;
this.weight = builder.weight;
this.speed = builder.speed;
}
public void setName(String name){this.name = name;}
public void setAge(int age){this.age = age;}
public void setHigh(int high){this.high = high;}
public void setWeight(int weight){this.weight = weight;}
public void setSpeed(int speed){this.speed = speed;}
public String getName(){return this.name;}
public int getAge(){return this.age;}
public int getHigh(){return this.high;}
public int getWeight(){return this.weight;}
public int getSpeed(){return this.speed;} public static class Builder{
String name;
int age;
int high;
int weight;
int speed;
public Builder name(String name){
this.name = name;
return this;
}
public Builder age(int age){
this.age = age;
return this;
}
public Builder high(int high){
this.high = high;
return this;
}
public Builder weight(int weight){
this.weight = weight;
return this;
}
public Builder speed(int speed){
this.speed = speed;
return this;
}
public BuilderTest build(){
return new BuilderTest(this);
}
}
public static void main(String[] args) {
BuilderTest t = new BuilderTest.Builder().age(20).speed(50).build();
BuilderTest s = new BuilderTest.Builder().age(120).speed(150).build();
System.out.println(t.name);
System.out.println(t.age);
System.out.println(t.high);
System.out.println(t.weight);
System.out.println(t.speed);
System.out.println(s.name);
System.out.println(s.age);
System.out.println(s.high);
System.out.println(s.weight);
System.out.println(s.speed);
}
}

输出为

null
20
0
0
50
null
120
0
0
150

Builder模式的目的是解耦构建过程,为什么要用内部类?的更多相关文章

  1. Java采用内部构造器Builder模式进行对类进行构建

    好处: 能保证重叠构造器模式的安全性: 能保证JAVABeans模式的可读性: package cn.lonecloud.builder; /** * 使用内部类构建器来对这个类进行构造 * @Tit ...

  2. Builder模式在Java中的应用

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

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

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

  4. 设计模式之Builder模式

    一.感性认识 二.Builder模式 1.定义 一个复杂对象的构建与其表示相分离,使得同样的构建过程可以创建不同的表示.即构建过程相同,但是子部件却不相同. 2.结构说明 Builder: 创建者接口 ...

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

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

  6. java的设计模式 - Builder模式

    Builder 模式的目的? 构造对象的方式过于复杂,不如将之抽离出来.比如,构造器参数过多 这样说也有点抽象,举个例子吧. 举个例子 比如 非常热门的消息队列RabbitMQ 的 AMQP.Basi ...

  7. 【5】Builder模式(构建者模式)

    一.引言 在软件系统中,有时需要创建一个复杂对象,并且这个复杂对象由其各部分子对象通过一定的步骤组合而成.例如一个采购系统中,如果需要采购员去采购一批电脑时,在这个实际需求中,电脑就是一个复杂的对象, ...

  8. 设计模式之构建者(Builder)模式

    在五大设计原则的基础上经过GOF(四人组)的总结,得出了23种经典设计模式,其中分为三大类:创建型(5种).结构型(7种).行为型(11种).今天对创建型中的构建者(Builder)模式的思想进行了一 ...

  9. JAVA Builder模式构建MAP/LIST的示例

    我们在构建一个MAP时,要不停的调用put,有时候看着觉得很麻烦,刚好,看了下builder模式,觉得这思路不错,于是乎,照着用builder模式写了一个构建MAP的示例,代码如下: import j ...

随机推荐

  1. JavaScript-----3.变量

    1.变量的使用 变量在使用的时候分两步:1. 声明变量 2. 赋值 1.1声明变量 //声明变量 var age;//声明一个名字为age的变量 var是JS的一个关键字,用于声明变量,使用该关键字声 ...

  2. python字符串的特性及相关应用

    一.字符串定义 字符串是 Python 中最常用的数据类型.用单引号(' '),双引号(" ")或者三引号(''' ''')括起来的数据称为字符串(其中,使用三引号的字符串可以横跨 ...

  3. SpringBoot添加热部署

    一.导入依赖 <!--热部署--> <dependency> <groupId>org.springframework.boot</groupId> & ...

  4. [TimLinux] django model关于QuerySet

    1. 获取执行过的sql命令 from django.db import connections connections['default'].queries 2. 获取QuerySet将执行的sql ...

  5. 2017 CCPC秦皇岛 A题 A Ballon Robot

    The 2017 China Collegiate Programming Contest Qinhuangdao Site is coming! There will be  teams parti ...

  6. Python计算IV值

    更多大数据分析.建模等内容请关注公众号<bigdatamodeling> 在对变量分箱后,需要计算变量的重要性,IV是评估变量区分度或重要性的统计量之一,python计算IV值的代码如下: ...

  7. windows程序设计01_utf8编码问题

    坚持与妥协 从学程序的第一天老师就给我们说源代码应该使用utf8保存.因为先入为主,"源代码应该使用utf8"的观念已经在"学院派"出身的程序员脑子里根深蒂固. ...

  8. Dubbo 2.7新特性之异步化改造

    这是why技术的第1篇原创文章 我与Dubbo的二三事 我是2016年毕业的,在我毕业之前,我在学校里面学到的框架都是SSH,即struts+spring+hibernate,是的你没有看错,在大学里 ...

  9. 转-友晶Sdram_Control_4Port的全页操作Bug?

    http://www.cnblogs.com/edaplayer/p/3678897.html 以前在学校初学fpga的时候碰到sdram就搞不定了,现在突然发现网上有好多现成的代码,友晶的,alte ...

  10. Python 3 对象关系映射(ORM)

    ORM 对象关系映射 Object Relational Mapping 表 ---> 类 字段 ---> 属性 记录 ---> 对象 # mysql_client.py impor ...