Help us understand the problem. What is going on with this article?

Android Builderの使い方

本内容は以下のサイトを参照して作ったです。
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();

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした