• 设计模式:Build模式


    优点:

    提高代码的可读性

    示例:

    public class Person {
        private String name;
        private int age;
        private double height;
        private double weight;
    
        private Person(Builder builder) {
            this.name=builder.name;
            this.age=builder.age;
            this.height=builder.height;
            this.weight=builder.weight;
        }
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public double getHeight() {
            return height;
        }
    
        public void setHeight(double height) {
            this.height = height;
        }
    
        public double getWeight() {
            return weight;
        }
    
        public void setWeight(double weight) {
            this.weight = weight;
        }
    
        static class Builder{
            private String name;
            private int age;
            private double height;
            private double weight;
            public Builder name(String name){
                this.name=name;
                return this;
            }
            public Builder age(int age){
                this.age=age;
                return this;
            }
            public Builder height(double height){
                this.height=height;
                return this;
            }
    
            public Builder weight(double weight){
                this.weight=weight;
                return this;
            }
    
            public Person build(){
                return new Person(this);
            }
        }
    }

    使用起来:

    Person.Builder builder=new Person.Builder();
    Person person=builder
            .name("张三")
            .age(18)
            .height(178.5)
            .weight(67.4)
            .build();

    常见的例子,对话框

    AlertDialog.Builder builder=new AlertDialog.Builder(this);
    AlertDialog dialog=builder.setTitle("标题")
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setView(R.layout.myview)
            .setPositiveButton(R.string.positive, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
    
                }
            })
            .setNegativeButton(R.string.negative, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
    
                }
            })
            .create();
    dialog.show();
  • 相关阅读:
    [机器学习实战]K-近邻算法
    [机器学习Lesson3] 梯度下降算法
    [机器学习Lesson 2]代价函数之线性回归算法
    [机器学习Lesson 1] 机器学习简介
    Flume
    Kafka基础入门
    打造MacOS版“XShell”
    你好,智哥(摘自微信公众号“野兔故事会”)
    打印小册子
    php--strlen()与mb_strlen的作用与区别
  • 原文地址:https://www.cnblogs.com/zhaozilongcjiajia/p/11752246.html
Copyright © 2020-2023  润新知