Constructor Builder

				
					public class Example {

    private final ArrayList<String> arrayList;
    private int id;
    private double price;
    private boolean isReal;

    public static class Builder {
        private final ArrayList<String> arrayList;
        private int id;
        private double price;
        private boolean isReal;

        public Builder(String... strings) {
            this.arrayList = new ArrayList<>();
            this.arrayList.addAll(Arrays.asList(strings));
        }

        public Builder id(int i) {
            id = i;
            return this;
        }

        public Builder price(int i) {
            price = i;
            return this;
        }
        
        public Builder isReal(boolean b) {
            isReal = true;
            return this;
        }

        public Example build() {
            return new Example(this);
        }
    }


    public Example(Builder builder) {
        arrayList = builder.arrayList;
    }
				
			
				
					public class Main {

public static void main(String[] args) {

Example example = new Example.Builder("first","second")
    .id(7)
    .price(9)
    .isReal(true)
    .build();
}
				
			

Zostaw komentarz