还没有看过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. Chapter 05—Advanced data management(Part 1)

    一. R的数学函数,统计函数及字符处理函数 例01:一道实际应用题 一组学生其数学,科学和英语的成绩如下表: 任务:根据成绩,决定对每个学生的单独指导: 前20%的学生的成绩为A,次之为B,以此类推: ...

  2. scrapy 五大核心组件-分页

    scrapy 五大核心组件-分页 分页 思路 总的原理和之前是一样的,但是由于框架的原因,要遵循他框架的使用方式,每次更改他的url,并指定回调函数 # -*- coding: utf-8 -*- i ...

  3. 【java】使用java.util的【Collections】简化List创建

    我们在创建一个List并往其中加入一个元素的时候一般会这么做: public List<User> getCurrentUser() { List<User> users = ...

  4. 【开发记录】Linux常用命令记录(一)【华为云技术分享】

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/devcloud/article/detai ...

  5. 补习系列(20)-大话 WebSocket 与 "尬聊"的实现

    目录 一.聊聊 WebSocket 二.Stomp 是个什么鬼 三.SpringBoot 整合 WebSocket A. 引入依赖 B. WebSocket 配置 C. 控制器 D. 前端实现 四.参 ...

  6. Nginx专题(2):Nginx的负载均衡策略及其配置

    本文介绍了Nginx的负载均衡策略,一致性hash分配原理,及常用的故障节点的摘除与恢复配置. 文章来源:宜信技术学院 & 宜信支付结算团队技术分享第一期-宜信支付结算八方数据团队高级技术经理 ...

  7. 洛谷 题解 P5595 【【XR-4】歌唱比赛】

    本蒟蒻又双叒叕被爆踩了. 考试时一遍过 其实这题还是很简单的,难度不会大于普及组T1. CSP 2019 RP++ 看开始看到题目,觉得特别长,不想看... 我来和你们分析分析题目,你们就都可以秒了. ...

  8. [TimLinux] scrapy 在Windows平台的安装

    1. 安装Python 这个不去细说,官网直接下载,安装即可,我自己选择的版本是 Python 3.6.5 x86_64bit windows版本. 2. 配置PATH 我用的windows 10系统 ...

  9. ARTS-S CentOS 7 minimal 版本安装后网络配置

    用root登录服务器,执行 nmcli d 可以看到ethernet disconnected,网卡是处于禁用状态.执行 nmtui 选Edit a connection-Edit,选中Automat ...

  10. JS基础-原型链和继承

    创建对象的方法 字面量创建 构造函数创建 Object.create() var o1 = {name: 'value'}; var o2 = new Object({name: 'value'}); ...