Quick actions

cmd+k|ctrl+k

Navigation

Languages

Builder

Snippet info

Language

Java

Visibility

public

Author

memo.minsk

Created

2017-01-31T09:55:11Z

Updated

2017-01-31T09:55:45Z

class Data {

    public final int a;
    public final int b;
    public final int c;

    public Data(int a, int b, int c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    @Override
    public String toString() {
        return "Data{a=" + a + ", b=" + b + ", c=" + c + '}';
    }

}

class DataBuilder {

    private int a;
    private int b;
    private int c;

    public DataBuilder setA(int a) {
        this.a = a;
        return this;
    }

    public DataBuilder setB(int b) {
        this.b = b;
        return this;
    }

    public DataBuilder setC(int c) {
        this.c = c;
        return this;
    }

    public Data build() {
        return new Data(this.a, this.b, this.c);
    }

}

public class Main {

    public static void main(String[] args) {

        // один билдер на все приложение
        // он хранит состояние последнего объекта
        DataBuilder builder = new DataBuilder();

        for (int i = 0; i < 3; i++) {
            builder.setA(i);
            for (int j = 0; j < 3; j++) {
                builder.setB(j);
                for (int k = 0; k < 3; k++) {
                    builder.setC(k);
                    // объект билдится прямо перед сохранением
                    System.out.println(builder.build());
                }
            }
        }

    }

}
INFO