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

  1. package xyz.n490808114.test;
  2. public class BuilderTest{
  3. String name;
  4. int age;
  5. int high;
  6. int weight;
  7. int speed;
  8.  
  9. public BuilderTest name(String name){
  10. this.name = name;
  11. return this;
  12. }
  13. public BuilderTest age(int age){
  14. this.age = age;
  15. return this;
  16. }
  17. public BuilderTest high(int high){
  18. this.high = high;
  19. return this;
  20. }
  21. public BuilderTest weight(int weight){
  22. this.weight = weight;
  23. return this;
  24. }
  25. public BuilderTest speed(int speed){
  26. this.speed = speed;
  27. return this;
  28. }
  29. public void setName(String name){this.name = name;}
  30. public void setAge(int age){this.age = age;}
  31. public void setHigh(int high){this.high = high;}
  32. public void setWeight(int weight){this.weight = weight;}
  33. public void setSpeed(int speed){this.speed = speed;}
  34. public String getName(){return this.name;}
  35. public int getAge(){return this.age;}
  36. public int getHigh(){return this.high;}
  37. public int getWeight(){return this.weight;}
  38. public int getSpeed(){return this.speed;}
  39. public static void main(String[] args) {
  40. BuilderTest t = new BuilderTest().age(20).speed(50);
  41. System.out.println(t.name);
  42. System.out.println(t.age);
  43. System.out.println(t.high);
  44. System.out.println(t.weight);
  45. System.out.println(t.speed);
  46. }
  47. }

输出为

  1. null
  2. 20
  3. 0
  4. 0
  5. 50

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

  1. package xyz.n490808114.test;
  2. public class BuilderTest{
  3. String name;
  4. int age;
  5. int high;
  6. int weight;
  7. int speed;
  8. public BuilderTest setName(String name){this.name = name;return this;}
  9. public BuilderTest setAge(int age){this.age = age;return this;}
  10. public BuilderTest setHigh(int high){this.high = high;return this;}
  11. public BuilderTest setWeight(int weight){this.weight = weight;return this;}
  12. public BuilderTest setSpeed(int speed){this.speed = speed;return this;}
  13. public String getName(){return this.name;}
  14. public int getAge(){return this.age;}
  15. public int getHigh(){return this.high;}
  16. public int getWeight(){return this.weight;}
  17. public int getSpeed(){return this.speed;}
  18. public static void main(String[] args) {
  19. BuilderTest t = new BuilderTest().setAge(20).setSpeed(50);
  20. System.out.println(t.name);
  21. System.out.println(t.age);
  22. System.out.println(t.high);
  23. System.out.println(t.weight);
  24. System.out.println(t.speed);
  25. }
  26. }

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

能够想到的是:

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

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

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

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

Builder模式

  1. package xyz.n490808114.test;
  2. public class BuilderTest{
  3. String name;
  4. int age;
  5. int high;
  6. int weight;
  7. int speed;
  8. public BuilderTest(Builder builder){
  9. this.name = builder.name;
  10. this.age = builder.age;
  11. this.high = builder.high;
  12. this.weight = builder.weight;
  13. this.speed = builder.speed;
  14. }
  15. public void setName(String name){this.name = name;}
  16. public void setAge(int age){this.age = age;}
  17. public void setHigh(int high){this.high = high;}
  18. public void setWeight(int weight){this.weight = weight;}
  19. public void setSpeed(int speed){this.speed = speed;}
  20. public String getName(){return this.name;}
  21. public int getAge(){return this.age;}
  22. public int getHigh(){return this.high;}
  23. public int getWeight(){return this.weight;}
  24. public int getSpeed(){return this.speed;}
  25.  
  26. public static class Builder{
  27. String name;
  28. int age;
  29. int high;
  30. int weight;
  31. int speed;
  32. public Builder name(String name){
  33. this.name = name;
  34. return this;
  35. }
  36. public Builder age(int age){
  37. this.age = age;
  38. return this;
  39. }
  40. public Builder high(int high){
  41. this.high = high;
  42. return this;
  43. }
  44. public Builder weight(int weight){
  45. this.weight = weight;
  46. return this;
  47. }
  48. public Builder speed(int speed){
  49. this.speed = speed;
  50. return this;
  51. }
  52. public BuilderTest build(){
  53. return new BuilderTest(this);
  54. }
  55. }
  56. public static void main(String[] args) {
  57. BuilderTest t = new BuilderTest.Builder().age(20).speed(50).build();
  58. BuilderTest s = new BuilderTest.Builder().age(120).speed(150).build();
  59. System.out.println(t.name);
  60. System.out.println(t.age);
  61. System.out.println(t.high);
  62. System.out.println(t.weight);
  63. System.out.println(t.speed);
  64. System.out.println(s.name);
  65. System.out.println(s.age);
  66. System.out.println(s.high);
  67. System.out.println(s.weight);
  68. System.out.println(s.speed);
  69. }
  70. }

输出为

  1. null
  2. 20
  3. 0
  4. 0
  5. 50
  6. null
  7. 120
  8. 0
  9. 0
  10. 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. 极化码之tal-vardy算法(1)

    继前两节我们分别探讨了极化码的编码,以及深入到高斯信道探讨高斯近似法之后,我们来关注一个非常重要的极化码构造算法.这个算法并没有一个明确的名词,因此我们以两位发明者的名字将其命名为“Tal-Vardy ...

  2. 【并发编程】关于Thread类的详细介绍

    多线程编程基础--Thread类 Thread类是Java中实现多线程编程的基础类.本篇博客就来介绍下Thread类的常用API和常见用法. Thread类常用的方法如下: Thread.active ...

  3. jQuery九宫格抽奖

    <div id="box"> <div class="content content-1">1</div> <div ...

  4. linux下docker安装部署项目(全)

    一 .系统安装 基于CentOS-7-x86_64-Minimal-1708.iso安装系统 1.2.  配置系统 1.2.1  在线更新内核版本(建议更新,旧版内核会有docker BUG) 1.2 ...

  5. 有了 serverless,前端也可以快速开发一个 Puppeteer 网页截图服务

    更多云原生技术资讯可关注阿里巴巴云原生技术圈. Puppeteer 是什么? puppeteer 官网的介绍如下: Puppeteer is a Node library which provides ...

  6. imageView的使用

    转自:http://www.runoob.com/ios/att-ios-ui-imageview.html 图像视图用于显示单个图像或动画序列的图像. 重要的属性 image highlighted ...

  7. ES6——async函数

    目录 1.async 函数是 Generator 函数的语法糖. 2.async函数对 Generator 函数的改进,体现在以下四点. 3.基本用法 一个获取股票报价的函数 指定多少毫秒后输出一个值 ...

  8. 过滤器和监听器实现用户的在线登录人数,以及设置session时长。

    过滤器: package com.bjsxt.filter; import java.io.IOException; import javax.servlet.Filter; import javax ...

  9. [TimLinux] Python nonlocal和global的作用

    1. 执行代码 以下实例都是通过执行以下代码,需要把以下执行代码放在后面实例代码的后面. a = outer_func()print("call a()") a() a() a() ...

  10. redis第一讲【redis的描述,linux和docker下的安装使用】

    Redis(REmote DIctionary Server):是什么 redis(远程字典服务器),是完全开源免费的,高性能的k/v分布式内存数据,热门的Nosql数据库 Redis可以干什么: 内 ...