Java采用内部构造器Builder模式进行对类进行构建

好处:

  能保证重叠构造器模式的安全性;

  能保证JAVABeans模式的可读性;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package cn.lonecloud.builder;
/**
 * 使用内部类构建器来对这个类进行构造
 * @Title: MyConstractor.java
 * @Package cn.lonecloud.builder
 * @Description:
 * @author lonecloud
 * @webSite http://wwww.lonecloud.cn
 * @date 2016年9月12日 上午10:17:11
 */
public class MyConstractor {
 
    //some field of class
    private final int size;
     
    private final int servings;
     
    private final int fat;
     
    private final int sodium;
     
    private final int age;
    //using getMethod
    public int getSize() {
        return size;
    }
    public int getServings() {
        return servings;
    }
    public int getFat() {
        return fat;
    }
    public int getSodium() {
        return sodium;
    }
    public int getAge() {
        return age;
    }
     
    @Override
    public String toString() {
        return "MyConstractor [size=" + size + ", servings=" + servings
                + ", fat=" + fat + ", sodium=" + sodium + ", age=" + age + "]";
    }
    //using constractMethod init field
    private MyConstractor(Builder builder){
        this.size=builder.size;
        this.servings=builder.servings;
        this.fat=builder.fat;
        this.sodium=builder.sodium;
        this.age=builder.age;
    }
    //using static class to set this class
    public static class Builder{
        //some field of staticClass
        private final int size;
         
        private final int servings;
        //using method to set field
        private  int fat=0;
         
        private  int sodium=0;
         
        private  int age=0;
        //using constractorMethod to init this final field
        public Builder(int size, int servings) {
            this.size = size;
            this.servings = servings;
        }
        //write method to set field;
        public Builder fat(int fat){
            this.fat=fat;
            return this;
        }
        public Builder sodium(int sodium){
            this.sodium=sodium;
            return this;
        }
        public Builder age(int age){
            this.age=age;
            return this;
        }
        //return MyConstractor
        public MyConstractor build(){
            return new MyConstractor(this);
        }
         
    }
}

测试类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package cn.lonecloud.constactor;
 
import org.junit.Test;
 
import cn.lonecloud.builder.MyConstractor;
 
public class MyTest {
 
    @Test
    public void constractorTest(){
        MyConstractor myConstractor = new MyConstractor.Builder(1,2).age(100).build();
        System.out.println(myConstractor.toString());
    }
}

 

posted @   lonecloud  阅读(1078)  评论(0编辑  收藏  举报
编辑推荐:
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
阅读排行:
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥
我的博客即将同步至 OSCHINA 社区,这是我的 OSCHINA ID:lonecloud,邀请大家一同入驻:https://www.oschina.net/sharing-plan/apply
点击右上角即可分享
微信分享提示