还没有看过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. Flask入门学习——配置参数的管理方式

    一般来说有这么几种方式: 直接操作config的字典对象 app.config["DEBUG"] = True 使用配置文件加载,直接传入文件名 app.config.from_p ...

  2. ++a与a++、--a与a--

    ++a 与 a++ public class Demo { public static void main(String[] args) { int a = 1; System.out.println ...

  3. Python网络爬虫——BeautifulSoup4库的使用

    使用requests库获取html页面并将其转换成字符串之后,需要进一步解析html页面格式,提取有用信息. BeautifulSoup4库,也被成为bs4库(后皆采用简写)用于解析和处理html和x ...

  4. 2019-2020-1 20199304《Linux内核原理与分析》第八周作业

    第七章 Linux内核如何装载和启动一个可执行程序 一.知识点 1.ELF(Executable and Linkable Format)概述: "目标文件"指编译器生成的文件,& ...

  5. iOS13暂时关闭黑暗模式+应用内状态栏无法显示问题解决办法

    现象: iOS13黑暗模式开启后,app显示会出现很多意外显示情况.暂时屏蔽是最好的选择.当开启黑暗模式,且在项目的target对应的info.plist中添加以下设置时(禁用黑暗模式): <k ...

  6. Unity3D 卡通描边之控制线条粗细

    一.前言 之前我发表过一篇Unity3D 卡通渲染 基于退化四边形的实时描边,最重要的实时描边已经实现了,本文接下来要完善一下它. 在之前的实时描边中,使用了几何着色器中的LineStream来进行绘 ...

  7. 基于webpack实现多html页面开发框架二 css打包、支持scss、文件分离

    本节主要介绍webpack打包的时候CSS的处理方式 一.解决什么问题      1.CSS打包      2.CSS处理浏览器兼容      3.SASS支持      4.CSS分离成单独的文件 ...

  8. React Native--Animated:`useNativeDriver`is not supported because the native animated module is missing

    目录 问题描述 解决方法 问题描述 react-native init 项目,在运行中报错 解决方法 打开Xcode项目,点击Libraries文件夹右键Add Files to "项目名& ...

  9. HDU5002 tree

    You are given a tree with N nodes which are numbered by integers 1..N. Each node is associated with ...

  10. Coderfocers-551C

    Professor GukiZ is concerned about making his way to school, because massive piles of boxes are bloc ...