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);
            }
    
        }
    
    }
  • 相关阅读:
    Linux 守护进程一
    Linux 改进捕捉信号机制(sigaction,sigqueue)
    Linux 发送信号
    Linux 信号捕捉
    Heartbeat+DRBD+MFS高可用
    centos7 MFS drbd keepalived
    RabbitMQ-官方指南-RabbitMQ配置
    CentOS 7 单用户模式+救援模式
    CentOS6.8 x64+Nginx1.3.8/Apache-httpd 2.4.3+PHP5.4.8(php-fpm)+MySQL5.5.28+CoreSeek4.1源码编译安装
    nginx定制header返回信息模块ngx_headers_more
  • 原文地址:https://www.cnblogs.com/Ocean123123/p/11023405.html
Copyright © 2011-2022 走看看