建造者模式之項(xiàng)目運(yùn)用

1 問題

建造者模式,我們也許不陌生,因?yàn)槲覀兛吹胶芏嚅_源框架或者Android源碼里面用到,類似這樣的代碼結(jié)構(gòu)

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

很明顯,一般這里的結(jié)構(gòu)有builder()構(gòu)造函數(shù),還有build()函數(shù),主要用來干嘛呢?還不是為了構(gòu)建對(duì)象,如果需要設(shè)置的參數(shù)很多的話,一個(gè)一個(gè)去set很繁瑣也不好看,下面有個(gè)例子簡(jiǎn)單粗暴展示。

 
2 測(cè)試代碼實(shí)現(xiàn)

    package com.example.test1;
     
    public class Student {
        private int id;
        private String name;
        private int age;
        private String classNmae;
     
        public Student(Builder builder) {
            this.id = builder.id;
            this.name = builder.name;
            this.age = builder.age;
            this.classNmae = builder.classNmae;
        }
        public int getId() {
            return id;
        }
     
        public String getName() {
            return name;
        }
     
        public int getAge() {
            return age;
        }
     
        public String getClassNmae() {
            return classNmae;
        }
     
        @Override
        public String toString() {
            return "Student{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    ", classNmae='" + classNmae + '\'' +
                    '}';
        }
     
        public static class Builder {
            private int id;
            private String name;
            private int age;
            private String classNmae;
     
            public Builder() {}
            public Builder id(int id) {
                this.id = id;
                return this;
            }
            public Builder name(String name) {
                this.name = name;
                return this;
            }
            public Builder age(int age) {
                this.age = age;
                return this;
            }
            public Builder className(String classNmae) {
                this.classNmae = classNmae;
                return this;
            }
            public Student build() {
                return new Student(this);
            }
        }
    }

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

 
3 運(yùn)行結(jié)果

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


4 總結(jié)

1)我們需要構(gòu)建一個(gè)對(duì)象很多參數(shù)的時(shí)候用這種方式

2)最關(guān)鍵的是需要構(gòu)建的對(duì)象類里面有個(gè)Builder類作為參數(shù)傳遞

        public Student(Builder builder) {
            this.id = builder.id;
            this.name = builder.name;
            this.age = builder.age;
            this.classNmae = builder.classNmae;
        }

3)最后build()函數(shù)里面返回的是需要構(gòu)建的類本身

            public Student build() {
                return new Student(this);
            }


 

 

作者:chen.yu
深信服三年半工作經(jīng)驗(yàn),目前就職游戲廠商,希望能和大家交流和學(xué)習(xí),
微信公眾號(hào):編程入門到禿頭 或掃描下面二維碼
零基礎(chǔ)入門進(jìn)階人工智能(鏈接)