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 ...
随机推荐
- Node_exporter一键安装部署脚本(Shell)
#!/bin/bash # # rhel7. 安装node_exporter 用于监控数据采集 # Usage: # sh addNode.sh #Logs: /var/log/messages #H ...
- 远程连接mysql出现1045错误的解决办法
第一步:停止MySQL服务 第二步:在你MySQL的安装目录下找到my.ini,文件,打开文件查找到 [mysqld] ,在其下方添加上一行 skip-grant-tables,然后保存. 第三步:启 ...
- 教你如何关闭IIS服务
由于IIS服务器和Apache的默认端口号都是80端口,有时我们需要关闭IIS服务,下面讲讲关闭IIS服务的方法. 方法如下: 1.services.msc,在里面找到一个“World Wid ...
- 关于token你需要知道的【华为云技术分享】
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/devcloud/article/detai ...
- NIO-Channel
目录 NIO-Channel 目录 前言 什么是Channel 通道类型 如何使用 ServerSocketChannel SocketChannel FileChannel 总结 相关文献 NIO- ...
- BOM对象中的常用方法
先看body中的内容: <body οnlοad="demo1()"> <p> <input type="button" id=& ...
- Open-Pit Mining
Description Open-pit mining is a surface mining technique of extracting rock or minerals from the ea ...
- VMware“该虚拟机似乎正在使用中”
问题现象: 在用VMware虚拟机的时候,有时会发现打开虚拟机时提示"该虚拟机似乎正在使用中.如果该虚拟机未在使用,请按"获取所有权(T)"按钮获取它的所有权.否则,请按 ...
- Android中实现轮询下载安装应用的Dialog
前言 因为有涉及到静默安装,主要是针对已经root的设备,应需求,是在ARM的主板上开发的,所以对于常规手机仅能做到轮询下载,做不到静默安装. 效果图 用的鸿洋大神的百分比布局,各个分辨率都完美适配, ...
- 深度优先搜索 & 广度优先搜索
目录 邻接表 邻接表的深度优先搜索 邻接表的广度优先搜索 临接数组 临接数组的深度优先搜索 临接数组的广度优先搜索 二叉树 二叉树的深度优先搜索 二叉树的广度优先搜索 邻接表 邻接表的深度优先搜索 假 ...