zoukankan      html  css  js  c++  java
  • build设计模式

    又叫生成器模式

    public class MainActivity extends AppCompatActivity {
    
        TextView textView;
        Button button;
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            textView = findViewById(R.id.text);
            button = findViewById(R.id.button);
    
            Student.Builder builder = new Student.Builder();
            String address = builder.address("北京").name("四环").build().getAddress();
            System.out.println(address);
        }
        }

    public class Student {
    
        private int id;
        private String name;
        private String passwd;
        private String sex;
        private String address;
    
        // 构造器尽量缩小范围
        private Student() {
        }
    
        // 构造器尽量缩小范围
        private Student(Student origin) {
            // 拷贝一份
            this.id = origin.id;
            this.name = origin.name;
            this.passwd = origin.passwd;
            this.sex = origin.sex;
            this.address = origin.address;
        }
    
        public int getId() {
            return id;
        }
    
        public String getName() {
            return name;
        }
    
        public String getPasswd() {
            return passwd;
        }
    
        public String getSex() {
            return sex;
        }
    
        public String getAddress() {
            return address;
        }
    
        /**
         * Student的创建完全依靠Student.Builder,使用一种方法链的方式来创建
         *
         */
        public static class Builder {
    
            private Student target;
    
            public Builder() {
                target = new Student();
            }
    
            public Builder address(int id) {
                target.id = id;
                return this;
            }
    
            public Builder name(String name) {
                target.name = name;
                return this;
            }
    
            public Builder password(String passwd) {
                target.passwd = passwd;
                return this;
            }
    
            public Builder sex(String sex) {
                target.sex = sex;
                return this;
            }
    
            public Builder address(String address) {
                target.address = address;
                return this;
            }
    
            public Student build() {
                return new Student(target);
            }
    
        }
    
    }
  • 相关阅读:
    开通博客园
    ios关键字
    FirstDay
    An example for pysnmp
    remove debug symbols to a seperate file
    qemu下通过gdb调试内核时 遇到 evaluation of this expression requires the program to have a function "malloc" 错误的解决办法
    关于常识与知识的思考
    基于Qemu在ubuntu上构建linux学习环境
    How to download prebuilt toolchain
    诡异的打印异常BUG
  • 原文地址:https://www.cnblogs.com/Ocean123123/p/11023405.html
Copyright © 2011-2022 走看看