建造者模式之项目运用

举报
chenyu 发表于 2021/07/26 23:59:05 2021/07/26
【摘要】 1 问题 建造者模式,我们也许不陌生,因为我们看到很多开源框架或者Android源码里面用到,类似这样的代码结构 A a = new A.builder().method1("111").method2("222").build(); 很明显,一般这里的结构有builder()构造函数,还有build()函数,主要用来干嘛呢?还不是为了构建对象,如果需要设置的参数很多的...

1 问题

建造者模式,我们也许不陌生,因为我们看到很多开源框架或者Android源码里面用到,类似这样的代码结构

A a = new A.builder().method1("111").method2("222").build();
 

很明显,一般这里的结构有builder()构造函数,还有build()函数,主要用来干嘛呢?还不是为了构建对象,如果需要设置的参数很多的话,一个一个去set很繁琐也不好看,下面有个例子简单粗暴展示。 

 

 

 

 

 

 

 

 

2 测试代码实现


  
  1. package com.example.test1;
  2. public class Student {
  3. private int id;
  4. private String name;
  5. private int age;
  6. private String classNmae;
  7. public Student(Builder builder) {
  8. this.id = builder.id;
  9. this.name = builder.name;
  10. this.age = builder.age;
  11. this.classNmae = builder.classNmae;
  12. }
  13. public int getId() {
  14. return id;
  15. }
  16. public String getName() {
  17. return name;
  18. }
  19. public int getAge() {
  20. return age;
  21. }
  22. public String getClassNmae() {
  23. return classNmae;
  24. }
  25. @Override
  26. public String toString() {
  27. return "Student{" +
  28. "id=" + id +
  29. ", name='" + name + '\'' +
  30. ", age=" + age +
  31. ", classNmae='" + classNmae + '\'' +
  32. '}';
  33. }
  34. public static class Builder {
  35. private int id;
  36. private String name;
  37. private int age;
  38. private String classNmae;
  39. public Builder() {}
  40. public Builder id(int id) {
  41. this.id = id;
  42. return this;
  43. }
  44. public Builder name(String name) {
  45. this.name = name;
  46. return this;
  47. }
  48. public Builder age(int age) {
  49. this.age = age;
  50. return this;
  51. }
  52. public Builder className(String classNmae) {
  53. this.classNmae = classNmae;
  54. return this;
  55. }
  56. public Student build() {
  57. return new Student(this);
  58. }
  59. }
  60. }

  
  1. Student student = new Student.Builder()
  2. .name("chenyu")
  3. .age(28)
  4. .id(1)
  5. .className("erzhong")
  6. .build();
  7. Log.i(TAG, "student toString is:" + student.toString());

 

 

 

 

 

 

 

 

3 运行结果

18376-18376/com.example.test1 I/chenyu: student toString is:Student{id=1, name='chenyu', age=28, classNmae='erzhong'}
 

 

 

 

 

 

 

 



4 总结

1)我们需要构建一个对象很多参数的时候用这种方式

2)最关键的是需要构建的对象类里面有个Builder类作为参数传递


  
  1. public Student(Builder builder) {
  2. this.id = builder.id;
  3. this.name = builder.name;
  4. this.age = builder.age;
  5. this.classNmae = builder.classNmae;
  6. }

3)最后build()函数里面返回的是需要构建的类本身


  
  1. public Student build() {
  2. return new Student(this);
  3. }

 

文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。

原文链接:chenyu.blog.csdn.net/article/details/105058815

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。