Builder模式的目的是解耦构建过程,为什么要用内部类?
还没有看过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模式的目的是解耦构建过程,为什么要用内部类?的更多相关文章
- Java采用内部构造器Builder模式进行对类进行构建
好处: 能保证重叠构造器模式的安全性: 能保证JAVABeans模式的可读性: package cn.lonecloud.builder; /** * 使用内部类构建器来对这个类进行构造 * @Tit ...
- Builder模式在Java中的应用
在设计模式中对Builder模式的定义是用于构建复杂对象的一种模式,所构建的对象往往需要多步初始化或赋值才能完成.那么,在实际的开发过程中,我们哪些地方适合用到Builder模式呢?其中使用Build ...
- Builder模式(建造者模式)
在设计模式中对Builder模式的定义是用于构建复杂对象的一种模式,所构建的对象往往需要多步初始化或赋值才能完成.那么,在实际的开发过程中,我们哪些地方适合用到Builder模式呢?其中使用Build ...
- 设计模式之Builder模式
一.感性认识 二.Builder模式 1.定义 一个复杂对象的构建与其表示相分离,使得同样的构建过程可以创建不同的表示.即构建过程相同,但是子部件却不相同. 2.结构说明 Builder: 创建者接口 ...
- Builder模式在Java中的应用(转)
在设计模式中对Builder模式的定义是用于构建复杂对象的一种模式,所构建的对象往往需要多步初始化或赋值才能完成.那么,在实际的开发过程中,我们哪些地方适合用到Builder模式呢?其中使用Build ...
- java的设计模式 - Builder模式
Builder 模式的目的? 构造对象的方式过于复杂,不如将之抽离出来.比如,构造器参数过多 这样说也有点抽象,举个例子吧. 举个例子 比如 非常热门的消息队列RabbitMQ 的 AMQP.Basi ...
- 【5】Builder模式(构建者模式)
一.引言 在软件系统中,有时需要创建一个复杂对象,并且这个复杂对象由其各部分子对象通过一定的步骤组合而成.例如一个采购系统中,如果需要采购员去采购一批电脑时,在这个实际需求中,电脑就是一个复杂的对象, ...
- 设计模式之构建者(Builder)模式
在五大设计原则的基础上经过GOF(四人组)的总结,得出了23种经典设计模式,其中分为三大类:创建型(5种).结构型(7种).行为型(11种).今天对创建型中的构建者(Builder)模式的思想进行了一 ...
- JAVA Builder模式构建MAP/LIST的示例
我们在构建一个MAP时,要不停的调用put,有时候看着觉得很麻烦,刚好,看了下builder模式,觉得这思路不错,于是乎,照着用builder模式写了一个构建MAP的示例,代码如下: import j ...
随机推荐
- linux 精确延时
void HeartBeat_Check_TASK(void *pdata){ struct timeval tv; struct timespec ts; int err; U32 dwcount= ...
- SpringBoot 使用IDEA 配置热部署
在开发中稍微更改一点内容就要重启,很麻烦.这个时候使用Spring Boot的热部署就能解决你的问题. 上图: 1,在pom.xml文件中添加依赖: <dependency> <gr ...
- Asp.net Core 3.0 Identity 使用smtp账户确认和密码恢复
当新建一个core项目后,使用identity基架后,确认邮件出现了错误,并不能正常使用. 建立文档在这里 https://docs.microsoft.com/zh-cn/aspnet/core/s ...
- Apache服务——个人用户主页功能
使用Apache服务部署静态网站(二) 个人用户主页功能 Apache服务程序中有个默认未开启的个人用户主页功能,能够为所有系统内的用户生成个人网站,确实很实用哦~ 第1步:开启个人用户主页功能: [ ...
- springboot整合thymleaf模板引擎
thymeleaf作为springboot官方推荐使用的模板引擎,简单易上手,功能强大,thymeleaf的功能和jsp有许多相似之处,两者都属于服务器端渲染技术,但thymeleaf比jsp的功能更 ...
- Apple Developer swift教程学习笔记
https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson6. ...
- Java修炼——容器体系框架总结
容器有俩大接口Collection接口(无序,不唯一)和Map接口 Collection接口有俩个子接口分别是List和Set. List接口特点是有序但是不唯一,她有三个子接口分别是:ArrayLi ...
- [TimLinux] django html如何实现固定表头
1. 需求 表格很长,这个时候就希望表格头部不动,而只是表格内容行支持滚动功能. 2. 方法 两张表:一张表(THeader)负责头部字段的显示,另一张表(TBody)负责内容行字段的显示. 两张表都 ...
- POJ 1949 Chores
Farmer John's family pitches in with the chores during milking, doing all the chores as quickly as p ...
- Kerberos+SSH安装配置使用教程
一.背景说明 最早听说KDC和Kerberos应该是大三的<应用密码学>,当时感觉这套对称密钥分发机制比非对称密钥的PKI分发机制要好理解.但几年下来由于现实中使用SSL的场景比较比(主要 ...