0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Android・Java】連続でセッターを記述できるようにする方法

Last updated at Posted at 2016-09-18

Javaで自作クラスのインスタンスのパラメータを設定する時に、連続でセッターを呼ぶ場面があって、DialogFragmentみたいに連続でセッターを呼ぶにはどうすればいいのかなと思って、調べたメモ。

#普通のセッター

Android Studioのセッターの自動生成を利用すると

Message.java
public class Message {

    private String mTitle;
    private String mContent;
    private String mAddress;

    public void setTitle(String title) {
        mTitle = title;
    }

    public void setContent(String content) {
        mContent = content;
    }


    public void setAddress(String address) {
        mAddress = address;
    }
}

これでセッターを呼ぶには

Message message = new Message();
message.setTitle("Hello");
message.setContent("How are you?");
message.setAddress("hoge@foobar.com");
.
.
.

何回もmessageを記述する必要があるので面倒。

目指す形はこんな感じ。

Message message = new Message();
message.setTitle("Hello")
        .setContent("How are you?")
        .setAddress("hoge@foobar.com")
        .setSender("John")
        .send();

#書き換えたセッター

どうやってるのかなと思って、いろんなライブラリのソースの見てみたら意外に簡単だった。

Message.java
public class Message {
    private String mTitle;
    private String mContent;
    private String mAddress;

    public Message setTitle(String title) {
        mTitle = title;
        return this;
    }

    public Message setContent(String content) {
        mContent = content;
        return this;
    }

    public Message setAddress(String address) {
        mAddress = address;
        return this;
    }
}

よく考えれば当たり前だけど、戻り値を自分自身にすればできました。

#追記

@kidotaka さんのご指摘のようにBuilderパターンで実装する方法が定石のようでした。

Message.java
public class Message {

    private String mTitle;
    private String mContent;
    private String mSender;
    private String mAddress;

    static class Builder {
        private Message message;

        public Builder() {
            message = new Message();
        }

        public Builder setTitle(String title) {
            message.setTitle(title);
            return this;
        }

        public Builder setContent(String content) {
            message.setContent(content);
            return this;
        }

        public Builder setSender(String sender) {
            message.setSender(sender);
            return this;
        }

        public Builder setAddress(String address) {
            message.setAddress(address);
            return this;
        }

        public Message build() {
            return message;
        }
    }

パラメータをセットする。

new Message.Builder()
        .setTitle("Hello")
        .setContent("How are you?")
        .setAddress("hoge@foobar.com")
        .setSender("John")
        .build();
0
0
3

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?