0
0

More than 3 years have passed since last update.

Android Builderの使い方

Last updated at Posted at 2019-11-25

本内容は以下のサイトを参照して作ったです。
https://blog.csdn.net/androidxiaogang/article/details/80586631 

// static 内部クラスの使い方
外部クラスの属性は多いし、可変し、絶対必須ではない。
そして、使用者によって非必須の属性を設定するのは使用者次第なので、
普通のやり方で、各属性のset関数でやります。
或いは、いっぱいのコンストラクタ関数を用意する。

Builderのコンストラクタ関数は可変で、使い方は便利である。
個人的な感じはBuilderパータンの方法は普通の使い型より少しだけを簡単になる。

以下のクラスの使い方法は

Person.java

public class Person {
    /*名前(必須)*/
    private final String name;
    /*性別(必須)*/
    private final String gender;
    /*年齢(非必須)*/
    private final String age;
    /*靴(非必須)*/
    private final String shoes;
    /*服(非必須)*/
    private final String clothes;
    /*金(非必須)*/
    private final String money;
    /*マンショ(非必須)*/
    private final String house;
    /*車(非必須)*/
    private final String car;
    /*仕事(非必須)*/
    private final String career;

    private Person(Builder builder) {
        this.name = builder.name;
        this.gender = builder.gender;
        this.age = builder.age;
        this.shoes = builder.shoes;
        this.clothes = builder.clothes;
        this.money = builder.money;
        this.house = builder.house;
        this.car = builder.car;
        this.career = builder.career;
    }

    public static class Builder {
        private final String name;
        private final String gender;
        private String age;
        private String shoes;
        private String clothes;
        private String money;
        private String house;
        private String car;
        private String career;

        public Builder(String name,String gender) {
            this.name = name;
            this.gender = gender;
        }

        public Builder age(String age) {
            this.age = age;
            return this;
        }

        public Builder car(String car) {
            this.car = car;
            return this;
        }

        public Builder shoes(String shoes) {
            this.shoes = shoes;
            return this;
        }

        public Builder clothes(String clothes) {
            this.clothes = clothes;
            return this;
        }

        public Builder money(String money) {
            this.money = money;
            return this;
        }

        public Builder house(String house) {
            this.house = house;
            return this;
        }

        public Builder career(String career) {
            this.career = career;
            return this;
        }

    // 方法1  private Person(Builder builder)が必要
        public Person build(){
            return new Person(this);
        }
    // 方法1  private Person(Builder builder)がない
        public Person build(){
            Person person = new Person();
            person.name = this.name;
            return person;
        }
    }

使い方:
Person person = new Person.Builder("ほげほげ","もげもげ")
.age(32)
.money(10000000)
.builder();

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0